RuntimeError:由于找不到乳胶而无法使用tex处理字符串

时间:2019-09-26 16:52:03

标签: python matplotlib

我试图在Matplotlib中使用LaTeX渲染文本。 Matplotlib在https://matplotlib.org/3.1.1/gallery/text_labels_and_annotations/tex_demo.html提供了一个演示。

但是,当我运行此演示时,出现此错误,提示“ RuntimeError:由于找不到乳胶,无法使用tex处理字符串”,但我确定我已经安装了Matlibplot所需的Miktex和Ghostscript。

任何人都知道我应该如何解决? 谢谢。

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()

这是错误回溯:

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

4 个答案:

答案 0 :(得分:6)

根据matplotlib documentation

Matplotlib的LaTeX支持需要可运行的LaTeX安装,dvipng(它可能包含在LaTeX安装中)和Ghostscript(需要GPL Ghostscript 9.0或更高版本)。这些外部依赖项的可执行文件必须全部位于PATH中。

如果您不熟悉PATH,则意味着这些程序的.exe文件必须位于系统环境变量中。在Windows中,编辑系统环境变量,单击变量 Path ,然后将上述程序的文件路径添加到列表中。

  • 获取LaTex here
  • 获取dvipng here
  • 获取Ghostscript > 9.0 here

答案 1 :(得分:0)

我设法通过安装texlivelatex解决了这个问题。在基于apt的Linux中,此命令将安装texlive:

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

乳胶可以通过pip轻松安装:

pip install latex

答案 2 :(得分:0)

Matplotlib实际上带有自己的TeX样式文本,称为mathtex

请注意,由于Matplotlib,您不需要安装TeX 附带了自己的TeX表达式解析器,布局引擎和字体。的 布局引擎是布局算法的直接改编 在Donald Knuth的TeX中,因此质量相当不错(matplotlib也 为想要调出TeX的用户提供usetex选项 生成其文本(请参见使用LaTeX渲染文本)。

在大多数情况下,使用此功能就足够了,所需的东西也更少。

它的使用方式与LaTeX完全相同:

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

生成

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

答案 3 :(得分:-2)

我对此有疑问。我安装了乳胶,dvipng和dvis,并可以从命令行运行。仍然遇到相同的问题。