summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/lualatex/pyluatex/example/beamer.tex
blob: 31ba69db821caf713081472097badb41052e8bd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
%% Copyright 2021-2022 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.

\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())}}

\title{PyLuaTeX Example -- BEAMER Presentation}
\author{Tobias Enderle}

\begin{document}

\maketitle

%%%%%%%%%%%%%%%%%%%%%% 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{pythonq}
msg = 'Hello slide 1'

print(msg)
\end{pythonq}
\pytypeset

Calling \pycq{print('test', end='')}\codeinline\ in Python outputs ``\outputraw''.

\begin{python}
x = 4
\end{python}
The value of \pyq{x}\codeinline\ is \outputraw.

The result of \pyq{17 + 300}$\coderaw$ is \outputraw.
\end{frame}

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

\begin{pythonq}
msg = 'Hello slide 2'

print(msg)
\end{pythonq}

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

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

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

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

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

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

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

\end{document}