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

Uploaded Source

Built Distribution

texenv-0.0.4-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: texenv-0.0.4.tar.gz
  • Upload date:
  • Size: 12.9 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.4.tar.gz
Algorithm Hash digest
SHA256 5ff0ea97cbff0b39a7c7646e125fc9f7ba9f3bb82f4ee60d3d4034c0a361b902
MD5 bf954d5279b4036aed9cd9ce96792208
BLAKE2b-256 25edd584ca99afea8942ad0b79ded322adb7b4b5717d0dd551874a0eb84982b0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: texenv-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 13.6 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1858d9d81e5db18346cfab6e62272a9f88c3f019ed7e3c87f184cfcc8f7acee4
MD5 8d12cb61cc8bd8e496182a86ca35a611
BLAKE2b-256 8c864f14cec2bf8eeb048081c59cbf43d1ad7e6db7313524640c4181ab7bbd39

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