Skip to main content

latex inside a python virtual environment

Project description

texenv

Creates lightweight TeX virtual environments and call Python methods directly from TeX code.

Installation

texenv is currently only supported on Windows, and requires texlive to be installed on the system.
https://tug.org/texlive/windows.html#install

Install texenv inside an existing Python virtual environment,

pip install texenv

To create a standalone TeX installation visible only to the current environment,

texenv init

This installs a bare-bones version of LaTeX in .venv/tex and modifies the environment activation scripts so this TeX installation is used instead of the base installation. The console needs to be closed and re-opened for the changes to take effect.

Usage

The TeX environment supports the standard tlmgr tool which can be used to install and update packages as normal.

tlmgr install <package name>

To write all currently installed TeX packages in the environment to a file (excluding core packages required for LaTeX to run),

texenv freeze > texrequirements.txt

To synchronize the TeX installation with the packages found in a requirements file,

texenv sync texrequirements.txt

Compiling

.tex files can be compiled in the environment using the standard tools (i.e pdflatex). texenv also supports a preprocessor that can be used to call Python methods directly from TeX code. The string returned by the Python method will be inserted into the document before compilation.

Writing TeX macros in Python is much more straight forward and more versatile than writing them in TeX.

Contents of example.tex:

\documentclass{article}

\import\pymacros_1 as \pym
\pydef\test true

\begin{document}
   
   \pym\simple[argument1, arg2=\test]
   
\end{document}

Contents of pymacros_1.py, located in the same directory as example.tex, or in a installed module named pymacros_1

def simple(arg1: str, arg2: str = 'default') -> str:
    return str(arg1) + '|' + str(arg2)

Compile on command line inside virtual environment:

texenv run example.tex

The run command invokes the preprocessor, and then calls pdflatex on the post-processed .tex file. The synctex file is modified after running pdflatex so the intermediate file is transparent to synctex.

Contents of post-processed example.tex (located in build folder):

\documentclass{article}



\begin{document}
   
   argument1|true
   
\end{document}

VSCode Setup

texenv is designed to work with the Latex Workshop extension in VSCode. Once the extension is installed, the following settings should be added to the settings.json file:

    "latex-workshop.latex.tools": [
        {
          "name": "texenv",
          "command": "texenv",
          "args": [
            "run", "%DOC_EXT%"
          ],
          "env": {
            "Path": "%WORKSPACE_FOLDER%/.venv/tex/bin/windows;%WORKSPACE_FOLDER%/.venv/Scripts;%PATH%"
          }
        },
    ],
    "latex-workshop.view.pdf.internal.synctex.keybinding": "double-click"

License

texenv is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

texenv-0.0.7.tar.gz (19.6 kB view hashes)

Uploaded Source

Built Distribution

texenv-0.0.7-py3-none-any.whl (20.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page