[Tex/LaTex] way to create Tex Live or LaTeX virtual environment

packagespathstexlive-2018

I am looking for a Python-style virtualenv or venv to create a virtual environment where I can install LaTeX packages such that the packages are available to my current LaTeX project only (but not available system-wide).

I am expecting something like the following:

  • Create a virtual-environment path, say, ./venv.
  • Set some environment variables or modify tlmgr install commands such that new packages are installed into ./venv with the proper directory structure.
  • Set some environment variables or modify pdflatex commands such that the new documents I compile use packages from the ./venv directory along with the packages in the system-wide paths that came with Tex Live distribution.
  • I do not however want to hardcode any ./venv paths in the LaTeX documents (the .tex files). The source code in the .tex file should be agnostic of whether it is being compiled with a virtual environment or not.

Best Answer

You can use TeX Live's user mode for that purpose. As you have already mentioned in the comments, you can create a new user tree (allowing multiple trees) using

$ tlmgr init-usertree --usertree ~/test-tree

To install a package in this tree you have to pass the user tree and the option --usermode

$ tlmgr install --usertree ~/test-tree --usermode <package>

To run LaTeX you have to give it a hint where to look for user packages, by setting the TEXMFHOME variable. You can either do this separately for each run

$ TEXMFHOME=~/test-tree pdflatex test.tex

or globally for the current shell by exporting the name (use unset to exit the “virtual environment”)

$ export TEXMFHOME=~/test-tree
$ pdflatex test.tex
$ unset TEXMFHOME
Related Question