summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/pythonimmediate/pythonimmediate.tex
blob: 539a770fc2991988a0be094c7fa3165d05a4592e (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
\ProvidesFile{pythonimmediate.tex}[2023/03/07 0.4.0 Library to run Python code]
\RequirePackage{fvextra}
\documentclass{l3doc}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{arrows.meta}
\EnableCrossrefs
\CodelineIndex
\fvset{breaklines=true,gobble=0,tabsize=4,frame=single,numbers=left,numbersep=3pt}

\AtBeginDocument{\DeleteShortVerb\"}  % https://tex.stackexchange.com/a/650966/250119
\MakeOuterQuote{"}

\newcommand\reg{régime}
\newcommand\DescribePython{\DescribeMacro}  % hack


\begin{document}


% hack to make quotes inside |...| straight
\edef\temp{\def\noexpand|{\noexpand\Verb\string|}}\temp
% basically execute \def| { \Verb ⟨catcode other |⟩ }

\GetFileInfo{\jobname.tex}

\title{\pkg{\jobname} --- \fileinfo
\thanks{This file describes version \fileversion, last revised \filedate.}
}
\author{user202729}
\date{Released \filedate}

\maketitle

\section{Motivation}

Just like \pkg{Perl\TeX} or \pkg{PyLua\TeX} (and unlike \pkg{Python\TeX} or \pkg{lt3luabridge}),
this only requires a single run, and variables are persistent throughout the run.

Unlike \pkg{Perl\TeX} or \pkg{PyLua\TeX}, there's no restriction on compiler or script required to run the code.

There's also debugging functionalities -- \TeX\ errors results in Python traceback, and Python error results in \TeX\ traceback.
Errors in code executed with the |pycode| environment gives the correct traceback point to the Python line of code in the \TeX\ file.

For advanced users, this package allows the user to manipulate the \TeX\ state directly from within Python,
so you don't need to write a single line of \TeX\ code.

\section{Installation}

%You need to install Python \pkg{daemoniker} and \pkg{psutil} package.

In addition to the \LaTeX\ package, you need the Python \pkg{pythonimmediate-tex} package,
installation instruction can be found at \url{https://pypi.org/project/pythonimmediate-tex/}.

Note that not all \TeX\ package versions are compatible with all Python package versions.
This \TeX\ package is compatible with Python package version |0.4.0|.

Remember to enable unrestricted\footnote{
	There's little point in supporting restricted mode, since it's possible to execute arbitrary shell commands in Python anyway.
	If it's needed to execute untrusted \TeX\ code, separate sandboxing should be used.
} shell-escape. (there's a guide on \TeX.SE if necessary: \url{https://tex.stackexchange.com/q/598818/250119})

\subsection{Installation on Overleaf}\label{installation-on-overleaf}

At the point of writing, this package can be used on Overleaf.

Nevertheless, you cannot use |pip| to install Python packages on Overleaf directly, instead it's possible to
download |.zip| file, include it in your Overleaf project, and specify where the package can be found to Python
using |PYTHONPATH| environment variable.

Instruction:

\begin{itemize}
	\item Download the following files and place it in the root folder of Overleaf:
		\begin{itemize}
			\item \file{saveenv.sty}
			\item \file{precattl.sty}
			\item \file{pythonimmediate.sty}
			\item \file{pythonimmediate_helper.lua}
			\item \file{pythonimmediate-tex-0.4.0.zip}
		\end{itemize}
		The |0.4.0| part should be replaced with the desired version
		of the Python package.

		The |.sty| files can be downloaded from CTAN at 
		\url{https://ctan.org/pkg/saveenv},
		\url{https://ctan.org/pkg/precattl},
		\url{https://ctan.org/pkg/pythonimmediate}
		respectively.

		The |.zip| file containing Python source code
		can be downloaded from PyPI: \url{https://pypi.org/project/pythonimmediate-tex/\#files}.

	\item Write the following in the preamble:
\begin{verbatim}
\usepackage[abspath]{currfile}
\usepackage[python-executable={PYTHONPATH=pythonimmediate-tex-0.4.0.zip/pythonimmediate-tex-0.4.0/ python3},args={--mode=unnamed-pipe}]{pythonimmediate}
\end{verbatim}
As above, replace both occurrences of |0.4.0| with the downloaded version specified in the zip file above.

Refer to \ref{troubleshoot-source-file-not-found} for explanation of the |abspath| option.
\end{itemize}

For some unknown reason in the default mode on Overleaf (|\nonstopmode|), when there's an error
the log file might be truncated,
so in that case consider writing |\errorstopmode|.

Refer to \ref{troubleshoot-Python-error} to read the error traceback in case of Python error.

Some construct inside the |pycode| block might make the code editor on Overleaf report an error,
even though the code is valid. Refer to \url{https://www.overleaf.com/learn/how-to/Code_Check#Code_Check_Limitations}.

\section{Usage}

\subsection{Package options}

Normally no options are required.
If you're not sure what they do, just use the default options.

\DescribeOption{args=}
Arguments to be passed to the Python component of the program. Run
\begin{verbatim}
python -m pythonimmediate.pytotex --help
\end{verbatim}
on the command-line to view the available options.

The documentation is also available at
\url{https://pythonimmediate.readthedocs.io/en/latest/pythonimmediate.html#module-pythonimmediate.pytotex}.

\DescribeOption{python-executable=}
The name/path to the Python executable. Default to |python3|.

Can also be used to provide environment variables to the Python process.
An example how to do that is explained in \ref{installation-on-overleaf}.

\DescribeOption{python-flags=}
Flags to be passed to the Python interpreter. For example pass |-O| to disable assertions.

The documentations can be found by running |python --help| on the command-line,
or visit \url{https://docs.python.org/3/using/cmdline.html}.

\subsection{\TeX\ interface}

The interface mimics those in popular packages such as \pkg{Python\TeX} or \pkg{PyLua\TeX}.

\subsubsection{Inline commands}

\DescribeMacro{\py}\label{py-command}
Evaluate some Python expression, consider the result as a string, then execute the result as \TeX\ command.

\begin{texnote}
	The command is not expandable, and the argument will be fully expanded with the active |~| set to |\relax|,
	|\set@display@protect| executed and |\escapechar=-1|, then the result
	passed to Python as a string.
\end{texnote}

Which, for the users who are not familiar with \TeX\ terminology, roughly means the following:
\begin{itemize}
	\item the value can only be used to typeset text,
		it must not be used to pass "values" to other \LaTeX\ commands.

		The following is legal:
\begin{verbatim}
The value of $1+1$ is $\py{1+1}$.
\end{verbatim}
The following is illegal, as the result (2) can only be used to typeset text, not passed to another command that expect a "value":
\begin{verbatim}
\setcounter{abc}{\py{1+1}} 
\end{verbatim}
A possible workaround is:
\begin{verbatim}
\py{ r'\\setcounter{abc}{' + str(1+1) + '}' }
\end{verbatim}
In this example it works without escaping the |{}| characters, but if the Python code has those unbalanced then you can escape them as mentioned below.

	\item Special characters can be "escaped" simply by prefixing the character with backslash.

		For example
\begin{verbatim}
\pyc{assert len('\ \ ')==2}
\pyc{assert ord('\\\\')==0x5c}
\pyc{assert ord('\%')  ==0x25}
\end{verbatim}
In the examples above, Python "sees" (i.e. the Python code being executed is)
\begin{verbatim}
assert len('  ')==2
assert ord('\\')==0x5c
assert ord('%') ==0x25
\end{verbatim}
respectively.

	\item Macros will be expanded.
\begin{verbatim}
\def\mycode{1+1}
The value of $1+1$ is $\py{\mycode}$.
\end{verbatim}
\end{itemize}


\DescribeMacro{\pyc}
Execute some Python code provided as an argument (the argument will be interpreted as described above).

The command is not expandable -- roughly speaking, you can only use this at "top level".

Any output (as described in \ref{print-to-TeX}) will be typesetted.

The difference between |\py| and |\pyc| is that the argument of |\py| should be a Python expression
(suitable for passing into |eval()| Python function) while the argument of |\pyc| should be a Python
statement (suitable for passing into |exec()| Python function).

Therefore,
\begin{itemize}
	\item |\py{1+1}| will typeset 2.
	\item |\pyc{1+1}| is valid, but will do nothing just like |exec("1+1")|.
	\item |\py{x=1}| is invalid.
	\item |\pyc{x=1}| is valid and assigns the variable |x| to be 1.
\end{itemize}

\DescribeMacro{\pycq}
Same as above, but output (\ref{print-to-TeX}) will not be typesetted.

\DescribeMacro{\pyfile}
Given an argument being the file name, execute that file.

\DescribeMacro{\pys}
Performs "string interpolation", the same way as \pkg{Python\TeX}.
(not yet implemented)

\subsubsection{Environments}

\DescribeEnv{pycode}
Verbatim-like environment that executes the code inside as Python.

Example usage: The following will typeset |123|
\begin{verbatim}
\begin{pycode}
pythonimmediate.print("123")
\end{pycode}
\end{verbatim}

Special note: white spaces at the end of lines are preserved.

Any output (as described in \ref{print-to-TeX}) will be typesetted.

\DescribeEnv{pycodeq}
Same as above, but output will not be typesetted.

\DescribeEnv{pysub}
Not yet implemented.

\subsection{Python interface}

The \TeX\ interface is only used to call Python. Apart from that, all the work can be done on the Python side.

All functions in this section should be imported from |pythonimmediate| package, unless specified otherwise.

Currently, all the documentations are moved to the Python package documentation,
see \url{https://pythonimmediate.readthedocs.io/}.

Documentation of a few functions are still kept here for convenience, but \textbf{they might be outdated}.
Always refer to the online documentation.

\subsubsection{Print to \TeX}\label{print-to-TeX}

\DescribePython{.print_TeX()}
\DescribePython{.file}
These functions are used in |\pyc| command or |pycode| environment.

Unlike most other packages, using |print()| function in Python will print to the console (\TeX\ standard output).
In order to print \TeX\ code to be executed, you can do one of
\begin{verbatim}
pythonimmediate.print_TeX(...)
print(..., file=pythonimmediate.file)
with contextlib.redirect_stdout(pythonimmediate.file):
	print(...)
\end{verbatim}
Note that in quiet environments, |pythonimmediate.file| is None, the second variant using |print()| will print to stdout
instead of suppress output. The third variant works as expected.

All output will be buffered until the whole Python code finishes executing.
In order to typeset the text immediately use one of the advanced commands.



\DescribePython{.newcommand()}
\DescribePython{.renewcommand()}
Same as \LaTeX's |\newcommand| and |\renewcommand|. Can be used as follows:

\begin{verbatim}
from pythonimmediate import newcommand, renewcommand

@newcommand
def function():
	...
# define |\function| in TeX

@newcommand("controlsequencename")
def function():
	...
# define |\controlsequencename| in TeX

def function():
	...
newcommand("controlsequencename", function)
\end{verbatim}

\DescribePython{.get_arg_str()}
\DescribePython{.get_optional_arg_str()}
\DescribePython{.get_verb_arg()}
\DescribePython{.get_multiline_verb_arg()}
\DescribePython{.peek_next_char()}
\DescribePython{.get_next_char()}
There are those functions that is mostly understandable to an inexperienced \LaTeX\ user,
and should be sufficient for a lot of programming works.

This is an example of how the functions could be used. The name should be mostly self-explanatory.

\begin{verbatim}
\documentclass{article}
\usepackage{pythonimmediate}
\begin{document}
\begin{pycode}
from pythonimmediate import newcommand, peek_next_char, get_next_char, get_arg_str
from pythonimmediate import print_TeX as print
@newcommand
def innerproduct():
	s = get_arg_str()    # in the example below this will have the value '\mathbf{a},\mathbf{b}'
	x, y = s.split(",")  # it's just a Python string, manipulate normally (but be careful of comma inside braces, parse the string yourself)
	print(r"\left\langle" + x + r"\middle|" + y + r"\right\rangle")

@newcommand
def fx():
	if peek_next_char() == "_":
		get_next_char()
		subscript = get_arg_str()
		print("f_{" + subscript + "}(x)")
	else:
		print("f(x)")

@newcommand
def sumManyArgs():
	s = 0
	while peek_next_char() == "{":
		i = get_arg_str()
		s += int(i)
	print(str(s))
\end{pycode}
$1+2+3 = \sumManyArgs{1}{2}{3}$

$\innerproduct{\mathbf{a},\mathbf{b}}=1$

$\fx = 1$, $\fx_i = 2$, $\fx_{ij} = 3$
\end{document}
\end{verbatim}

It will typeset:

\begin{quote}
$1+2+3=6$

$\left\langle\mathbf{a}\middle\vert\mathbf{b}\right\rangle=1$

$f(x)=1$, $f_i(x)=2$, $f_{ij}(x)=3$
\end{quote}

\DescribePython{.get_arg_estr()}
\DescribePython{.get_optional_arg_estr()}
Similar to some functions above, except that the argument is fully expanded and "escapes" of common characters are handled correctly,
similar to how |\py| command (\ref{py-command}) reads its arguments.

\DescribePython{.execute()}
Takes a string and execute it immediately. (so that any |.execute()| will be executed before any |.print_TeX()|)

Assuming \TeX\ is in errorstopmode (i.e. errors halt \TeX\ execution),
any error in \TeX\ will create an error in Python and the traceback should point to the correct line of code.

For example, in the following code

\begin{verbatim}
\documentclass{article}
\usepackage{tikz}
\usepackage{pythonimmediate}
\begin{document}

\begin{tikzpicture}
\begin{pycode}
from pythonimmediate import execute
execute(r'\draw (0, 0) to (1, 1);')
execute(r'\draw (2, 2) to (p);')
execute(r'\draw (3, 3) to (4, 4);')
\end{pycode}
\end{tikzpicture}

\end{document}
\end{verbatim}
each |\draw| command will be executed immediately when the Python |.execute()| function is executed,
and as the second line throws an error, the Python traceback will point to that line.

\section{Troubleshooting}

\subsection{"Source file not found!" error message}\label{troubleshoot-source-file-not-found}

In order to obtain the exact code with trailing spaces and produce error traceback
point to the correct \TeX\ file, the Python code need to know the full path to the current
\TeX\ file for the |pycode| environment.

Nevertheless, this is difficult and does not always work
(refer to the documentation of \pkg{currfile} for details), so this message is issued
when the file cannot be found.

In that case try the following fixes:

\begin{itemize}
	\item Include |\usepackage[abspath]{currfile}| at the start of the document, after the |\documentclass| line.
		(this option is not included by default because it's easy to get package clash, and usually \pkg{currfile} without
		the |abspath| option works fine -- unless custom |jobname| is used)
	\item Explicitly override |currfilename| or |currfileabspath| -- for example
\begin{verbatim}
\def\currfilename{main.tex}
\end{verbatim}
		Technically this is an abuse of the \pkg{currfile} package API, but it usually works regardless.
\end{itemize}

\subsection{"Python error" error message}\label{troubleshoot-Python-error}

In case of Python error, the Python traceback is included in the terminal and \TeX\ log file.

Search for "Python error traceback" before the error line in the log file.

On Overleaf, you can either view the log file ("Raw logs" section)
or the traceback on stderr (download \file{output.stderr} file)

\subsection{"\TeX\ error" error message}\label{troubleshoot-TeX-error}

If an error occur in \TeX, traceback cannot be included in the log file.

Besides, this can only be detected in |\errorstopmode|. Such an error will always halt \TeX,
and Python will be force-exited after printing the error traceback.

On Overleaf, download \file{output.stderr} file to read the traceback.

\section{Implementation note}

Communication between \TeX\ and Python are done by opening two pseudo-files from the output of a Python process |textopy|
(similar to |\ior_shell_open:Nn|)
and to the input of another Python process |pytotex| (this would be |\iow_shell_open:Nn|, if \LaTeX3 have such a function).

There are various methods for the 2 Python child processes to communicate with each other.
After some initial bootstrapping to setup the communication, we can consider only the |textopy| script, the other
merely serves as the bridge to send input to \TeX.

The communication protocol is a little complicated, since it must support nesting bidirectional execution of \TeX\ and Python.

Besides, I believe it's not possible to make a "background listener" on the \TeX\ side, so it must keep track of whether a command should be read from Python and executed.

Currently, exception handling (throwing a Python exception in a nested Python function, catch it in the outer Python function) is not supported.

These are some examples of what could happen in the communication protocol.

\ExplSyntaxOn
\let\fpEval\fp_eval:n
\ExplSyntaxOff

\begingroup

\tikzset{myarrow/.style={-{Latex[length=2mm]}}}

\def\blockWidth{1}
\def\blockHeight{7}
\def\separation{10.5}
\def\step{0.7}

\def\left #1{
	\draw [myarrow] (p) ++(\separation, 0) -- node [above] {\small #1} ++(-\separation, 0);
	\path (p) ++ (0, -\step) coordinate (p);
}
\def\right #1{
	\draw [myarrow] (p) -- node [above] {\small #1} ++(\separation, 0);
	\path (p) ++ (0, -\step) coordinate (p);
}

\def\initDrawing{
	\path (\blockWidth, \fpEval{-\step}) coordinate (p);
}

\newdimen \tempx
\newdimen \tempy

\ExplSyntaxOn
\let \dimToDecimalInUnit \dim_to_decimal_in_unit:nn 
\ExplSyntaxOff

\def\drawBlocks{
    \path [transform canvas] (p);
	\pgfgetlastxy{\tempx}{\tempy}
	\edef\blockHeight{ \fpEval{ - \dimToDecimalInUnit{\tempy}{1cm}  } }

	\draw (0, \fpEval{-\blockHeight}) rectangle node [rotate=90] {\TeX} (\blockWidth, 0);
	\draw (\fpEval{\blockWidth+\separation}, \fpEval{-\blockHeight}) rectangle node [rotate=270] {Python} (\fpEval{\separation+2*\blockWidth}, 0);
}

\vspace{5pt}
\begin{tikzpicture}
\edef\blockHeight{\fpEval{\step*3}}
\initDrawing
\right{execute Python code: print(1)}
\left{execute \TeX\ code: 1}
\drawBlocks
\end{tikzpicture}

Nevertheless, there may be more complicated cases where the Python code itself may call \TeX\ code before actually returns:

\vspace{5pt}
\begin{tikzpicture}
\edef\blockHeight{\fpEval{\step*5}}
\initDrawing
\right{execute Python code: print(var(a)*2)}
\left{execute \TeX\ code: sendtopy(a); execute another command}
\right{123}
\left{execute \TeX\ code: 123123}
\drawBlocks
\end{tikzpicture}

Or:

\vspace{5pt}
\begin{tikzpicture}
\edef\blockHeight{\fpEval{\step*7}}
\initDrawing
\right{execute Python code: tex.exec(a=456); print(var(a)*2)}
\left{execute \TeX\ code: a=456; sendtopy(done); execute another command}
\right{done}
\left{execute \TeX\ code: sendtopy(a); execute another command}
\right{456}
\left{456456}
\drawBlocks
\end{tikzpicture}

\endgroup

The Python side must not just listen for "done" command back, but must potentially call a nested loop.

The exact protocol is:
\begin{itemize}
	\item "execute Python code" sends from \TeX\ to Python has a single line "|i|\meta{handler name}",
		followed by any number of arguments (depends on the handler).

		Refer to the |define_TeX_call_Python| internal function for details.

	\item "done" sends from \TeX\ to Python has the format "|r|\meta{optional return value as a string in a single line}".

		This is sent by executing \TeX\ command |\pythonimmediatecontinue|, which takes a single argument to be e-expanded using |\write|
		as the "return value".

	\item "execute \TeX\ code" sends from Python to \TeX\ must only be sent when the \TeX\ side listens for a command.
		It consist of a single line specify the "command name", which \TeX\ will
		execute the command named
		|\__run_|\meta{command name}|:|
		which must already be defined on the \TeX\ side.

		The command itself might contain additional code to execute more code, e.g. by reading more lines from Python.

		Refer to the |define_Python_call_TeX| internal function for details.
\end{itemize}



\PrintIndex

\end{document}