| 1 |
%% Copyright 2021-2026 Tobias Enderle |
| 2 |
%% |
| 3 |
%% This work may be distributed and/or modified under the |
| 4 |
%% conditions of the LaTeX Project Public License, either version 1.3c |
| 5 |
%% of this license or (at your option) any later version. |
| 6 |
%% The latest version of this license is in |
| 7 |
%% http://www.latex-project.org/lppl.txt |
| 8 |
%% and version 1.3c or later is part of all distributions of LaTeX |
| 9 |
%% version 2005/12/01 or later. |
| 10 |
|
| 11 |
% DESCRIPTION |
| 12 |
% Demonstrates the use of PyLuaTeX environments and typesetting in *BEAMER* |
| 13 |
% presentations. In particular, the `fragile` option for frames is highlighted. |
| 14 |
|
| 15 |
\documentclass[t]{beamer} |
| 16 |
|
| 17 |
\usepackage{pyluatex} |
| 18 |
\usepackage{listings} |
| 19 |
\usepackage{xcolor} |
| 20 |
\lstset{ |
| 21 |
language=Python, |
| 22 |
breaklines=true, |
| 23 |
framesep=1ex, |
| 24 |
frame=lrtb, |
| 25 |
framerule=0pt, |
| 26 |
numbers=none, |
| 27 |
basicstyle=\ttfamily, |
| 28 |
keywordstyle=\bfseries\color{green!40!black}, |
| 29 |
stringstyle=\bfseries\color{red!80!black}, |
| 30 |
identifierstyle=\color{blue}, |
| 31 |
backgroundcolor=\color{gray!10!white}, |
| 32 |
} |
| 33 |
|
| 34 |
\usepackage{luacode} |
| 35 |
\begin{luacode} |
| 36 |
function pytypeset() |
| 37 |
tex.print("\\begin{lstlisting}[language=Python]") |
| 38 |
tex.print(pyluatex.get_last_code()) |
| 39 |
tex.print("\\end{lstlisting}") |
| 40 |
tex.print("") -- ensure newline |
| 41 |
end |
| 42 |
|
| 43 |
function pytypeset_inline() |
| 44 |
-- assume there is only one line of code in get_last_code() |
| 45 |
tex.print("\\lstinline[columns=fixed]@" .. pyluatex.get_last_code()[1] .. "@") |
| 46 |
end |
| 47 |
\end{luacode} |
| 48 |
|
| 49 |
\newcommand*{\pytypeset}{% |
| 50 |
\textbf{Input:} |
| 51 |
\directlua{pytypeset()} |
| 52 |
\textbf{Output:} |
| 53 |
\begin{center} |
| 54 |
\directlua{tex.print(pyluatex.get_last_output())} |
| 55 |
\end{center} |
| 56 |
} |
| 57 |
\newcommand*{\coderaw}{\directlua{tex.print(pyluatex.get_last_code())}} |
| 58 |
\newcommand*{\codeinline}{\directlua{pytypeset_inline()}} |
| 59 |
\newcommand*{\outputraw}{\directlua{tex.print(pyluatex.get_last_output())}} |
| 60 |
|
| 61 |
\begin{document} |
| 62 |
|
| 63 |
%%%%%%%%%%%%%%%%%%%%%% frame |
| 64 |
|
| 65 |
\begin{frame}{Important} |
| 66 |
Using PyLuaTeX environments (\texttt{python}, \texttt{pythonq}, or |
| 67 |
\texttt{pythonrepl}) inside BEAMER frames requires the \texttt{fragile} option |
| 68 |
for those frames. |
| 69 |
|
| 70 |
\medskip |
| 71 |
If you don't use overlays in a frame, i.e. the frame contains only a single slide, |
| 72 |
you can use the \texttt{fragile=singleslide} option. |
| 73 |
\end{frame} |
| 74 |
|
| 75 |
%%%%%%%%%%%%%%%%%%%%%% frame |
| 76 |
|
| 77 |
\begin{frame}[fragile=singleslide]{Python environment inside frame} |
| 78 |
\begin{python}[q] |
| 79 |
msg = 'Hello slide 1' |
| 80 |
|
| 81 |
print(msg) |
| 82 |
\end{python} |
| 83 |
\pytypeset |
| 84 |
|
| 85 |
Calling \pyc[q]{print('test', end='')}\codeinline\ in Python outputs ``\outputraw''. |
| 86 |
|
| 87 |
\begin{python} |
| 88 |
x = 4 |
| 89 |
\end{python} |
| 90 |
The value of \py[q]{x}\codeinline\ is \outputraw. |
| 91 |
|
| 92 |
The result of \py[q]{17 + 300}$\coderaw$ is \outputraw. |
| 93 |
\end{frame} |
| 94 |
|
| 95 |
%%%%%%%%%%%%%%%%%%%%%% frame |
| 96 |
|
| 97 |
\begin{python}[q] |
| 98 |
msg = 'Hello slide 2' |
| 99 |
|
| 100 |
print(msg) |
| 101 |
\end{python} |
| 102 |
|
| 103 |
\begin{frame}{Python environment outside frame} |
| 104 |
\pytypeset |
| 105 |
|
| 106 |
The \texttt{fragile} option is not required in this frame because |
| 107 |
the \texttt{python} environment is outside the frame. |
| 108 |
Only the command \texttt{\textbackslash pytypeset} for typesetting the code and output is inside |
| 109 |
the frame. |
| 110 |
|
| 111 |
\medskip |
| 112 |
Inline Python still works: The result of \py[q]{17 + 300}$\coderaw$ is \outputraw |
| 113 |
\end{frame} |
| 114 |
|
| 115 |
%%%%%%%%%%%%%%%%%%%%%% frame |
| 116 |
|
| 117 |
\begin{frame}[fragile]{Overlays} |
| 118 |
\begin{python}[q] |
| 119 |
msg = 'Custom environment' |
| 120 |
|
| 121 |
print(msg) |
| 122 |
\end{python} |
| 123 |
\pytypeset |
| 124 |
\pause |
| 125 |
|
| 126 |
In this frame, \texttt{fragile} is required instead of \texttt{fragile=singleslide}, |
| 127 |
because we use overlays. |
| 128 |
\end{frame} |
| 129 |
|
| 130 |
\end{document} |