Python & LaTeX

Write and run Python code directly in your LaTeX projects.

BlaBLaTeX includes a Python execution environment directly in the editor. A .py file can be created alongside your LaTeX files, run directly from the project, and used to perform calculations, process data, or generate plots and figures. Files generated by the script are added to the project and can then be used in your LaTeX document.

Calculate

Perform numerical or scientific calculations directly from your project.

Plot

Use Python to generate plots and figures that can be included in your document.

Process data

Read files from your project, process data, and generate new results.

Usage

A Python file in your LaTeX project

Python files are stored in the project alongside your .tex files, images, and data.

When you open a .py file, the editor switches to Python mode and a Run button lets you execute the script directly.

File tree
main.tex
figure.py
data.csv
figure.py
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-2, 2, 200)
y = x ** 2

plt.plot(x, y)
plt.savefig("plot.pdf")
Script completed successfully.
Python + LaTeX

From a Python script to a LaTeX document

Python can generate files that remain in the project: figures, CSV data, or other outputs. They can then be used directly in your LaTeX document.

Python script figure.py
Execution Run
Output plot.pdf
LaTeX document main.tex
Results

Use files generated by Python

Files created by a script automatically appear in the project file tree. A figure generated with Matplotlib can therefore be saved and then included in the LaTeX document.

The same approach can be used to generate or transform data and then reuse it in the document or in another script.

Project files

main.tex
figure.py
data.csv
plot.pdf
\includegraphics{plot.pdf}
Example

Calculate, plot, and include the result

A Python script can, for example, calculate the values of a function, plot its curve, and automatically save the figure used in a document.

figure.py Python
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-2, 2, 200)
y = x ** 2

plt.plot(x, y)
plt.savefig("plot.pdf")
Generated result
Curve of y = x² Parabola corresponding to the Python code shown on the left. −2.0 −1.5 −1.0 −0.5 0.0 0.5 1.0 1.5 2.0 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0
Environment

Python directly in your browser

Python execution is integrated into the BlaBLaTeX editor and is powered by Pyodide . Scripts run directly in your browser using your computer's resources.

This means you do not need to install Python on the computer you are using. The available libraries are those supported by the Pyodide environment . For very large or computationally demanding tasks, a local Python environment remains more suitable.