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:

For example, the argument of py() has annotated type TTPEBlock, which means you can do the following:


and the Python code 1+2 will be executed and 3 printed in \(\TeX\).

Some command are not documented here, refer to: 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 as a=1 is not suitable here. Use pyc() 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:` Use pyc() 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.


a = 1
b = 2


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 to can_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 by print_TeX() is suppressed.

pythonimmediate.texcmds.pyfile(filepath: TTPELine) None[source]

Execute a Python file from filepath, as if with pyc() or pycode().

Example: \pyfile{a/}

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{} where is stored along with the .sty files. Running kpsewhich on the command-line should give the full path to it.