This article presents a minimal way of \(\mathrm{\LaTeX}\) support that some softwares depend. on. In this example, the barebone \(\mathrm{\LaTeX}\) support for Python virtual env’s matplotlib module is presented.
Sometimes researchers want to enhance the typography on plots with no other than \(\mathrm{\LaTeX}\). I have to admit that this is not always straightforward to do as what the official website claims. In this post, I will discuss \(\mathrm{\LaTeX}\) support for matplotlib, which is a popular scientific plotting modules widely used in Python.
I use the example of Python‘s matplotlib as an example, you could apply this idea to support other software.
Python Virtual Env using Conda
use conda virtual env and conda compoents for \(\mathrm{\LaTeX}\)
Render math equations using TeX
\(\bigstar\)tex_demo.py provides an example on how \(\mathrm{\LaTeX}\) could be embedded in the matplotlib plot. We will use it as an example for demonstrations purpose. Finally you could save the your designed figured in pdf or svg format and use that for scenarios including scientific publications. This link also contains detailed info on how matplotlib would work with \(\mathrm{\LaTeX}\). One of the most important thing is to make sure that there is a installation of \(\mathrm{\LaTeX}\) installed and the path to the executables is within the $PATH
variable in your system.
The following Python snippet could help you check if your $PATH
variable contains directory that has the string “tex”. I assume that readers do know something about \(\mathrm{\LaTeX}\), they know how to setup \(\mathrm{\LaTeX}\) environment.
import os
print([p for p in os.getenv('PATH').split(':') if 'tex' in p.lower()])
PythonConda Based \(\mathrm{\LaTeX}\) Support
In the begininng, I want to see how we could setup conda so that the Python virtual env could handle \(\mathrm{\LaTeX}\) requirement without installing \(\mathrm{\LaTeX}\) separately.
In the following snippet, a python virtual env is created, activated.
matplotlib_latex ➤ conda create -n matplotlib_latex python=3.13 -y
matplotlib_latex ➤ conda activate matplotlib_latex
(matplotlib_latex) matplotlib_latex ➤ pip install matplotlib numpy
BashIf I run the \(\bigstar\)tex_demo.py directly within the virtual env, I could see the following error, this is because you could see this is due to the fact matplotlib cannot find latex. command. This is expected, because we haven’t yet to install a environment at all.
File "/opt/homebrew/Caskroom/miniconda/base/envs/matplotlib_latex/lib/python3.13/site-packages/matplotlib/texmanager.py", line 254, in _run_checked_subprocess
raise RuntimeError(
f'Failed to process string with tex because {command[0]} '
'could not be found') from exc
RuntimeError: Failed to process string with tex because latex could not be found
BashThe natural thought would be to install \(\mathrm{\LaTeX}\) directly within Python virtual env. By searching the conda archive, I could only find texlive-core
.I then tried to setup the \(\mathrm{\LaTeX}\) within python by running the command conda install anaconda::texlive-core
. It should be noted that this is not possible for me to generate the plots after texlive-core
is installed, even through matplotlib could find the executable. There are multiple errors that I could not be able to do so.
File "/opt/homebrew/Caskroom/miniconda/base/envs/matplotlib_latex/lib/python3.13/site-packages/matplotlib/dviread.py", line 343, in _read
raise self._missing_font.to_exception()
FileNotFoundError: Matplotlib's TeX implementation searched for a file named 'cmss17.tfm' in your texmf tree, but could not find it
BashIf I try to run fmtutil-sys --all
, it still shows errors.
(matplotlib_latex) matplotlib_latex ➤ fmtutil-sys --all
Can't locate TeXLive/TLUtils.pm in @INC (you may need to install the TeXLive::TLUtils module) (@INC contains: /opt/homebrew/Caskroom/miniconda/base/envs/matplotlib_latex/share/tlpkg /opt/homebrew/Caskroom/miniconda/base/envs/matplotlib_latex/share/texmf-dist/scripts/texlive /Library/Perl/5.34/darwin-thread-multi-2level /Library/Perl/5.34 /Network/Library/Perl/5.34/darwin-thread-multi-2level /Network/Library/Perl/5.34 /Library/Perl/Updates/5.34.1 /System/Library/Perl/5.34/darwin-thread-multi-2level /System/Library/Perl/5.34 /System/Library/Perl/Extras/5.34/darwin-thread-multi-2level /System/Library/Perl/Extras/5.34) at /opt/homebrew/Caskroom/miniconda/base/envs/matplotlib_latex/bin/fmtutil line 45.
BEGIN failed--compilation aborted at /opt/homebrew/Caskroom/miniconda/base/envs/matplotlib_latex/bin/fmtutil line 45.
Bash My conclusion is that the texlive-core
could not fulfill the requirement.
Using TinyTeX
If cannot figure out a way for \(\mathrm{\LaTeX}\) support, I need to admit that I need to install a version of \(\mathrm{\LaTeX}\) outside of Conda. I went online to find TinyTeX and its GitHub repo. The obvious advantages is that you don’t need gigs of storages for a full setup. Various versionf of TinyTeX would use at most 700MB. Another advantage is that it can be put anywhere at any OS, all you need to ensure is to put the path to executables within TinyTeX to the $PATH variable. As different OSes need different setup, you need to check how to modify the $PATH variable.
For matplotlib \(\mathrm{\LaTeX}\) support, when running \(\bigstar\)tex_demo.py using different versions of TinyTeX, I recommend TinyTeX-1
and more comprehensive versions, including TinyTeX
and TinyTeX-2
. Let’s focus on TinyTeX-1
first. You could download the archives from GitHub repo and unzip at a specific location, be sure to modify the $PATH variable to add the executable locations.
Run the following command to ensure \(\bigstar\)tex_demo.py can be run, this enables the scipt to run properly. It should also be noted that if you use a customized script you need to make sure the additional packages are installed in tlmgr
.
tlmgr install cm-super type1cm underscore dvipng
BashOnce the packages for TinyTeX are installed, we could run the \(\bigstar\)tex_demo.py again. It should be noted that on Mac, you may find the following warning “luatex” Not Opened.

You need to go to Settings and then Allow the executable to run, shown as follows.


Finally, you could save a copy of svg plot shown as follows.
Conclusion
In this post, we try to add barebone \(\mathrm{\LaTeX}\) support for softwares, for example matplotlib. It can be seen that conda’s venv management tool could not provide proper \(\mathrm{\LaTeX}\) support. An alternative version namely TinyTeX could be utilized to get the minimal \(\mathrm{\LaTeX}\) support.
Leave a Reply to Practical Tips for Machine Learning Research and Development – Mustang Wang Cancel reply