【发布时间】:2019-07-12 15:27:05
【问题描述】:
所以我使用 pgf backendin matplotlib 在我的 TeX 文档中包含一些自动编译的对我的 Tex 文档的其他部分(图、参考书目)的引用。
import matplotlib
matplotlib.use('pgf')
import matplotlib.pyplot as plt
plt.figure()
plt.txt(0.0,0.5,r'Some text compiled in latex \cite{my_bib_tag}')
plt.savefig("myfig.pgf", bbox_inches="tight", pad_inches=0)
然后在我的 tex 文档中,我有以下几行:
\usepackage[dvipsnames]{xcolor}
%yada yada yada
\begin{figure}
\input{myfig.pgf}
\end{figure}
效果很好,但是当我尝试为文本添加一些透明度时,它不起作用。比如设置时:
plt.txt(0.0,0.5,r'Some text compiled in latex \cite{my_bib_tag}', alpha=0.5)
文本看起来没有改变,如果我尝试在编译中使用 xcolor 包中的\textcolor(或 LateX 中的任何其他命令,如透明包)进行编译,我在编译 Python 时会遇到解析错误。
我尝试转义字符,但不知怎的,我无法让它工作。
plt.txt(0.0,0.5,r'\textcolor{gray}{Some text compiled in latex \cite{my_bib_tag}}', alpha=0.5)
#raise ValueError !Undefined Control Sequence
编辑 1: 我尝试在序言中添加所需的包,但在保存到 pgf(使用 pgf 后端)时它不起作用,它使用 Agg 后端(我认为它是预期行为)。但是我需要将它保存在 pgf 中才能动态解析引用。
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams["text.usetex"] = True
matplotlib.rcParams["text.latex.preamble"].append(r'\usepackage[dvipsnames]{xcolor}')
matplotlib.verbose.level = 'debug-annoying'
plt.figure()
plt.text(0.0,0.5,r'\textcolor{gray}{Some text compiled in latex \cite{my_bib_tag}}', alpha=0.5)
#works in .png, does not work in .pgf
#plt.savefig("myfig.png", bbox_inches="tight", pad_inches=0)
编辑 2: 一种解决方法是在 plt.text 中使用颜色参数,但如果我想使用更复杂的 LateX 样式怎么办...
【问题讨论】:
-
您需要通过 matplotlib rcParams 中的 tex 序言包含您想要使用的任何包。
-
哦,我不知道!谢谢马上做!
-
无法正常工作
标签: python matplotlib latex pgf