'FileNotFoundError: [Errno 2] No such file or directory: 'latex': 'latex' (Python 3.6 issue)

I'm trying to use the latex interpreter for figure labels. I generate my figures using the matplotlib library.

I am having trouble finding an answer to this common problem. I see many answers suggesting latex should be added to the path, how do we do this?

I've tried installing Ghostscript, updating matplotlib, etc but to no avail. Any help on this matter would be appreciated greatly.

Snippet of code for testing:

from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
## for Palatino and other serif fonts use:
#rc('font',**{'family':'serif','serif':['Palatino']})
rc('text', usetex=True)

import numpy as np
import matplotlib.pyplot as plt


# Example data
t = np.arange(0.0, 1.0 + 0.01, 0.01)
s = np.cos(4 * np.pi * t) + 2

plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.plot(t, s)

plt.xlabel(r'\textbf{time} (s)')
plt.ylabel(r'\textit{voltage} (mV)',fontsize=16)
plt.title(r"\TeX\ is Number "
          r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
          fontsize=16, color='gray')
# Make room for the ridiculously large title.
plt.subplots_adjust(top=0.8)

plt.savefig('tex_demo')
plt.show()

Here is the result of the executed code:

Traceback (most recent call last):

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/IPython/core/formatters.py", line 341, in __call__
    return printer(obj)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/IPython/core/pylabtools.py", line 241, in <lambda>
    png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/IPython/core/pylabtools.py", line 125, in print_figure
    fig.canvas.print_figure(bytes_io, **kw)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 2212, in print_figure
    **kwargs)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 513, in print_png
    FigureCanvasAgg.draw(self)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 433, in draw
    self.figure.draw(self.renderer)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/figure.py", line 1475, in draw
    renderer, self, artists, self.suppressComposite)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
    a.draw(renderer)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 2607, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
    a.draw(renderer)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/axis.py", line 1192, in draw
    renderer)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/axis.py", line 1130, in _get_tick_bboxes
    extent = tick.label1.get_window_extent(renderer)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/text.py", line 922, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/text.py", line 309, in _get_layout
    ismath=ismath)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 232, in get_text_width_height_descent
    s, fontsize, renderer=self)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/texmanager.py", line 501, in get_text_width_height_descent
    dvifile = self.make_dvi(tex, fontsize)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/texmanager.py", line 365, in make_dvi
    texfile], tex)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/texmanager.py", line 335, in _run_checked_subprocess
    stderr=subprocess.STDOUT)

  File "/Users/selih/anaconda3/lib/python3.6/subprocess.py", line 356, in check_output
    **kwargs).stdout

  File "/Users/selih/anaconda3/lib/python3.6/subprocess.py", line 423, in run
    with Popen(*popenargs, **kwargs) as process:

  File "/Users/selih/anaconda3/lib/python3.6/subprocess.py", line 729, in __init__
    restore_signals, start_new_session)

  File "/Users/selih/anaconda3/lib/python3.6/subprocess.py", line 1364, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)

FileNotFoundError: [Errno 2] No such file or directory: 'latex': 'latex'

<Figure size 432x288 with 1 Axes>

I believe this is quite a common error, but the answers for solving it have not gave me the tools to resolve it. Any help on adding latex, dvipng to my PATH would be appreciated.



Solution 1:[1]

I had the same problem and installed these packages, and the problem is gone.

sudo aptitude install texlive-fonts-recommended texlive-fonts-extra
sudo apt-get install dvipng

You may also want to try this answer: https://stackoverflow.com/a/55137294/4448477

Solution 2:[2]

I had the same problem, except I already had the fonts and latex installed. The problem was that latex wasn't being included in my python path.

You can check if python can access latex by running:

import subprocess
subprocess.check_call(["latex"])

if that still returns a FileNotFound error, then you can manually (temporarily) add latex to your path using something like:

import os 
os.environ["PATH"] += os.pathsep + '/usr/bin'

Note: /usr/bin/latex is where latex is for ubuntu and perhaps other linux systems. Might be different for you. If you are using linux, you can check where latex is by running (in the command line):

whereis latex

Solution 3:[3]

In ubuntu 20.04 I had to do all this as well.

sudo apt-get install cm-super

Solution 4:[4]

If this error occurs on a docker container you have to add the following command on the Dockerfile in order for these packages to be included in the new docker image.

RUN apt-get -y --no-install-recommends install texlive-latex-base texlive-fonts-recommended texlive-fonts-extra texlive-latex-extra

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 weefwefwqg3
Solution 2 GattacaCat
Solution 3 Zombiee
Solution 4