pythonimmediate.texcmds module
This module defines the \(\TeX\) commands and environments that will be available globally when the module is loaded.
The way the argument is absorbed is shown in the type annotation of the function, for example:
if the type is
TTPELine
orTTPEBlock
then the argument is expanded according to Note on argument expansion of estr-type functions.if the type is
TTPLine
orTTPBlock
then the argument is not expanded, but the tokenization may change the form of the argument, refer to Note on parameter tokenization.
For example, the argument of py()
has annotated type TTPEBlock
, which means you can do the following:
\def\myvalue{1+2}
\py{\myvalue}
and the Python code 1+2
will be executed and 3
printed in \(\TeX\).
Some command are not documented here, refer to:
add_handler()
for\pythonimmediatecallhandler
and\pythonimmediatelisten
.continue_until_passed_back()
andcontinue_until_passed_back_str()
for\pythonimmediatecontinue
and\pythonimmediatecontinuenoarg
.
- pythonimmediate.texcmds.py(code: TTPEBlock) None [source]
Evaluate some Python code provided as an argument and typeset the result as \(\TeX\) code. For example
\py{1+1}
will typeset 2.The code is evaluated as if with
eval()
, so assignments such asa=1
is not suitable here. Usepyc()
instead.
- pythonimmediate.texcmds.pyc(code: TTPEBlock) None [source]
Execute some Python code provided as an argument, typeset whatever resulted by
print_TeX()
.The Python code is absorbed as described in Note on argument expansion of estr-type functions.
The code is evaluated as if with
exec()
, so assignments such as\pyc{a=1}
can be used. Nevertheless, for long code :meth:` Usepyc()
instead.
- pythonimmediate.texcmds.pycode(code: TTPBlock, lineno_: TTPLine, filename: TTPLine, fileabspath: TTPLine) None [source]
A \(\TeX\) environment to execute some Python code. Must be used at top-level, and content can be verbatim.
If the code inside causes any error, the error traceback will point to the correct line in the \(\TeX\) file.
Example:
\begin{pycode} a = 1 b = 2 \end{pycode}
Note
Internal note:
The Python function itself is an auxiliary function.
The
code
is not executed immediately, instead we search for the source \(\TeX\) file that contains the code (it must be found, otherwise an error is thrown), then read the source from that file.\(\TeX\) might mangle the code a bit before passing it to Python, as such we use
can_be_mangled_to()
(might return some false positive) to compare it with the code read from the sourcecode.
- pythonimmediate.texcmds.pycodefuzzy(code: TTPBlock, lineno_: TTPLine) None [source]
Same as
pycode()
, but may mangle the code (strip trailing spaces, etc. Refer tocan_be_mangled_to()
for technical details). Not recommended unless you don’t have[abspath]currfile
package loaded.
- pythonimmediate.texcmds.pycq(code: TTPEBlock) None [source]
Similar to
pyc()
, however any output byprint_TeX()
is suppressed.
- pythonimmediate.texcmds.pyfile(filepath: TTPELine) None [source]
Execute a Python file from filepath, as if with
pyc()
orpycode()
.Example:
\pyfile{a/b.py}
- pythonimmediate.texcmds.pyfilekpse(filename: TTPELine) None [source]
Given a file name, use
kpsewhich
executable to search for its full path, then execute the file at that path.This function is meant for library author wanting to execute Python code stored in separate file.
Example:
\pyfilekpse{a.py}
wherea.py
is stored along with the.sty
files. Runningkpsewhich a.py
on the command-line should give the full path to it.