Search code examples
pythonmatplotliblatex

Matplotlib: use TeX only if is available


I would like to run the following snippet:

import matplotlib.pyplot as plt
params = {'text.usetex': True}
plt.rcParams.update(params)

but be able to fall back to 'text.usetex' = False if latex or another requirement (ghostscript comes to mind) is not installed on the machine. What is the most pythonic way to do this?

I was thinking of trying a dummy plot and setting text.usetex to False if an error was raised, but I don't know which error will be raised if latex is not available.


Solution

  • I was looking for the same functionality. After having a look at matplotlib's code, you have a function:

    import matplotlib
    usetex = matplotlib.checkdep_usetex(True)
    

    In my case, as LaTeX is not installed, this warning is displayed

    usetex mode requires TeX.
    

    And usetex is set to False.