import numpy as np
import matplotlib
matplotlib.rcParams['text.usetex'] = True
import matplotlib.pyplot as plt
t = np.linspace(0.0, 1.0, 100)
s = np.cos(4 * np.pi * t) + 2
fig, ax = plt.subplots(figsize=(6, 4), tight_layout=True)
ax.plot(t, s)
ax.set_xlabel(r'\textbf{time (s)}')
ax.set_ylabel('\itshape {Velocity (\N{DEGREE SIGN}/sec)}', fontsize=16)
ax.set_title(r'\TeX\ is Number $\displaystyle\sum_{n=1}^\infty'
r'\frac{-e^{i\pi}}{2^n}$!', fontsize=16, color='r')
plt.show()
这是 Matplotlib 网站上的 Tex 示例。但是,我收到了这条信息untimeError:
latex was not able to process the following string:
'\\\\itshape {Velocity (\\\\N{DEGREE SIGN}/sec)}'
以下是 Latex 生成的完整报告:
This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015/Debian) (preloaded format=latex)
restricted \write18 enabled.
entering extended mode
(/home/joshua/.cache/matplotlib/tex.cache/88a38ed565fe9aac24ebc44c9a0be443.tex
LaTeX2e <2016/02/01>
Babel <3.9q> and hyphenation patterns for 81 language(s) loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/type1cm/type1cm.sty)
(/usr/share/texlive/texmf-dist/tex/latex/base/textcomp.sty
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1enc.def))
(/usr/share/texlive/texmf-dist/tex/latex/ucs/ucs.sty
(/usr/share/texlive/texmf-dist/tex/latex/ucs/data/uni-global.def))
(/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty
(/usr/share/texlive/texmf-dist/tex/latex/ucs/utf8x.def))
(/usr/share/texlive/texmf-dist/tex/latex/geometry/geometry.sty
(/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifpdf.sty)
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifvtex.sty)
(/usr/share/texlive/texmf-dist/tex/generic/ifxetex/ifxetex.sty)
Package geometry Warning: Over-specification in `h'-direction.
`width' (5058.9pt) is ignored.
Package geometry Warning: Over-specification in `v'-direction.
`height' (5058.9pt) is ignored.
) (./88a38ed565fe9aac24ebc44c9a0be443.aux)
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1cmr.fd)
(/usr/share/texlive/texmf-dist/tex/latex/ucs/ucsencs.def)
*geometry* driver: auto-detecting
*geometry* detected driver: dvips
LaTeX Font Warning: Font shape `OT1/cmss/m/it' in size <16> not available
(Font) Font shape `OT1/cmss/m/sl' tried instead on input line 15.
! Undefined control sequence.
l.15 ...0.000000}{\sffamily \itshape {Velocity (\N
{DEGREE SIGN}/sec)}}
No pages of output.
Transcript written on 88a38ed565fe9aac24ebc44c9a0be443.log.ge over and over.
有谁可以解决这个问题吗?
答案1
python 尝试处理的方式\N{DEGREE SIGN}
(尝试放入 unicode 度数符号)与 TeX 接收的方式之间存在问题。对于已丢失的 matplotlib 上的旧版 python 来说,它可能有效。通常,您最好采用 TeX 方法并使用 而$\circ$
不是\N{DEGREE SIGN}
,使该行ax.set_ylabel(r'\itshape {Velocity ($\circ$/sec)}', fontsize=16)
。请注意那里的开关,r
在标签命令的开头添加 ,即将该行视为原始字符串,而不是尝试将反斜杠视为 python 转义符(错误消息来自此)。
当前的 matplotlib LaTeX 示例也与您的不同,它读取
import numpy as np
import matplotlib.pyplot as plt
# Example data
t = np.arange(0.0, 1.0 + 0.01, 0.01)
s = np.cos(4 * np.pi * t) + 2
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.plot(t, s)
plt.xlabel(r'\textbf{time} (s)')
plt.ylabel(r'\textit{voltage} (mV)',fontsize=16)
plt.title(r"\TeX\ is Number "
r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
fontsize=16, color='gray')
# Make room for the ridiculously large title.
plt.subplots_adjust(top=0.8)
plt.savefig('tex_demo')
plt.show()