Study on an example of integration between Python and LaTeX
Table of Contents
- Table of Contents
- Subject of this article.
- Description of the steps without using Python.
- Python file content.
- Program analysis.
- Source in LaTeX generated by Python.
- To try the program.
Subject of this article.
I am not a computer scientist but I have been using LaTeX for over ten years to write court documents. Only a few months ago I started to follow some tutorials on programming with Python.
I immediately wondered if LaTeX and Python could interact with each other.
So I asked Gemini to generate a source in Python for that purpose.
The structure of the program in Python is as follows: an array consisting of 5 columns and 10 rows of random numbers is generated, and a LaTeX language document containing the newly generated array is created.
Although the code is generated by Gemini, the observations and analysis are my own.
The study was fully conducted on GNU/Linux operating systems, mostly Arch Linux.
Description of the steps without using Python.
To achieve the same result without using a Python would first require using a spreadsheet, enter a formula for generating random numbers into a cell, extend the formula to neighboring cells, copy the generated contents, and insert the material into a previously prepared LaTeX document.
Using LibreOffice Calc you can use the command =RANDARRAY(10,5,1,100,1)
where:
RANDARRAY
creates an array of random numbers.(10,5,1,100)
provides the previous function with the parameters of ARRAYS, COLUMNS, BEGINNING NUMBER and END NUMBER.- The last number
1
represents the boolean variable TRUE which limits creation to integers. If it were0
, or FALSE decimal numbers would be generated. - The function model is, therefore, as follows:
RANDARRAY(rows, columns, min, max, integer)
.
After the creation of the data it would be necessary, then, to insert them into a previously prepared LaTeX document taking care to insert among the numeric data the symbols for the generation of tables, according to the syntax of LaTeX.
You could also use the multiple cursors as described in the following article of mine.
Python file content.
Below is the program generated by Python:
import random
import subprocess
# Generates random data
data = [[random.randint(1, 100) for _ in range(5)] for _ in range(10)]
# Creates the LaTeX template
with open('template.tex', 'w') as f:
f.write(r'''
\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{rrrrr}
\toprule
\multicolumn{5}{c}{Tabella di dati casuali} \\
\midrule
''')
# Add data into the tables
for row in data:
f.write(' & '.join(map(str, row)) + r' \\')
f.write(r'''
\bottomrule
\end{tabular}
\end{document}
''')
# Compiles le LaTeX document
subprocess.call(['pdflatex', 'template.tex'])
Program analysis.
The program in Python is very simple; the basic step is as follows:
data = [[random.randint(1, 100) for _ in range(5)] for _ in range(10)]
It is a list comprehension in which three operations are visible, identifiable by reading the string from right to left:
- The rightmost loop, “
for _ in range(10)
”, creates 10 internal lists. - The other loop, “
for _ in range(5)
,” populates each internal list with 5 elements. - The leftmost part, “
random.randint(1, 100)
,” generates a random number between 1 and 100 at each iteration.
Everything is assigned to the data
variable.
Basically, a list of lists, i.e., an array, is created with 10 outer lists and 5 random integer elements in each inner list, i.e., a 10x5 array filled with random numbers between 1 and 100.
After content generation, the document is constructed using the LaTeX language.
Below is an example of the source in LaTeX created by Python. One must, of course, consider that the numbers given in the matrix are an implementation of causality and, therefore, could be different at each compilation of the program.
Source in LaTeX generated by Python.
\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{rrrrr}
\toprule
\multicolumn{5}{c}{Tabella di dati casuali} \\
\midrule
32 & 78 & 89 & 53 & 78 \\
3 & 53 & 13 & 2 & 69 \\
27 & 52 & 80 & 81 & 69 \\
88 & 74 & 92 & 89 & 52 \\
68 & 74 & 33 & 2 & 20 \\
10 & 90 & 45 & 38 & 33 \\
79 & 90 & 20 & 37 & 48 \\
100 & 18 & 35 & 33 & 82 \\
17 & 10 & 74 & 49 & 44 \\
14 & 41 & 9 & 79 & 62 \\
\bottomrule
\end{tabular}
\end{document}
To try the program.
To test the program, the Phyton compiler must be launched.
You must use the command python
or python3
, depending on the distributions used, followed by the name of the file in which Python code is present.
A single command against the various steps described in par. 2 of this article.
Thank you for your attention.
This article was written in Org Mode.