| 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 different Python sessions in a document |
| 13 |
|
| 14 |
\documentclass{article} |
| 15 |
|
| 16 |
\usepackage{pyluatex} |
| 17 |
|
| 18 |
\pysession{session-a} |
| 19 |
\pyc{greeting = "Hi, I'm session A"} |
| 20 |
\pyc{var = 'Only available in session A'} |
| 21 |
|
| 22 |
\pysession{session-b} |
| 23 |
\pyc{greeting = "Hi, I'm session B"} |
| 24 |
|
| 25 |
\begin{document} |
| 26 |
|
| 27 |
In this document, the use of sessions is demonstrated. |
| 28 |
Sessions provide a way to structure and separate code. |
| 29 |
Variables, function definitions, etc. of one session are only accessible |
| 30 |
by that session. |
| 31 |
This can be helpful if you have long documents with a lot of code. |
| 32 |
|
| 33 |
\paragraph{Session A} |
| 34 |
\pysession{session-a} |
| 35 |
The content of the variable \texttt{greeting} in session A is:\\ |
| 36 |
\py{greeting} |
| 37 |
|
| 38 |
\paragraph{Session B} |
| 39 |
\pysession{session-b} |
| 40 |
The content of the variable \texttt{greeting} in session B is:\\ |
| 41 |
\py{greeting} |
| 42 |
|
| 43 |
The variable \texttt{var} is only defined in session A, therefore |
| 44 |
an error is printed here: |
| 45 |
\begin{python} |
| 46 |
try: |
| 47 |
print(var) |
| 48 |
except NameError as e: |
| 49 |
print(e) |
| 50 |
\end{python} |
| 51 |
|
| 52 |
\end{document} |