RuntimeError:LaTeX无法处理以下字符串:r'lb' [英] RuntimeError: LaTeX was not able to process the following string: r'lb'

查看:149
本文介绍了RuntimeError:LaTeX无法处理以下字符串:r'lb'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里也有类似的问题,但是似乎没有一个问题可以解决所有问题.如果我使用 Jupyter Notebook 或 Anaconda Python 在 matplotlib 网站上复制并粘贴 演示代码在Windows上:

There are similar questions on here, but none seem to capture all this problem. If I copy-and-paste the demo code on matplotlib website using a Jupyter Notebook or Anaconda Python on Windows:

"""
Demo of TeX rendering.

You can use TeX to render all of your matplotlib text if the rc
parameter text.usetex is set.  This works currently on the agg and ps
backends, and requires that you have tex and the other dependencies
described at http://matplotlib.org/users/usetex.html
properly installed on your system.  The first time you run a script
you will see a lot of output from tex and associated tools.  The next
time, the run may be silent, as a lot of the information is cached in
~/.tex.cache

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

我收到以下错误(注意:类似的问题是运行 Python 3.x 还是 2.7x).

I get the following error (Note: similar issues whether running Python 3.x or 2.7x).

Traceback (most recent call last):
  File "demo.py", line 34, in <module>
    plt.savefig('tex_demo')
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 688, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/figure.py", line 1565, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backend_bases.py", line 2232, in print_figure
    **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py", line 527, in print_png
    FigureCanvasAgg.draw(self)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py", line 474, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/figure.py", line 1159, in draw
    func(*args)
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/axes/_base.py", line 2324, in draw
    a.draw(renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 1108, in draw
    renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 1058, in _get_tick_bboxes
    extent = tick.label1.get_window_extent(renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/text.py", line 961, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/text.py", line 352, in _get_layout
    ismath=False)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py", line 229, in get_text_width_height_descent
    renderer=self)
  File "/usr/lib/python3/dist-packages/matplotlib/texmanager.py", line 675, in get_text_width_height_descent
    dvifile = self.make_dvi(tex, fontsize)
  File "/usr/lib/python3/dist-packages/matplotlib/texmanager.py", line 422, in make_dvi
    report))
RuntimeError: LaTeX was not able to process the following string:
b'lp'
Here is the full report generated by LaTeX: 

这是在Windows或Linux上运行的iPython笔记本上发生的.但是,当我使用以下命令在 bash shell 中运行它时:

This happened on iPython notebooks running on Windows or Linux. However, when I ran this in a bash shell using:

python3 demo.py

python3 demo.py

我在 stderr 输出中得到了这个小宝石:

I got this little gem in the stderr output:

sh:1:乳胶:未找到

sh: 1: latex: not found

此错误未出现在 Windows shell 或 PowerShell 中,仅出现在 GNU/Linux bash shell 中.安装 pylatex 或其他 Python 乳胶工具似乎不会实际安装 LaTeX 本身,并且可能导致您不会收到错误消息,告诉您它未安装.希望这会对其他人有所帮助.

This error did not appear in the Windows shell or PowerShell and only appeared in the GNU/Linux bash shell. It appears that installing pylatex or other Python latex tools will not actually install LaTeX itself and may result in you not getting errors telling you it is not installed. Hopefully this will help others.

一旦我安装了LaTex(在Ubuntu上使用 sudo apt install latex ),我就重新运行该演示:

Once I installed LaTex (using sudo apt install latex on Ubuntu), I reran the demo:

python3 demo.py

python3 demo.py

现在我明白了:

Traceback (most recent call last):
  File "demo.py", line 34, in <module>
    plt.savefig('tex_demo')
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 688, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/figure.py", line 1565, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backend_bases.py", line 2232, in print_figure
    **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py", line 527, in print_png
    FigureCanvasAgg.draw(self)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py", line 474, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/figure.py", line 1159, in draw
    func(*args)
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/axes/_base.py", line 2324, in draw
    a.draw(renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 1108, in draw
    renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 1058, in _get_tick_bboxes
    extent = tick.label1.get_window_extent(renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/text.py", line 961, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/text.py", line 352, in _get_layout
    ismath=False)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py", line 229, in get_text_width_height_descent
    renderer=self)
  File "/usr/lib/python3/dist-packages/matplotlib/texmanager.py", line 675, in get_text_width_height_descent
    dvifile = self.make_dvi(tex, fontsize)
  File "/usr/lib/python3/dist-packages/matplotlib/texmanager.py", line 422, in make_dvi
    report))
RuntimeError: LaTeX was not able to process the following string:
b'lp'
Here is the full report generated by LaTeX: 

This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015/Debian) (preloaded format=latex)
 restricted \write18 enabled.
entering extended mode
(./710670ddfec8e192b546b57b05284e6c.tex
LaTeX2e <2016/02/01>
Babel <3.9q> and hyphenation patterns for 3 language(s) loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))

! LaTeX Error: File `type1cm.sty' not found.

Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: sty)

Enter file name: 
! Emergency stop.
<read *> 

l.3 \renewcommand
                 {\rmdefault}{pnc}^^M
No pages of output.
Transcript written on 710670ddfec8e192b546b57b05284e6c.log.

这似乎可以将问题减少到与Python:无法在 Matplotlib 中渲染 Tex 并运行 sudo apt install dvipng texlive-latex-extra texlive-fonts-recommended(同样在 Ubuntu 上,但在 Mac 上安装 extras 包应该有类似的效果).

This seems to reduce the problem to the same one as Python: Unable to Render Tex in Matplotlib and running sudo apt install dvipng texlive-latex-extra texlive-fonts-recommended (again on Ubuntu, but installing the extras packages on Mac should have similar effects).

不幸的是,我还没有想出如何在 Windows 下解决这个问题(特别是 Windows 下的 Jupyter Notebook),而且由于我有很多使用 Windows 的学生,任何帮助表示感谢.

Unfortunately, I have not figured out how to fix this under Windows (in particular the Jupyter Notebook under Windows) and since I have a number of students who use Windows, any help is appreciated.

推荐答案

在Windows中手动下载并安装所需的库: MiKTeX .dvipng 将在 MiKTeX 安装文件夹的 bin 文件夹中可用.在用于Python之前,请验证它们是否在Windows PATH上并且可以独立运行.

Download and install the required libraries manually in Windows: Ghostscript and MiKTeX. dvipng will be available in the bin folder of the MiKTeX install folder. Verify they're on your windows PATH and working independently before using for Python.

这篇关于RuntimeError:LaTeX无法处理以下字符串:r'lb'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆