HOME
PDF

## Note on generating Latex code from Lua and Python

July 4, 2015 page compiled on July 4, 2015 at 3:00pm

Small example is given that generate Latex code on the ﬂy. One uses Lua in which the Lua code is embedded inside the main Latex ﬁle itself using \begin{luacode}....\end{luacode} and the other example uses Python to generate the complete Latex document to a ﬁle, which is then compiled using either pdflatex or lualatex.

When using the Lua method, the ﬁle has to be compiled using lualatex.

Python is one of the few dynamics languages that supports multi-line raw strings using the r""" .... """ construct. This feature allows any arbitrary content to be used as is inside this construct. It also allows one to concatenate Python variables with the string to build the ﬁnal Latex output. Without this feature, using Python to generate Latex code would not be practical to do.

The example used (slightly modiﬁed) is an arbitrary one which builds small multiplication table taken from why-does-luaexec-work-but-the-luacode-environment-doesnt answer by Herbert. There was no speciﬁc reason to pick this example, I just wanted to try to compare using Lua vs. Python to do this, and I was browsing and just saw this example there and it seemed like a good one to try to compare Lua with Python on. More examples will be added.

When the generated Latex code is compiled, it will generate this output:

The two methods are best contrasted and compared using the following diagram. The python script is ﬁrst compiled using Python, then the generated Latex ﬁle is compiled using either lualatex or pdﬂatex.

Here is the Lua and the Python code, side by side, for comparison

 Lua Python \documentclass{article}  \usepackage{luacode}     \begin{document}    \begin{luacode}   tex.print("\\begin{tabular}{|l|l|l|l|l|l|l|l|l|}\\hline")   num = 6  for i=1,num do    for j=1,num do       product = '$'..i..'\\times'..j..'='..i*j..'$';       tex.print(product)      if(j

The Latex ﬁle generated by the Python code above is the following, which is then compiled using pdflatex

\documentclass{article}
\begin{document}

\begin{tabular}{|l|l|l|l|l|l|l|l|l|}\hline
$1 \times 1 = 1$&$1 \times 2 = 2$&$1 \times 3 = 3$&$1 \times 4 = 4$&$1 \times 5 = 5$&$1 \times 6 = 6$\\
$2 \times 1 = 2$&$2 \times 2 = 4$&$2 \times 3 = 6$&$2 \times 4 = 8$&$2 \times 5 = 10$&$2 \times 6 = 12$\\
$3 \times 1 = 3$&$3 \times 2 = 6$&$3 \times 3 = 9$&$3 \times 4 = 12$&$3 \times 5 = 15$&$3 \times 6 = 18$\\
$4 \times 1 = 4$&$4 \times 2 = 8$&$4 \times 3 = 12$&$4 \times 4 = 16$&$4 \times 5 = 20$&$4 \times 6 = 24$\\
$5 \times 1 = 5$&$5 \times 2 = 10$&$5 \times 3 = 15$&$5 \times 4 = 20$&$5 \times 5 = 25$&$5 \times 6 = 30$\\
$6 \times 1 = 6$&$6 \times 2 = 12$&$6 \times 3 = 18$&$6 \times 4 = 24$&$6 \times 5 = 30$&$6 \times 6 = 36$\\
\hline\end{tabular}
\end{document}

It is clear than both using Lua or Python to generate the table is much easier than using direct Tex coding (programming directly in Tex is very hard for most of us and can only be done by the real Latex programmers with many long years of Latex programming).

Using a higher level language such as Lua or Python is much simpler as the languages are easier to program in.

Currently I use Python to generate Latex ﬁles. For example, the following page was generated from one Python script.

There are advantages to both methods. And it is left to the reader to decide which method might work for them. This note is meant to only illustrate the methods and document them.