Can You Execute Python Code Inside LaTeX?
At a glance
- 1. Choose a method
Use PythonTeX when you want Python code to run as part of a document workflow.
- 2. Add PythonTeX code
Put statements in a PythonTeX environment and insert results with its commands.
- 3. Run the build sequence
Compile LaTeX, run pythontex on the document, then compile LaTeX again.
- 4. Check security and support
Python code runs on your computer, so only build documents from sources you trust.
Yes—but not with LaTeX alone. TeX typesets your document; it does not natively interpret Python. To execute Python and use its results in a document, a common option is the PythonTeX package. It runs Python in a separate build step and passes the results back to LaTeX.
This is useful for calculations, generated text, and reproducible documents. If you only want to show Python code without running it, use a code-listing package instead.
A minimal PythonTeX example
Save this as example.tex:
\documentclass{article}
\usepackage{pythontex}
\begin{document}
Two plus three is \py{2 + 3}.
\begin{pycode}
x = 6
y = 7
\end{pycode}
The product of $x$ and $y$ is \py{x * y}.
\end{document}
The pycode environment runs statements that set up values. The \py{...} command evaluates a Python expression and inserts its result into the typeset text. In this example, the PDF contains “Two plus three is 5” and “The product of and is 42.”
How to build the document
Install PythonTeX and make sure Python is available. Then, from a terminal in the directory containing example.tex, run:
pdflatex example.tex
pythontex example
pdflatex example.tex
The first LaTeX run creates files PythonTeX needs. The pythontex command executes the Python code and writes results for LaTeX to use. The final LaTeX run incorporates those results into the PDF. If you change the Python code, run the sequence again.
The PythonTeX command is a separate program; it is not a command that you put inside the .tex file. Its name and availability depend on your TeX distribution and installation. If the command is not found, check that PythonTeX is installed and that its executable is on your system’s PATH.
What runs, and where?
PythonTeX runs code on the machine building the document. It can calculate values, create text, and work with Python libraries available in that environment. It is not the same as displaying a code snippet: a verbatim block or a syntax-highlighting package shows code as text but does not execute it.
This separation also affects online editors and automated build services. Some do not allow arbitrary code execution or do not provide PythonTeX, so the example may not build there. In that case, build locally or calculate the values and generate figures in a separate Python script, then include the finished text or files in the LaTeX document.
Common mistakes
- Expecting
pdflatexalone to run Python. In the usual PythonTeX workflow, you must run thepythontexcommand between LaTeX runs. - Using a Python expression where statements are needed. Put assignments, loops, and other statements in
pycode; use\py{...}for an expression whose result should appear in the text. - Assuming every build service supports it. A platform may block external execution or lack the package and its dependencies.
- Building untrusted documents. Python code can read or modify files and run other commands with the permissions of the build process. Treat a document containing executable code as you would any other program: inspect it before building it.
If you only need to include an already-created plot, image, or data file, generating it with Python first and then including the file with LaTeX may be simpler. Use PythonTeX when having the document build process execute the code and insert its results is helpful.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.