我正在尝试使用 matplotlib 中的 pgf 后端来创建手稿的图形文件,按照 matplotlib 文档中的示例进行操作我没有 xelatex 所以我尝试使用 pdflatex 和 lualatex。但是,如果我尝试使用文档的示例并保存该图,则会收到错误。互联网搜索并没有多大帮助。我很高兴知道我是否做错了什么,或者它是否对其他人有效(在这种情况下,知道你的 matplotlib 和 pdfTeX/LuaTeX 版本会很有帮助 - 我的是 matplotlib 3.5.1、pdfTeX 3.1415926-2.5-1.40。 14(TeX Live 2013)和 LuaTeX beta-0.76.0-2020040104)。或者我的 TeX 设置中的某些内容是否需要更改?

我收到的错误消息是

! LaTeX Error: File `pdftex.def' not found.

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

Enter file name: ! TeX capacity exceeded, sorry [input stack size=5000].
<argument> \usepackage 
                       [T1]{fontenc}
l.92 ...ed{ver@\Gin@driver}{\input{\Gin@driver}}{}

这是使用以下代码,使用文档中的示例:

import matplotlib.pyplot as plt
plt.rcParams.update({
    "pgf.texsystem": "pdflatex",
    "pgf.preamble": "\n".join([
         r"\usepackage[utf8x]{inputenc}",
         r"\usepackage[T1]{fontenc}",
         r"\usepackage{cmbright}",
    ]),
})

fig, ax = plt.subplots(figsize=(4.5, 2.5))

ax.plot(range(5))

ax.text(0.5, 3., "serif", family="serif")
ax.text(0.5, 2., "monospace", family="monospace")
ax.text(2.5, 2., "sans-serif", family="sans-serif")
ax.set_xlabel(r"µ is not $\mu$")

fig.tight_layout(pad=.5)

fig.savefig('/user/home/test.pdf', backend='pgf')

The last line is how I'm trying to save the file, which is the only line not in the docs' example, but it says above that this should work. The other methods it gives to use the backend don't work either.


升级到 TexLive 2022 解决了这个问题。


TeX Live 2013 听起来很老

@samcarter_is_at_topanswers.xyz 谢谢 - 是的,升级到 2022 版本似乎解决了这个问题。

使用闪亮的新 texlive 2022 版本,您不应该再使用\usepackage[utf8x]{inputenc}. 要么将其更改为\usepackage[utf8]{inputenc}(no x) 要么完全删除它,它现在已经成为默认值好几年了。

...唯一的用途\usepackage[T1]{fontenc}是使用 pdflatex 进行编译。如果使用 lualatex 编译,请勿使用它