%% Copyright 2021-2026 Tobias Enderle %% %% This work may be distributed and/or modified under the %% conditions of the LaTeX Project Public License, either version 1.3c %% of this license or (at your option) any later version. %% The latest version of this license is in %% http://www.latex-project.org/lppl.txt %% and version 1.3c or later is part of all distributions of LaTeX %% version 2005/12/01 or later. % DESCRIPTION % A detailed example for typesetting code and output with the *minted* package \documentclass{article} \usepackage{pyluatex} \usepackage{minted} \usepackage{luacode} \begin{luacode} function pytypeset() tex.print("\\begin{minted}[bgcolor=gray!10!white]{python}") tex.print(pyluatex.get_last_code()) tex.print("\\end{minted}") tex.print("") -- ensure newline end function pytypeset_inline() -- assume there is only one line of code in get_last_code() tex.print("\\mintinline{python}@" .. pyluatex.get_last_code()[1] .. "@") end \end{luacode} \newcommand*{\pytypeset}{% \noindent\textbf{Input:} \directlua{pytypeset()} \textbf{Output:} \begin{center} \directlua{tex.print(pyluatex.get_last_output())} \end{center} } \newcommand*{\coderaw}{\directlua{tex.print(pyluatex.get_last_code())}} \newcommand*{\codeinline}{\directlua{pytypeset_inline()}} \newcommand*{\outputraw}{\directlua{tex.print(pyluatex.get_last_output())}} \begin{document} \section*{Typesetting of Code Blocks and Output} \begin{python}[q] msg = 'Hello' print(msg) \end{python} \pytypeset \section*{Typesetting of Inline Code and Output} Calling \pyc[q]{print('test', end='')}\codeinline\ in Python outputs ``\outputraw''. \begin{python} x = 4 \end{python} The value of \py[q]{x}\codeinline\ is \outputraw. The result of \py[q]{17 + 300}$\coderaw$ is \outputraw. \section*{Custom Environments} You can create a custom environment for typesetting: \NewDocumentEnvironment{typesetpython}{} {\PyLTVerbatimEnv\begin{python}[q]} {\end{python}\pytypeset} \begin{typesetpython} msg = 'Custom environment' print(msg) \end{typesetpython} \end{document}