\documentclass[a4paper]{article} \usepackage{fontspec} \setmainfont{TeX Gyre Heros} \setmonofont{TeX Gyre Cursor}[Ligatures=NoCommon] \usepackage[margin=3cm]{geometry} \usepackage{parskip} \linespread{1.1} \usepackage[listings,breakable]{tcolorbox} \tcbset{breakable,listing only,size=fbox,colframe=black!10,boxrule=3pt, colback=black!10} \NewDocumentCommand{\inlcode}{O{}v}{\texttt{#1#2}} \usepackage[colorlinks=true,urlcolor=blue,linkcolor=blue]{hyperref} \title{The \emph{pyluatex} package} \author{Tobias Enderle\\\url{https://github.com/tndrle/PyLuaTeX}} \date{v0.7.0 (2026/04/17)} \begin{document} \maketitle \raggedright \textbf{Execute Python code on the fly in your \LaTeX{} documents} PyLuaTeX allows you to execute Python code and to include the resulting output in your \LaTeX{} documents in a \textit{single compilation run}. \LaTeX{} documents must be compiled with Lua\LaTeX{} for this to work. \section{Example}\label{Example} \begin{enumerate} \item \LaTeX{} document \inlcode|example.tex| \begin{tcblisting}{}\documentclass{article} \usepackage{pyluatex} \begin{python} import math import random random.seed(0) greeting = 'Hello PyLuaTeX!' \end{python} \newcommand{\randint}[2]{\py{random.randint(#1, #2)}} \begin{document} \py{greeting} $\sqrt{371} = \py{math.sqrt(371)}$ \randint{2}{5} \end{document} \end{tcblisting} \item Compile using Lua\LaTeX{} (shell escape is required) \begin{tcblisting}{}lualatex --shell-escape example.tex \end{tcblisting} \end{enumerate} \textbf{Note:} PyLuaTeX starts Python 3 using the command \inlcode|python3| by default. If \inlcode|python3| does not start Python 3 on your system, find the correct command and extend \inlcode|\usepackage{pyluatex}| to \inlcode|\usepackage[executable=]{pyluatex}|. For example, \inlcode|\usepackage[executable=python.exe]{pyluatex}|. \textbf{Security note:} Running \LaTeX{} with the \inlcode|--shell-escape| option allows arbitrary code to be executed. For this reason, it is recommended to \textbf{compile trusted documents only}. \subsection{Further Examples}\label{Further Examples} The folder \inlcode|example| contains additional example documents: \begin{itemize} \item \inlcode|beamer.tex|\par Demonstrates the use of PyLuaTeX environments and typesetting in \textit{BEAMER} presentations. In particular, the \inlcode|fragile| option for frames is highlighted. \item \inlcode|data-visualization.tex|\par Demonstrates the visualization of data using \textit{pgfplots} and \textit{pandas} \item \inlcode|matplotlib-external.tex|\par Demonstrates how \textit{matplotlib} plots can be generated and included in a document \item \inlcode|matplotlib-pgf.tex|\par Demonstrates how \textit{matplotlib} plots can be generated and included in a document using \textit{PGF} \item \inlcode|readme-example.tex|\par The example above \item \inlcode|repl.tex|\par Demonstrates how a Python console/REPL can be run and typeset \item \inlcode|sessions.tex|\par Demonstrates the use of different Python sessions in a document \item \inlcode|typesetting-example.tex|\par The code typesetting example below \item \inlcode|typesetting-listings.tex|\par A detailed example for typesetting code and output with the \textit{listings} package \item \inlcode|typesetting-minted.tex|\par A detailed example for typesetting code and output with the \textit{minted} package \end{itemize} \section{Reference}\label{Reference} \subsection{Commands}\label{Commands} Most of the following commands accept key-value options. See \nameref{Options} for more details. \begin{itemize} \item \inlcode[\bfseries]|\py[]{}|\par Executes (object-like) \inlcode|| and writes its string representation to the document.\par \textit{Allowed options:} \inlcode|ignoreerrors|, \inlcode|quiet|, \inlcode|repl|, \inlcode|session|, \inlcode|store|, \inlcode|verbose|\par \textit{Example:} \inlcode|\py{3 + 7}| \item \inlcode[\bfseries]|\pyc[]{}|\par Executes \inlcode||. Output (e.g. from a call to \inlcode|print()|) is written to the document.\par \textit{Allowed options:} \inlcode|ignoreerrors|, \inlcode|quiet|, \inlcode|repl|, \inlcode|session|, \inlcode|store|, \inlcode|verbose|\par \textit{Examples:} \inlcode|\pyc{x = 5}|, \inlcode|\pyc{print('hello')}| \item \inlcode[\bfseries]|\pyfile[]{}|\par Executes the Python file specified by \inlcode||. Output (e.g. from a call to \inlcode|print()|) is written to the document.\par \textit{Allowed options:} \inlcode|ignoreerrors|, \inlcode|quiet|, \inlcode|repl|, \inlcode|session|, \inlcode|store|, \inlcode|verbose|\par \textit{Example:} \inlcode|\pyfile{main.py}| \item \inlcode[\bfseries]|\pyif[]{}{}{}|\par Evaluates the Python boolean expression \inlcode||, and then executes either the \LaTeX{} code in \inlcode|| or the \LaTeX{} code in \inlcode||.\par \textit{Allowed options:} \inlcode|session|, \inlcode|verbose|\par \textit{Example:} \inlcode|\pyif{a == 1}{$a = 1$}{$a \neq 1$}| \item \inlcode[\bfseries]|\pyoptions{}|\par Sets options globally. For more information see the \nameref{Options} section.\par \textit{Allowed options:} \inlcode|ignoreerrors|, \inlcode|quiet|, \inlcode|repl|, \inlcode|session|, \inlcode|store|, \inlcode|verbose|\par \textit{Example:} \inlcode|\pyoptions{verbose,session=main}| \end{itemize} The following commands exist as shortcuts and for backward compatibility with previous versions of PyLuaTeX: \begin{itemize} \item \inlcode[\bfseries]|\pyq[]{}|\par Executes (object-like) \inlcode||. Any output to the document is suppressed.\par This is equivalent to \inlcode|\py[quiet,]{}|.\par \textit{Allowed options:} \inlcode|ignoreerrors|, \inlcode|repl|, \inlcode|session|, \inlcode|store|, \inlcode|verbose|\par \textit{Example:} \inlcode|\pyq{3 + 7}| \item \inlcode[\bfseries]|\pycq[]{}|\par Executes \inlcode||. Any output to the document is suppressed.\par This is equivalent to \inlcode|\pyc[quiet,]{}|.\par \textit{Allowed options:} \inlcode|ignoreerrors|, \inlcode|repl|, \inlcode|session|, \inlcode|store|, \inlcode|verbose|\par \textit{Example:} \inlcode|\pycq{x = 5}| \item \inlcode[\bfseries]|\pyfileq[]{}|\par Executes the Python file specified by \inlcode||. Any output to the document is suppressed.\par This is equivalent to \inlcode|\pyfile[quiet,]{}|.\par \textit{Allowed options:} \inlcode|ignoreerrors|, \inlcode|repl|, \inlcode|session|, \inlcode|store|, \inlcode|verbose|\par \textit{Example:} \inlcode|\pyfileq{main.py}| \item \inlcode[\bfseries]|\pysession{}|\par Sets \inlcode|| globally as Python session for subsequent Python code. The session that is active at the beginning is \inlcode|default|.\par This is equivalent to \inlcode|\pyoptions{session=}|.\par \textit{Example:} \inlcode|\pysession{main}| \item \inlcode[\bfseries]|\pyoption{