摘要
我正在导出matplotlib图形作为PGF在LaTeX中使用。
当将图形保存为PGF时,matplotlib似乎向每个文本条目(轴标签、蜱、图例条目、注释)添加了一个\sffamily。这将阻止它正确地从全局文档字体继承字体。
如果文本来自同一家族,则可以从全局文档字体继承字体,但如果全局字体来自不同的家族,则该字体将恢复为默认的浮雕字体。
我去的地方
我认为我已经隔离了这个问题:如果我编辑PGF文档,只需删除任何文本条目的\sffamily部分,问题就不再存在,全局字体也会被继承。删除不会阻止LaTeX正确编译它,我也没有错误。
由于上述发现,我认为这个问题与rcParams或任何LaTeX序言无关(无论是在python中还是在实际的LaTeX文档中)。
米维
我只是在最简单的情节下试了一下,并能重现这个问题:
import matplotlib.pyplot as plt
fig = plt.figure()
plt.xlabel('a label')
fig.savefig('fig.pgf')pgf文档将包含以下一行:
\pgftext[x=3.280000in,y=0.240809in,,top]{\color{textcolor}\sffamily\fontsize{10.000000}{12.000000}\selectfont a label}%因此,添加了\sffamily。在LaTeX中呈现这一点将强制使用无衬线字体。删除\sffamily并呈现它将允许它继承字体系列。
TLDR
是否有办法避免在matplotlib的PGF输出中包含字体系列?
发布于 2019-05-24 05:38:06
我不能提供一个解决方案,而是在@samcarter的评论基础上构建一个解决方案:您可以在本地重新定义\sffamily,例如:
\documentclass{article}
\usepackage{pgf}
\usepackage{fontspec}
\setmainfont{DejaVu Serif}
\setsansfont{DejaVu Sans}
\setmonofont{DejaVu Sans Mono}
\begin{document}
Lorem ipsum {\sffamily Lorem ipsum}
\begin{center}
\renewcommand\sffamily{}
\input{fig.pgf}
\end{center}
Lorem ipsum {\sffamily Lorem ipsum}
\end{document}而不是center,您可以使用任何环境或\begingroup和\endgroup。
发布于 2019-05-24 05:47:17
在https://matplotlib.org/users/pgf.html#font-specification上构建您可以使用:
import matplotlib as mpl
import matplotlib.pyplot as plt
pgf_with_rc_fonts = {
"font.family": "serif",
}
mpl.rcParams.update(pgf_with_rc_fonts)
fig = plt.figure()
plt.xlabel('a label')
fig.savefig('fig.pgf')以这种方式使用\rmfamily而不是\sffamily。
发布于 2020-01-21 15:43:35
还有另一个解决办法,在导入tex-文档中的pgf文件之前,用sed替换字体规范。
\documentclass{article}
\usepackage{pgf}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepackage{filecontents}
\begin{document}
\begin{figure}
\begin{filecontents*}{tmpfile.sed}
# sed command file
s/\\color{textcolor}\\sffamily\\fontsize{.*}{.*}\\selectfont //\end{filecontents*}
\immediate\write18{sed -i -f tmpfile.sed yourplot.pgf}
\import{yourplot.pgf}
\end{figure}
\end{document}https://stackoverflow.com/questions/56280224
复制相似问题