nick jia
nick jia

Reputation: 601

RuntimeError: Failed to process string with tex because latex could not be found

I was trying to render text with LaTeX in Matplotlib. There is a demo provided by Matplotlib at https://matplotlib.org/3.1.1/gallery/text_labels_and_annotations/tex_demo.html.

However, when I ran this demo, I got this error saying "RuntimeError: Failed to process string with tex because latex could not be found", but I'm sure I have installed Miktex and Ghostscript required by Matlibplot.

Anyone knows how should I solve it? Thanks.

import numpy as np
import matplotlib
matplotlib.rcParams['text.usetex'] = True
import matplotlib.pyplot as plt


t = np.linspace(0.0, 1.0, 100)
s = np.cos(4 * np.pi * t) + 2

fig, ax = plt.subplots(figsize=(6, 4), tight_layout=True)
ax.plot(t, s)

ax.set_xlabel(r'\textbf{time (s)}')
ax.set_ylabel('\\textit{Velocity (\N{DEGREE SIGN}/sec)}', fontsize=16)
ax.set_title(r'\TeX\ is Number $\displaystyle\sum_{n=1}^\infty'
             r'\frac{-e^{i\pi}}{2^n}$!', fontsize=16, color='r')
plt.show()

Here is the error trace back:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 304, in _run_checked_subprocess
    stderr=subprocess.STDOUT)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 336, in check_output
    **kwargs).stdout
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 403, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The System Cannot Find the File Specified
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\tkinter\__init__.py", line 746, in callit
    func(*args)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\_backend_tk.py", line 338, in idle_draw
    self.draw()
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
    super(FigureCanvasTkAgg, self).draw()
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 388, in draw
    self.figure.draw(self.renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 1702, in draw
    **self._tight_parameters)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 2476, in tight_layout
    pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 362, in get_tight_layout_figure
    pad=pad, h_pad=h_pad, w_pad=w_pad)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 111, in auto_adjust_subplotpars
    tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 112, in <listcomp>
    if ax.get_visible()])
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axes\_base.py", line 4355, in get_tightbbox
    bb_xaxis = self.xaxis.get_tightbbox(renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1164, in get_tightbbox
    self._update_label_position(renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 2014, in _update_label_position
    bboxes, bboxes2 = self._get_tick_boxes_siblings(renderer=renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1999, in _get_tick_boxes_siblings
    tlb, tlb2 = axx.xaxis._get_tick_bboxes(ticks_to_draw, renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in _get_tick_bboxes
    for tick in ticks if tick.label1.get_visible()],
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in <listcomp>
    for tick in ticks if tick.label1.get_visible()],
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 890, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 291, in _get_layout
    ismath="TeX" if self.get_usetex() else False)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 201, in get_text_width_height_descent
    s, fontsize, renderer=self)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 448, in get_text_width_height_descent
    dvifile = self.make_dvi(tex, fontsize)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 338, in make_dvi
    texfile], tex)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 308, in _run_checked_subprocess
    'found'.format(command[0])) from exc
RuntimeError: Failed to process string with tex because latex could not be found
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 304, in _run_checked_subprocess
    stderr=subprocess.STDOUT)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 336, in check_output
    **kwargs).stdout
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 403, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The System Cannot Find the File Specified
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\_backend_tk.py", line 259, in resize
    self.draw()
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
    super(FigureCanvasTkAgg, self).draw()
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 388, in draw
    self.figure.draw(self.renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 1702, in draw
    **self._tight_parameters)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 2476, in tight_layout
    pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 362, in get_tight_layout_figure
    pad=pad, h_pad=h_pad, w_pad=w_pad)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 111, in auto_adjust_subplotpars
    tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 112, in <listcomp>
    if ax.get_visible()])
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axes\_base.py", line 4355, in get_tightbbox
    bb_xaxis = self.xaxis.get_tightbbox(renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1164, in get_tightbbox
    self._update_label_position(renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 2014, in _update_label_position
    bboxes, bboxes2 = self._get_tick_boxes_siblings(renderer=renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1999, in _get_tick_boxes_siblings
    tlb, tlb2 = axx.xaxis._get_tick_bboxes(ticks_to_draw, renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in _get_tick_bboxes
    for tick in ticks if tick.label1.get_visible()],
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in <listcomp>
    for tick in ticks if tick.label1.get_visible()],
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 890, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 291, in _get_layout
    ismath="TeX" if self.get_usetex() else False)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 201, in get_text_width_height_descent
    s, fontsize, renderer=self)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 448, in get_text_width_height_descent
    dvifile = self.make_dvi(tex, fontsize)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 338, in make_dvi
    texfile], tex)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 308, in _run_checked_subprocess
    'found'.format(command[0])) from exc
RuntimeError: Failed to process string with tex because latex could not be found
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 304, in _run_checked_subprocess
    stderr=subprocess.STDOUT)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 336, in check_output
    **kwargs).stdout
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 403, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The System Cannot Find the File Specified

Traceback (most recent call last):
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\tkinter\__init__.py", line 746, in callit
    func(*args)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\_backend_tk.py", line 338, in idle_draw
    self.draw()
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
    super(FigureCanvasTkAgg, self).draw()
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 388, in draw
    self.figure.draw(self.renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 1702, in draw
    **self._tight_parameters)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 2476, in tight_layout
    pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 362, in get_tight_layout_figure
    pad=pad, h_pad=h_pad, w_pad=w_pad)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 111, in auto_adjust_subplotpars
    tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 112, in <listcomp>
    if ax.get_visible()])
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axes\_base.py", line 4355, in get_tightbbox
    bb_xaxis = self.xaxis.get_tightbbox(renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1164, in get_tightbbox
    self._update_label_position(renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 2014, in _update_label_position
    bboxes, bboxes2 = self._get_tick_boxes_siblings(renderer=renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1999, in _get_tick_boxes_siblings
    tlb, tlb2 = axx.xaxis._get_tick_bboxes(ticks_to_draw, renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in _get_tick_bboxes
    for tick in ticks if tick.label1.get_visible()],
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in <listcomp>
    for tick in ticks if tick.label1.get_visible()],
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 890, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 291, in _get_layout
    ismath="TeX" if self.get_usetex() else False)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 201, in get_text_width_height_descent
    s, fontsize, renderer=self)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 448, in get_text_width_height_descent
    dvifile = self.make_dvi(tex, fontsize)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 338, in make_dvi
    texfile], tex)
  File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 308, in _run_checked_subprocess
    'found'.format(command[0])) from exc
RuntimeError: Failed to process string with tex because latex could not be found

Upvotes: 50

Views: 174503

Answers (7)

Orfeas Bourchas
Orfeas Bourchas

Reputation: 363

For anyone that the previous answers did not fix anything, you can use on this example and on scienceplots plt.style.use(['no-latex'])

Upvotes: 0

Ranjeet Kumar
Ranjeet Kumar

Reputation: 35

import numpy as np import matplotlib.pyplot as plt plt.rcParams.update(plt.rcParamsDefault)

this must be resolve the above issue, by putting the default paramater for the rcParams.

Upvotes: 0

adam.hendry
adam.hendry

Reputation: 5653

For some reason, MikTeX doesn't seem to add it's bin folder to PATH during installation. Be sure to add:

Local Install:

%LOCALAPPDATA%\Programs\MiKTeX\miktex\bin\x64\

System (Admin) Install:

C:\Program Files\MiKTeX\miktex\bin\x64\

to your PATH after installation. (Ironically, this gets removed from PATH when MikTeX is uninstalled). This should fix the problem.

NOTE: %LOCALAPPDATA% is C:\Users\your-user-name\AppData\Local.

Also, if you only need or want some text expressions rendered in LaTeX, and not the entire plot, matplotlib uses a subset of LaTeX under the hood so you don't need to install LaTeX or set rcParams in that case.

e.g. On

OS: Windows 10
Python: 3.8.10
matplotlib: 3.6.2
MikTeX: 22.10-x64
Ghostscript: 10.00.0

notice the difference between the two:

Without rcParams

import matplotlib.pyplot as plt
import numpy as np

t = np.linspace(0.0, 1.0, 100)
s = np.cos(4 * np.pi * t) + 2

fig, ax = plt.subplots(figsize=(6, 4), tight_layout=True)
ax.plot(t, s)

ax.set_xlabel(r'$\mathbf{time, t (s)}$')
ax.set_ylabel(r'$Velocity, v (\phi/sec)$', fontsize=16)
ax.set_title(r'Without rcParams', fontsize=16, color='r')

plt.show()

Without rcParams

With rcParams

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams.update({
    'text.usetex': True,
    'font.family': 'serif',
})

t = np.linspace(0.0, 1.0, 100)
s = np.cos(4 * np.pi * t) + 2

fig, ax = plt.subplots(figsize=(6, 4), tight_layout=True)
ax.plot(t, s)

ax.set_xlabel(r'$\mathbf{time, t (s)}$')
ax.set_ylabel(r'$Velocity, v (\phi/sec)$', fontsize=16)
ax.set_title(r'With rcParams', fontsize=16, color='r')

plt.show()

With rcParams

It depends on what you want to do.

Upvotes: 3

theo-brown
theo-brown

Reputation: 869

Matplotlib actually ships with its own way of doing TeX-style text, called mathtex:

Note that you do not need to have TeX installed, since Matplotlib ships its own TeX expression parser, layout engine, and fonts. The layout engine is a fairly direct adaptation of the layout algorithms in Donald Knuth's TeX, so the quality is quite good (matplotlib also provides a usetex option for those who do want to call out to TeX to generate their text (see Text rendering With LaTeX).

In most cases, using this is sufficient and requires less stuff installed.

It's used in exactly the same way as LaTeX:

r'$\sum_{i=0}^\infty x_i$'

generates

$\sum_{i=0}^\infty x_i$

Upvotes: 10

Thomas
Thomas

Reputation: 635

When I tried to change the standard font for matplotlib, I likely incorrectly applied a recommended script and broke my matplotlib and from that moment on always got this error message.

Simply resetting matplotlib settings worked perfectly for me.

import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)

Upvotes: 39

Rafael Ferro
Rafael Ferro

Reputation: 489

I managed to solve this issue by installing texlive and latex. In apt based Linux, this command installs texlive:

sudo apt install texlive texlive-latex-extra texlive-fonts-recommended dvipng

latex can be easily installed via pip:

pip install latex

Upvotes: 32

Nicolas Gervais
Nicolas Gervais

Reputation: 36704

According to the matplotlib documentation:

Matplotlib's LaTeX support requires a working LaTeX installation, dvipng (which may be included with your LaTeX installation), and Ghostscript (GPL Ghostscript 9.0 or later is required). The executables for these external dependencies must all be located on your PATH.

If you're not familiar with PATH, this means that the .exe files of these programs need to be in your system environment variables. In Windows, Edit the System Environment Variables, click on variable Path, and add the filepaths of the aforementioned programs to the list.

Upvotes: 16

Related Questions