summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/pyluatex/example/sessions.tex
blob: 49f49c4ddb05c6bba26d163eda4e0c48d9bff17e (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
%% Copyright 2021-2023 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{article}

\usepackage{pyluatex}

\title{PyLuaTeX Example -- Sessions}
\author{Tobias Enderle}

\pysession{session-a}
\pyc{greeting = "Hi, I'm session A"}
\pyc{var = 'Only available in session A'}

\pysession{session-b}
\pyc{greeting = "Hi, I'm session B"}

\begin{document}

\maketitle

In this document the use of sessions is demonstrated.
Sessions provide a way to structure and separate code.
Variables, function definitions, etc. of one session are only accessible
by that very session.
This can be helpful if you have long documents with a lot of code.

\paragraph{Session A}
\pysession{session-a}
The content of the variable \texttt{greeting} in session A is:\\
\py{greeting}

\paragraph{Session B}
\pysession{session-b}
The content of the variable \texttt{greeting} in session B is:\\
\py{greeting}

The variable \texttt{var} is only defined in session A, therefore
an error is printed here:
\begin{python}
try:
    print(var)
except NameError as e:
    print(e)
\end{python}

\end{document}