当尝试将 matplotlib 图保存为 pgf 时,我收到错误
Unicode character \u2212 (U+2212) not set up for use with LaTeX.
我知道这个错误来自需要将 Unicode 字符声明为 -
,并添加了以下行来更新我的乳胶前言:
mpl.rcParams['text.latex.preamble'] = r'\usepackage{amsmath} \usepackage[utf8]{inputenc}\DeclareUnicodeCharacter{2212}{-}'
按照其他答案和生成的 pgf 文件开头的建议。
我可以通过在更新后打印它来验证它是否存储在
rcparams
中,但是我仍然收到错误。当检查生成的 pgf 文件是否有效(没有负值)时,pgf 文件的开头是:
%% Creator: Matplotlib, PGF backend
%%
%% To include the figure in your LaTeX document, write
%% \input{<filename>.pgf}
%%
%% Make sure the required packages are loaded in your preamble
%% \usepackage{pgf}
%%
%% and, on pdftex
%% \usepackage[utf8]{inputenc}\DeclareUnicodeCharacter{2212}{-}
%%
%% or, on luatex and xetex
%% \usepackage{unicode-math}
%%
%% Figures using additional raster images can only be included by \input if
%% they are in the same directory as the main LaTeX file. For loading figures
%% from other directories you can use the `import` package
%% \usepackage{import}
%%
%% and then include the figures with
%% \import{<path to file>}{<filename>.pgf}
%%
%% Matplotlib used the following preamble
%%
\begingroup%
我在文件的其余部分找不到插入的序言,我想知道为什么会这样。 我在 Windows 上将 TexLive 与 pdflatex 一起使用,对于不包含该特定 unicode 字符的任何其他图,它工作得很好。任何帮助将不胜感激。
产生错误的示例代码:
fig = plt.figure(figsize=[5, 4])
ax = plt.gca()
ax.plot([0.1, 0.2], [9.76, -4.02])
plt.savefig("plots/test.pgf", backend="pgf")