【问题标题】:Avoid specifying font family in PGF export of matplotlib figure避免在 matplotlib 图形的 PGF 导出中指定字体系列
【发布时间】:2019-10-10 07:45:50
【问题描述】:

总结

我正在将 matplotlib 图形导出为 PGF 以在 LaTeX 中使用。

当将图形保存为 PGF 时,matplotlib 似乎在每个文本条目(轴标签、刻度、图例条目、注释)中添加了 \sffamily。这将阻止它正确地从全局文档字体继承字体。

如果文本来自同一家族,则文本可以从全局文档字体继承字体,但如果全局字体来自不同家族,它将恢复为默认的 sffamily 字体。

我要去哪里

我相信我已经隔离了问题:如果我编辑 PGF 文档并简单地删除任何文本条目的 \sffamily 部分,问题就不再存在并且全局字体被继承。删除不会阻止 LaTeX 正确编译它,并且我没有收到任何错误。

由于上述发现,我认为问题与rcParams 或任何 LaTeX 前导码(无论是在 python 中还是在实际的 LaTeX 文档中)都无关。

MWE

我只是在最简单的情节上进行了尝试,并且能够重现该问题:

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 输出中包含字体系列?

【问题讨论】:

    标签: matplotlib latex pgf


    【解决方案1】:

    我无法提供解决方案,只能提供基于 @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}
    

    您可以使用任何环境或\begingroup\endgroup,而不是\endgroup

    【讨论】:

    • 我会避免在这种情况下使用center 环境,它会增加额外的垂直间距。
    • 至于@samcarter 的评论,我使用的是\begin{figure}
    【解决方案2】:

    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

    【讨论】:

    • 这个想法是在 PGF 文件中根本不指定字体系列,这样如果我在 LaTeX 文件中更改,图形的字体也会更改。
    • @krg 来自链接的文档:“如果 matplotlib 使用的字体配置与您的 LaTeX 文档中的字体设置不同,则导入图形中文本元素的对齐可能会关闭。检查您的标题.pgf 文件,如果您不确定用于布局的字体 matplotlib。”所以mathplotlib 需要知道你使用的字体。
    • 确实,matplotlib 需要一种字体,如果我尝试删除字体规范,它将恢复为默认的 DejaVu(由 rcParamsDefault 指定)。所以我理解为什么matplotlib 需要字体规范,但令人讨厌的是它将它强制到 pgf 文档中。不过,您的其他答案很好地解决了这个问题。
    【解决方案3】:

    还有另一种解决方法,在将 pgf 文件导入 tex 文档之前,用 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}
    

    【讨论】:

      猜你喜欢
      • 2019-07-02
      • 2022-01-08
      • 1970-01-01
      • 2012-01-13
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 2013-04-24
      • 2013-08-02
      相关资源
      最近更新 更多