%% 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
% Demonstrates the use of PyLuaTeX environments and typesetting in *BEAMER*
% presentations. In particular, the `fragile` option for frames is highlighted.

\documentclass[t]{beamer}

\usepackage{pyluatex}
\usepackage{listings}
\usepackage{xcolor}
\lstset{
  language=Python,
  breaklines=true,
  framesep=1ex,
  frame=lrtb,
  framerule=0pt,
  numbers=none,
  basicstyle=\ttfamily,
  keywordstyle=\bfseries\color{green!40!black},
  stringstyle=\bfseries\color{red!80!black},
  identifierstyle=\color{blue},
  backgroundcolor=\color{gray!10!white},
}

\usepackage{luacode}
\begin{luacode}
function pytypeset()
  tex.print("\\begin{lstlisting}[language=Python]")
  tex.print(pyluatex.get_last_code())
  tex.print("\\end{lstlisting}")
  tex.print("") -- ensure newline
end

function pytypeset_inline()
  -- assume there is only one line of code in get_last_code()
  tex.print("\\lstinline[columns=fixed]@" .. pyluatex.get_last_code()[1] .. "@")
end
\end{luacode}

\newcommand*{\pytypeset}{%
  \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}

%%%%%%%%%%%%%%%%%%%%%% frame

\begin{frame}{Important}
Using PyLuaTeX environments (\texttt{python}, \texttt{pythonq}, or
\texttt{pythonrepl}) inside BEAMER frames requires the \texttt{fragile} option
for those frames.

\medskip
If you don't use overlays in a frame, i.e. the frame contains only a single slide,
you can use the \texttt{fragile=singleslide} option.
\end{frame}

%%%%%%%%%%%%%%%%%%%%%% frame

\begin{frame}[fragile=singleslide]{Python environment inside frame}
\begin{python}[q]
msg = 'Hello slide 1'

print(msg)
\end{python}
\pytypeset

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.
\end{frame}

%%%%%%%%%%%%%%%%%%%%%% frame

\begin{python}[q]
msg = 'Hello slide 2'

print(msg)
\end{python}

\begin{frame}{Python environment outside frame}
\pytypeset

The \texttt{fragile} option is not required in this frame because
the \texttt{python} environment is outside the frame.
Only the command \texttt{\textbackslash pytypeset} for typesetting the code and output is inside
the frame.

\medskip
Inline Python still works: The result of \py[q]{17 + 300}$\coderaw$ is \outputraw
\end{frame}

%%%%%%%%%%%%%%%%%%%%%% frame

\begin{frame}[fragile]{Overlays}
\begin{python}[q]
msg = 'Custom environment'

print(msg)
\end{python}
\pytypeset
\pause

In this frame, \texttt{fragile} is required instead of \texttt{fragile=singleslide},
because we use overlays.
\end{frame}

\end{document}
