Package: texenv
texenvCreate lightweight TeX virtual environments, and use Python methods directly from TeX code.Installationtexenvis currently only supported on Windows, and requirestexliveto be installed on the system.https://tug.org/texlive/windows.html#installInstalltexenvinside an existing Python virtual environment,pipinstalltexenvTo create a standalone TeX installation visible only to the current environment,texenvinitThis installs a bare-bones version of LaTeX in.venv/texand 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.UsageThe TeX environment supports the standardtlmgrtool which can be used to install and update packages as normal.tlmgrinstall<packagename>To write all currently installed TeX packages in the environment to a file (excluding core packages required for LaTeX to run),texenvfreeze>texrequirements.txtTo synchronize the TeX installation with the packages found in a requirements file,texenvsynctexrequirements.txt.texfiles can be compiled in the environment using the standard tools (i.epdflatex).texenvalso supports a preprocessor that can be used to call Python methods directly from TeX code. This is useful for generating figures and table data from python, or doing math that is difficult in LaTeX code. The example below shows a simple use case:Contents ofexample.tex:\documentclass{article}% import a python module either installed in the environment, or just a file in the same folder as the .tex file.% In this case figures.py is in the same folder.\import\figures% preprocessor variables can be defined with \pydef followed by the variable name and value.\pydef\cleanfigtrue\begin{document}% call a python method. Supports arguments and kwargs, but all are passed in as string types. The return type of% the method must be a string as it is inserted directly into the TeX code.\figures\figA[clean=\cleanfig, width=5in]\end{document}Contents offigures.py, located in the same directory asexample.tex:fromtexenv.macrosimportfigureimportmatplotlib.pyplotaspltdeffigA(clean="true",width="3in",**kwargs):ifclean=="false":returnfigure(file="figA.pdf",width=width,**kwargs)fig,ax1=plt.subplots(1,1)ax1.plot(range(11,17))fig.savefig("figA.pdf")returnfigure(file="figA.pdf",width=width,**kwargs)Compile on command line inside virtual environment:texenvrunexample.texFull example:examples/macro_example/macro_example.texTheruncommand invokes the preprocessor, and then callspdflatexon the post-processed.texfile. The synctex file is modified after runningpdflatexso the intermediate file is transparent to synctex.Slideshowstexenvprovides a simple way to generate PDF slideshow presentations directly from Python. Matplotlib figures, images, and LaTeX code (as strings in Python) can be assembled together into a slide using thePresentationclass:fromtexenvimportPresentation,datatable...# text to place on slide. This is interpreted as an enumerate list if \item appears first in the string.# Otherwise, the text is inserted directly as LaTeX code and supports math macros etc...text=r"""\item Bullet 1\item Bullet 2\[ \nabla \times \mathbf{E}= -\mu {\partial \mathbf{H}\over \partial t} \]\[ \nabla \times \mathbf{H}= \varepsilon {\partial \mathbf{E}\over \partial t} \]"""# create a table with header names, and a format string applied to each celltable=datatable(np.arange(16).reshape((4,4)),header_row=[f"Header{i}"foriinrange(4)],formatter="{:.2f}")pres=Presentation("example_slides.pdf")# add the content to the first slide. The layout will be a 2x2 grid, with the figure centered along both rows in the# second column. The text and table will split the first column.pres.add_slide(content=[[text,fig],[table,None]],title="Example TeX Slides",width_ratio=(1,2)# make the second column twice as wide as the first column.)pres.save()Full example:examples/slideshow/slideshow.pyVSCode Setuptexenvis designed to work with the Latex Workshop extension in VSCode. Once the extension is installed, the following
settings should be added to the usersettings.jsonfile:"latex-workshop.latex.recipes":[{"name":"texenv","tools":["texenv"]}],"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","latex-workshop.latex.autoBuild.run":"onSave",Licensetexenv is licensed under the MIT License.