Transcription of Combining LaTeX with Python - TeX Users Group (TUG)
1 Combining LaTeX with PythonUwe ZiegenhagenAugust 9, 2019 Dante e. V. Heidelberg1 About me Uwe Ziegenhagen, from Cologne, Germany In-house analyst in banking and private equity Responsible for developing and maintainingindividual software applications Teacher for IT-related subjects at a private Universityof Applied Sciences2 What s this talk about? LaTeX -files are pure text files, so pretty much anyprogramming language can be used to create them Python has been my favourite programminglanguage Python is sufficiently fast, easy to learn and has ahuge set of libraries This talk is about Python and the way we canutilize it with LATEX3 Today s Topics Introducing Python Creating LaTeX files with Python Running Python from within LATEX4 Python Is a general purpose scripting language Comes with a rich standard library batteriesincluded Was invented 1991 by Guido van Rossum at theCentrum Wiskunde & Informatica in theNetherlands, Version in 1994 Comes in version and use version 3!
2 5 Python Design Philosophy Open source Simple, intuitive, but incredibly powerful Source code as readable as plain English Is suitable for everyday jobs as well as for machinelearning Offers short development cycles Uses indentation, not brackets6 Some basic PythonThe usual Hello World! 1print( Hello + + World )Some function definition1def addTwo(a, b):2return a+b34print(addTwo(5,3))5print(addTwo( U , S ))Interation over lists, arrays, = Hello TUG! 2for i in some_string:3print(i)7 Some functional PythonMapping a function on a list1some_list = [1, 2, 3, 4, 5, 6]2g = lambda x : x**23print(list(map(g,some_list)))Filter ing even values from a list1some_list = [1, 2, 3, 4, 5, 6, 7, 8]2result = filter(lambda x: x % 2 == 0, some_list)3print(list(result))8 Some object-oriented PythonClasses and objects1class Person:23def __init__(self, name, age) = = age67def print_age(self):8print( + is + str( ))910john = Person( John , 50) ()9 Today s Topics Introducing PythonX Creating LaTeX files with Python Doing Python within LATEX10 Creating FilesWriting LaTeX -Files I Context managerwith takes care of errors and closes the file handleafterwards Backslashes need to be escaped11with open( , w ) as ( \\documentclass{article}\n ) ( \\begin{document}\n ) ( Hello Palo Alto!)
3 \n ) ( \\end{document}\n )Listing 1: Writing TEX-files1 There are raw stringsr hello as well..11 Writing LaTeX -Files II1import subprocess, os23with open( , w ) as ( \\documentclass{article}\n ) ( \\begin{document}\n ) ( Hello Palo Alto!\n ) ( \\end{document}\n )89x = ( pdflatex )10if x != 0:11print( Exit-code not 0, check result! )12 ( start )Listing 2: Writing & Processing TEX-files12 Dynamic Text Replacements I Define variableplace Read template file with $variable$inside Replace$SomePlace$ with variable Write new file1place = Palo Alto 23with open( , r ) as myfile:4text = ()5text_new = ( $SomePlace$ , place)67with open( , w ) as (text_new)Listing 3: Replacing text13 Dynamic Text Replacements II Approach can be easily extended kvis a key-value dictionary1kv = { place : Palo Alto , month : August }23with open( , r ) as myfile:4text = ()56for key, value in ():7text = ( $ +key+ $ , value)89with open( , w ) as (text)Listing 4.
4 Replacing text with dictionaries14 Python s Jinja2 Template System Approach works, but it s like re-inventing thewheel Python offers a variety of template engines2 Some template engines even allow templates to bemixed with logic I have worked with Jinja23: full Unicode support,sandboxed execution, template inheritance, etc. For instance you can reconfigure Jinja2 to better fitoutput formats such as LaTeX or JavaScript. 2 A Basic (non-TEX) Example1from jinja2 import Template23mytemplate = Template("Hello {{place}}!")4print( (place="Palo Alto"))56mytemplate = Template("Some numbers: {% for n in range(1,10) %}{{n}}{% endfor %}")7print( ())Listing 5: A Jinja2 exampleWhat can we learn from this example:1. Syntax is (easily) understandable2. Jinja2 brings its own notation for looping, Extensive use of { , % , } 16 Jinja2 for LATEX1import os2import jinja234latex_jinja_env = (5block_start_string = \BLOCK{ ,6block_end_string = } ,7variable_start_string = \VAR{ ,8variable_end_string = } ,9comment_start_string = \#{ ,10comment_end_string = } ,11line_statement_prefix = %- ,12line_comment_prefix = %# ,13trim_blocks = True,14autoescape = False,15loader = ( (.)))
5 16)17 Jinja2 for LaTeX - Some Explanation based allows to load templates from the file system redefines the template structure:single variablesinstead of {{ }} we use\VAR{}logic blocksinstead of\{% %\}we use\BLOCK{} both commands will be defined in the document asempty commands via\newcommand(so the templatecan be compiled as well)18 Jinja Example generating LaTeX I1\documentclass[12pt,english]{article}2 \usepackage[T1]{fontenc}3\usepackage{bab el}45\newcommand{\VAR}[1]{}6\newcommand{ \BLOCK}[1]{}78\begin{document}910 Hello \VAR{place}!1112\end{document}Listing 6: LaTeX Template for Jinja219 Jinja Example generating LaTeX II Excerpt from the complete code Running the Python Code replaces the placeholderswith content1# load template from file2template = ( )3# combine template and variables4document = (place= Palo Alto )5#write document6with open( , w ) as (document)Listing 7: Rendering the document20 Jinja Example generating LaTeX II: Output1\documentclass[12pt,english]{arti cle}2\usepackage[T1]{fontenc}3\usepackag e{babel}45\newcommand{\VAR}[1]{}6\newcom mand{\BLOCK}[1]{}78\begin{document}910 Hello Palo Alto!
6 1112\end{document}Listing 8: The generated document21 Extending the Python Code Excerpt from full code, uses a list of cities Save each file under <cityname>.tex, replacesspaces in filename Could be extended to run in parallel threads: = ( )2list_of_towns = [ Berlin , New York , Tokyo ]34for town in list_of_towns:5document = (place=town)6with open( ( , ) + .tex , w ) as (document)8x = ( pdflatex + ( , ) + .tex )9if x != 0:10print( Exit-code not 0 for + town + , check Code! )22 Jinja 2 - A complex example For several years I had been treasurer for acharitable makerspace in Cologne Donations were tax deductable, receipts had to bemade following some strict template Relies heavily on pandas library, which offers R-like DataFrames Uses Excel files for the storage of data code23 The LaTeX Template for the Tax ReceiptsAussteller (Bezeichnung und Anschrift der steuerbeg nstigten Einrichtung)Name des Vereins, Anschrift des Vereins, PLZ und OrtSammelbest tigung ber Geldzuwendungen/Mitgliedsbeitr geim Sinne des 10b des Einkommensteuergesetzes an eine der in 5 Abs.
7 1 Nr. 9 des K rperschaftsteuergesetzesbezeichneten K rperschaften, Personenvereinigungen oder Verm gensmassenName und Anschrift des Zuwendenden<Empf nger der Spendenquittung>Summe der Zuwendungen - in Ziffern -- in Buchstaben -Zeitraum der Sammelbest tigung123,45e Einhundertdreiundzwangig sind wegen F rderung (Angabe des beg nstigten Zwecks / der beg nstigten Zwecke)nach dem letzten uns zugegangenen Freistellungsbescheid bzw. nach der Anlage zum K rperschafts-steuerbescheid des FinanzamtsStNrvomf r den letzten Veran-lagungszeitraumnach 5 Abs. 1 Nr. 9 des K rperschaftssteuergesetzes von der K rper-schaftssteuer und nach 3 Nr. 6 des Gewerbesteuergesetzes von der Gewerbesteuer Einhaltung der satzungsm igen Voraussetzungen nach den 51, 59, 60 und 61 AO wurdevom Bescheid vomnach 60 AO gesondertfestgestellt. Wir f rdern nach unserer Satzung (Angabe des beg nstigten Zwecks / der beg nstigtenZwecke)Es wird best tigt, dass die Zuwendung nur zur F rderung der beg nstigten Zwecke 1, 2, 3 und 4AO verwendet wird best tigt, dass ber die in der Gesamtsumme enthaltenen Zuwendungen keine weiteren Be-st tigungen, weder formelle Zuwendungsbest tigungen noch Beitragsquittungen o.
8 , ausgestellt wur-den und es sich um den Verzicht auf Erstattung von Aufwendungen handelt, ist der Anlage zur Sammelbe-st tigung zu , den 12. M rz 2014 Max Mustermann(Ort, Datum und Unterschrift des Zuwendungsempf ngers)Hinweis:Wer vors tzlich oder grob fahrl ssig eine unrichtige Zuwendungsbest tigung erstellt oderwer veranlasst, dass Zuwendungen nicht zu den in der Zuwendungsbest tigung angegebenen steuer-beg nstigten Zwecken verwendet werden, haftet f r die Steuer, die dem Fiskus durch einen etwaigenAbzug der Zuwendungen beim Zuwendenden entgeht ( 10b Abs. 4 EStG, 9 Abs. 3 KStG, 9 Nr. 5 GewStG).Diese Best tigung wird nicht als Nachweis f r die steuerliche Ber cksichtigung der Zuwendung an-erkannt, wenn das Datum des Freistellungsbescheides l nger als 5 Jahre bzw. das Datum der derFeststellung der Einhaltung der satzungsm igen Voraussetzungen nach 60 Abs.
9 1 AO l nger als 3 Jahre seit Ausstellung des Bescheides zur ckliegt ( 63 Abs. 5 AO).Anlage zur Sammelbest tigungDatum der Zuwendung Art der ZuwendungVerzicht auf die Erstattung vonAufwendungen (ja/nein) ,00eSumme:123,00e24 Today s Topics Introducing PythonX Creating LaTeX files with PythonX Using Python from LATEX25 Using Python from LaTeXSeveral approaches Similar projects like Sweave and knitR exist forPython as well: knitpy ( ) pyLit and PyReport for literate programming I want to show two other approaches Plain-vanilla code execution PythonTEX26My own Approach (Thank you, Google and TSX) Basic idea: Use special LaTeX environment During compilation, write its content to external file Run external program on this file, create output file(requires--shell-escapeenabled) Include this output in the LaTeX output, here withsyntax highlighting by themintedpackage Advantage.
10 Needs only a single LaTeX -run, can beadapted for other languages Disadvantage: Needs adjustments if non-text outputis to be included, always writes and executes witheach LaTeX -run27 Example1\usepackage{fancyvrb}2\makeatlet ter3\newenvironment{pycode}[1]%4{\xdef\d @tn@me{#1}\xdef\r@ncmd{ Python # > # }%5\typeout{Writing file #1}\VerbatimOut{# }%6}7{\endVerbatimOut %8\toks0{\immediate\write18}%9\expandaft er\toks\expandafter1\expandafter{\r@ncmd }%10\edef\d@r@ncmd{\the\toks0{\the\toks1 }}\d@r@ncmd %11\noindent Input12\inputminted{ Python }{\d@ Output14\inputminted{text}{\d@ 9: Write ext. file and execute28 ExampleUsed in the document as following:1\begin{document}23\begin{pyco de}{abc}4import pandas as pd5print( );6print(1+123424)7\end{pycode}8910\end{ document}Listing 10: Write ext. file and execute29 ResultInput1import pandas as pd2print( );3print(1+123424) Different approach:PythonTEXpackage by GeoffreyPoore4, also author of themintedpackage for syntaxhighlighting Workflow: embed Python -code in LaTeX documents run LaTeX on the document runpythontexon the file run LaTeX on the document Python -code is only rerun if it has been modified Supports parallelization and non-text output4 simple PythonTeX example51%!}}}