Python:Matplotlib无法渲染Tex

74

我最近将我的笔记本电脑升级到了 Snow Leopard,更新了 TeX 到 3.1415926 版本 (TeX Live 2011/MacPorts 2011_5),同时安装了 Python 2.7.3。在这些安装完成后,我运行了 macport selfupdate 和 macport upgrade outdated。但是,现在当我尝试在 matplotlib 中使用 TeX 时,我收到了以下错误信息:

LaTeX was not able to process the following string:'lp'
Here is the full report generated by LaTeX: 

This is pdfTeX, Version 3.1415926-2.3-1.40.12 (TeX Live 2011/MacPorts 2011_5)
 restricted \write18 enabled.  
entering extended mode (./64a53cc27244d5ee10969789771e33fa.tex
LaTeX2e <2009/09/24>
Babel <v3.8l> and hyphenation patterns for english, dumylang, nohyphenation, cz
ech, slovak, dutch, ukenglish, usenglishmax, basque, french, german-x-2009-06-1
9, ngerman-x-2009-06-19, german, ngerman, swissgerman, italian, polish, portugu
ese, spanish, catalan, galician, ukenglish, loaded.
(/opt/local/share/texmf-texlive-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/opt/local/share/texmf-texlive-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)


l.3 \renewcommand
             {\rmdefault}{pnc}^^M
No pages of output.

此前的问题类似,我尝试通过以下方式在我的Python代码中设置路径:

os.environ['PATH'] = os.environ['PATH'] + ':/opt/local/bin/latex'

由于which latex的结果是/opt/local/bin/latex,然而尝试使用该命令时却出现了相同的错误信息。我还尝试了tex的路径以及上一个问题中的示例,但都无济于事。

然后我尝试通过强制安装可能缺失的软件包来解决问题:

matplotlib.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]

但是,那也没有起作用。

我唯一能让我的图表工作的方式是使用rc('text', usetex=False),这不是理想的。非常感谢任何帮助。


7
这个命令sudo apt-get install dvipng对我很有用。 - Alexander McFarlane
5个回答

95
在 Ubuntu 14.04 上,以上答案的组合起作用了。我sudo apt-get installdvipngtexlive-latex-extratexlive-fonts-recommended包,这就解决了问题。
$ sudo apt-get install dvipng texlive-latex-extra texlive-fonts-recommended 

编辑:自Matplotlib 3.2.1起,您还需要安装cm-super包(请参见https://github.com/matplotlib/matplotlib/issues/16911)。

$ sudo apt-get install dvipng texlive-latex-extra texlive-fonts-recommended cm-super

5
同样适用于Ubuntu16.04操作系统。 - user
2
同样适用于Windows子系统中的Ubuntu 16.04.1。 - michaelosthege
1
我可以生成图表,但它们具有衬线字体。 - Soren
1
更新于2021年9月:这也适用于Ubuntu 21.04。 - Heath
在 Windows(防火墙后面)遇到了类似的问题。添加 cm-super 解决了它。 - Marcel Flygare
1
也适用于22.04 =) - Rasmus

57

3
仍适用于我使用的Ubuntu 13.10机器。我还需要安装dvipng软件包。 - travc
3
在Ubuntu 14.04.1上,我需要同时安装texlive-latex-extratexlive-fonts-recommended - eldad-a
1
在Ubuntu 15.10上同样的问题...然而,Matplotlib继续使用安装这些软件包之前生成的图形的缓存版本。在安装这些软件包之前,Matplotlib将生成没有任何文本的图形。花了一点时间才意识到正在使用缓存版本,但删除~/.cache/matplotlib/tex.cache/中的dvi和pdf文件解决了问题。 - Matt Hancock
9
在Ubuntu 16.04上,需要安装texlive-latex-extratexlive-fonts-recommendeddvipng - galath
2
有没有想过在Windows机器上添加什么?尝试添加type1cm,但它说在miktex上找不到该软件包。谢谢。 - Wade Bratz
显示剩余3条评论

44

我需要在一个基于Ubuntu 18.04的发行版(jupyter/minimal-notebook)中安装cm-super软件包。

$ sudo apt-get install cm-super

6
在Ubuntu 18.04中,除了需要安装dvipngtexlive-latex-extratexlive-fonts-recommended之外,还需要安装cm-super。这些软件包是必需的。 - Donshel
1
我在(LM) Ubuntu 18.04上也需要cm-super - diatomicDisaster
这个方法对我也起作用了。之前我已经使用sudo apt install texlive(其中包括texlive-fonts-recommended)。但是我还需要安装cm-super和texlive-latex-extra(使用apt-get安装,因为某些原因apt无法工作)。 - nabla
Ubuntu 19.10 中,安装 cm-super 对我也起到了作用,但有两个注意点:(i) 现在绘图需要更长的时间来呈现,(ii) 我的字体样式受到了影响(这是我不想要的)。 - kmario23
@kmario23 我不确定为什么渲染需要更长时间,但对于字体,我认为这可能是有道理的。你能否检查一下使用了哪些字体来渲染图表而没有使用LaTeX(默认或rc('text', usetex=True)),以及在使用rc('text', usetex=True)后使用了哪些字体? - FZeiser

2

在Ubuntu 20.04中,除了需要安装dvipng texlive-latex-extra texlive-fonts-recommended之外,还需要安装cm-super


0
在Manjaro(Arch)上,我只需要安装texlive-latexextra,其中包含type1cm.sty文件。
pacman -S texlive-latexextra

我的电脑上已经安装了texlive-bintexlive-core软件包,这些软件包可能也是必需的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接