【问题标题】:Latex implementation problem with matplotlib/python: RuntimeError: Latex command not foundmatplotlib/python 的 Latex 实现问题:RuntimeError: Latex command not found
【发布时间】:2019-06-17 06:06:20
【问题描述】:

我想在我的 matplotlib 图形中使用乳胶作为我的标签。我正在使用 Mac OS X 和 thonny。我下载了一个测试程序,但每次都报错:

RuntimeError:找不到 Latex 命令。安装 'xelatex' 或将 pgf.texsystem 更改为所需的命令。

我认为是路径错误...

我也试过改路径

os.environ["PATH"] += os.pathsep + '/usr/local/texlive/2018/bin/x86_64-darwin'

我得到了这个错误:

kpathsea:make_tex: 无效的文件名“DejaVu Serif”,包含“” /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory 已弃用;将来,将在“datapath”目录中找到示例。 “相对于'datapath'目录找到。”.format(key)) /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory 已弃用;将来,将在“datapath”目录中找到示例。 “相对于'datapath'目录找到。”.format(key)) /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory 已弃用;将来,将在“datapath”目录中找到示例。 “相对于'datapath'目录找到。”.format(key)) /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory 已弃用;将来,将在“datapath”目录中找到示例。 “相对于'datapath'目录找到。”.format(key)) /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory 已弃用;将来,将在“datapath”目录中找到示例。 “相对于'datapath'目录找到。”.format(key)) /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory 已弃用;将来,将在“datapath”目录中找到示例。 “相对于'datapath'目录找到。”.format(key)) /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory 已弃用;将来,将在“datapath”目录中找到示例。 “相对于'datapath'目录找到。”.format(key)) /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory 已弃用;将来,将在“datapath”目录中找到示例。 “相对于'datapath'目录找到。”.format(key)) /Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/init.py:886: MatplotlibDeprecationWarning: examples.directory 已弃用;将来,将在“datapath”目录中找到示例。 “相对于'datapath'目录找到。”.format(key)) 回溯(最近一次通话最后): 文件“/Users/Marten/Downloads/pgf_preamble_sgskip.py”,第 33 行,在 plt.tight_layout(.5) 文件“/Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/pyplot.py”,第 1382 行,位于tight_layout gcf().tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect) 文件“/Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/figure.py”,第 2370 行,位于tight_layout 渲染器 = get_renderer(self) 文件“/Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/tight_layout.py”,第 228 行,在 get_renderer 渲染器 = canvas.get_renderer() 文件“/Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/backends/backend_pgf.py”,第 946 行,在 get_renderer return RendererPgf(self.figure, None, dummy=True) init 中的文件“/Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/backends/backend_pgf.py”,第 393 行 self.latexManager = LatexManagerFactory.get_latex_manager() 文件“/Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/backends/backend_pgf.py”,第 208 行,在 get_latex_manager new_inst = LatexManager() init 中的文件“/Users/Marten/Library/Python/3.7/lib/python/site-packages/matplotlib/backends/backend_pgf.py”,第 290 行 "或序言错误:\n%s" % stdout) matplotlib.backends.backend_pgf.LatexError: LaTeX 返回错误,可能缺少字体或序言中的错误: b'这是 XeTeX,版本 3.14159265-2.6-0.99999(TeX Live 2018)(预加载格式=xelatex)\n 受限\write18 已启用。\n**进入扩展模式\nLaTeX2e 补丁级别 2\ nBabel 和 84 种语言的断字模式已加载。\n\n*(/usr/local/texlive/2018/texmf-dist/tex/latex/base/minimal.cls\n文档类:最小 2001/ 05/25 标准 LaTeX 最小类\n)\n*(/usr/local/texlive/2018/texmf-dist/tex/latex/units/units.sty\n(/usr/local/texlive/2018/texmf- dist/tex/latex/base/ifthen.sty)\n(/usr/local/texlive/2018/texmf-dist/tex/latex/units/nicefrac.sty))\n*(/usr/local/texlive/ 2018/texmf-dist/tex/latex/metalogo/metalogo.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/graphics/graphicx.sty\n(/usr/local/texlive/ 2018/texmf-dist/tex/latex/graphics/keyval.sty)\n(/usr/local/texlive/2018/texmf-dist/tex/latex/graphics/graphics.sty\n(/usr/local/texlive /2018/texmf-dist/tex/latex/graphics/trig.sty)\n(/usr/local/texlive/2018/texmf-dist/tex/latex/graphics-cfg/graphics.cfg)\n(/ usr/local/texlive/2018/texmf-dist/tex/latex/graphics-def/xetex.def))))\n(/usr/local/texlive/2018/texmf-dist/tex/generic/ifxetex/ifxetex. sty)\n(/usr/local/texlive/2018/texmf-dist/tex/latex/fontspec/fontspec.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/l3packages/xparse /xparse.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/l3kernel/expl3.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/l3kernel /expl3-code.tex)\n(/usr/local/texlive/2018/texmf-dist/tex/latex/l3kernel/l3xdvipdfmx.def)))\n(/usr/local/texlive/2018/texmf-dist /tex/latex/fontspec/fontspec-xetex.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/base/fontenc.sty\n(/usr/local/texlive/2018/texmf -dist/tex/latex/base/tuenc.def)))\n(/usr/local/texlive/2018/texmf-dist/tex/latex/fontspec/fontspec.cfg))))\n*(/usr/本地/texlive/2018/texmf-dist/tex/latex/unicode-math/unicode-math.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/unicode-math/unicode-math -xetex.s\nty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/l3packages/l3key s2e/l3keys2e.sty)\n (/usr/local/texlive/2018/texmf-dist/tex/latex/filehook/filehook.sty)\n(/usr/local/texlive/2018/texmf-dist/tex/ latex/base/fix-cm.sty\n(/usr/local/texlive/2018/texmf-dist/tex/latex/base/ts1enc.def))\n(/usr/local/texlive/2018/texmf- dist/tex/latex/unicode-math/unicode-math-table.t\nex)))\n*\n!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n!\n!字体规范错误:“未找到字体”\n! \n!找不到字体“DejaVu Serif”。​​\n! \n!有关详细信息,请参阅 fontspec 文档。\n! \n!如需即时帮助,请键入 H .\n!................................................. ...... \n \n \setmainfont{DejaVu Serif}\n \n没有输出页面。\n写在 texput.log 上的文字记录。\n'

测试程序:

import matplotlib as mpl
mpl.use("pgf")
import matplotlib.pyplot as plt
import os
os.environ["PATH"] += os.pathsep + '/usr/local/texlive/2018/bin/x86_64-darwin'

plt.rcParams.update({
    "font.family": "serif",  # use serif/main font for text elements
    "text.usetex": True,     # use inline math for ticks
    "pgf.rcfonts": False,    # don't setup fonts from rc parameters
    "pgf.texsystem" : "xelatex",
    "pgf.preamble": [
         "\\usepackage{units}",          # load additional packages
         "\\usepackage{metalogo}",
         "\\usepackage{unicode-math}",   # unicode math setup
         r"\setmathfont{xits-math.otf}",
         r"\setmainfont{DejaVu Serif}",  # serif font via preamble
         ]
})

plt.figure(figsize=(4.5, 2.5))
plt.plot(range(5))
plt.xlabel("unicode text: я, ψ, €, ü, \\unitfrac[10]{°}{µm}")
plt.ylabel("\\XeLaTeX")
plt.legend(["unicode math: $λ=∑_i^∞ μ_i^2$"])
plt.tight_layout(.5)

plt.savefig("pgf_preamble.pdf")
plt.savefig("pgf_preamble.png")

【问题讨论】:

  • 奇怪的是您的安装没有正确处理PATH。无论如何,关于您的第二个错误,很可能是因为您使用的是 xelatex 特定软件包。所以把"pgf.texsystem" : "pdflatex",这一行改成"pgf.texsystem" : "xelatex",
  • 是的 xelatex 是默认值。 xelatex 也是同样的错误,所以我尝试了 pdflatex,但它是一样的。因此,如果有帮助,我可以从任何方向使用终端启动 xelatex ...:/
  • 我很确定您获得的第二个错误来自您使用pdflatex 而不是xelatex。您能否使用"pgf.texsystem" : "xelatex", 运行您的程序并编辑您的答案以包含完整的错误消息?
  • 所以我编辑并放入了完整的错误。我还尝试注释掉dejavu serif,我得到不同的错误RuntimeError: No suitable pdf to png renderer found.
  • 问题现在很清楚了:latex 找不到 DejaVu Serif 字体...所以要么这个字体没有安装在你的系统上,要么你的 texlive 安装有问题(或者你调用这个python脚本的方式)。您可以使用xelatex 编译以下内容吗? (将其保存在文件中,例如 test.tex 并运行 xelatex test.tex):\documentclass{minimal} \usepackage{fontspec} \setmainfont{DejaVu Serif} \begin{document} Hello \end{document}

标签: python matplotlib latex


【解决方案1】:

所以我用plt.rc('text', usetex=True)plt.rc('font', family='serif') 尝试了一个不同的测试程序,没有额外的序言。这在我添加路径os.environ["PATH"] += os.pathsep + '/usr/local/texlive/2018/bin/x86_64-darwin' 时有效。所以问题以不同的方式解决。 – madden994 刚刚编辑

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-02
    • 2010-11-12
    • 1970-01-01
    • 2023-02-19
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    相关资源
    最近更新 更多