在matplotlib w / pgf后端关闭自动转义

时间:2018-03-27 09:24:10

标签: python matplotlib latex

我找不到答案。

我正在使用matplotlib渲染乳胶文档的图。拥有最好的"外观"我使用PGF后端,它对我所做的大部分工作非常有效。但是,目前我无法生成我的一个数字,因为PGF后端似乎是自动转义的字符。

我的图是使用此设置生成的:

import matplotlib as mpl
mpl.use('pgf')              # PGF backend

pgf_with_latex = {                      # setup matplotlib to use latex for output
    "text.usetex"  : True,              # use LaTeX to write all text
    "pgf.texsystem": "lualatex",        # change this if using xetex or lautex
    "font.family"  : "serif",
    "font.serif"   : [],                # blank entries should cause plots to inherit fonts from the document
    "font.sans-serif": [],
    "font.monospace": [],
    "axes.labelsize": 10,               # LaTeX default is 10pt font.
    "font.size": 10,
    "legend.fontsize": 8,               # Make the legend/label fonts a little smaller
    "xtick.labelsize": 8,
    "ytick.labelsize": 8,
    "figure.figsize": figsize(),        # default fig size of 0.9 textwidth
    "pgf.preamble": [
      r"\usepackage[utf8x]{inputenc}",    # use utf8 fonts becasue your computer can handle it :)
      r"\usepackage[T1]{fontenc}",        # plots will be generated using this preamble
      r"\usepackage{amsmath}",            # math stuff
      r"\usepackage{siunitx}",            # SI units
      r"\usepackage{mhchem}",             # chemical elements
    ],
"text.latex.unicode" : True
}
mpl.rcParams.update(pgf_with_latex)

我用这种方式绘图:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)
x = np.linspace(0, 2*np.pi, 100)
ax.plot(x, np.sin(x))
ax.set_ylabel(r'not working~~[\SI{1e10}{\m^{-2}}]')

sax = ax.twinx()
sax.set_ylabel(r'working~~[\SI{e12}{\m}$^{-2}$]')
plt.savefig('test.pgf')

使用Lualatex作为解释器我导入如下图:

\begin{figure}[t]
   \centering
   \import{}{test.pgf}
   \caption{bla.}
\end{figure}

Latex文档具有与matplotlib前导码相同的包。现在我得到的错误信息是:

LaTeX Warning: Command \^ invalid in math mode on input line xxx.

! Please use \mathaccent for accents in math mode.
\MT@orig@add@accent ...ctor \spacefactor }\accent 
                                              #1 #2\egroup \spacefactor ...
l.xxx ...electfont not working~~[\SI{1e10}{\m\^{-2}}
                                              ]}%

据我所知,问题在于逃避了。这似乎是PGF后端特有的问题,因为我使用类似的设置(在这种情况下不同的rcparams)生成了PDF等。

有谁知道,问题出在哪里?我错过了配置吗?

提前感谢您提供任何帮助

0 个答案:

没有答案
相关问题