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.5.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

texenv-0.0.5-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file texenv-0.0.5.tar.gz.

File metadata

  • Download URL: texenv-0.0.5.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for texenv-0.0.5.tar.gz
Algorithm Hash digest
SHA256 5b356b12a0e9e29d90f918b4155b34090405ee8d80a81db039154c742d643472
MD5 758a55a401dca70298d53d738199dae9
BLAKE2b-256 ae135e1e7265a6c55aa068ab39947dd949ddd4058906f0bac06d59f534f66543

See more details on using hashes here.

File details

Details for the file texenv-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: texenv-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for texenv-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 eaaafb1df56877def22be265364d7254fda9640d4eaa598cdbbbd5347b0f11dd
MD5 ced9a90e893276c0a2f488ba9d3eb1e9
BLAKE2b-256 607b02a8201a9264c80751d2494fe4510a5994f8538bc359e1e24a306427e071

See more details on using hashes here.

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