在 LateX 中编译时使用 matplotlib+pgf 后端获取半透明文本

詹杜特

所以我使用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)

文本看起来没有变化,如果我尝试在编译中使用\textcolorxcolor 包(或 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 样式怎么办...

詹杜特

感谢@ImportanceOfBeingErnest 我终于让它与 pgf 后端一起工作:

import matplotlib
matplotlib.use('pgf')

import matplotlib.pyplot as plt

pgf_with_custom_preamble = {
    "text.usetex": True,    # use inline math for ticks
    "pgf.rcfonts": False,   # don't setup fonts from rc parameters
    "pgf.preamble": [
                     "\\usepackage[dvipsnames]{xcolor}",         # load additional packages
                     ]
}
matplotlib.rcParams.update(pgf_with_custom_preamble)

plt.figure()
plt.text(0.0,0.5,r'\textcolor{gray}{Some text compiled in latex \cite{my_biblio_tag}}')
plt.savefig("myfig.pgf", bbox_inches="tight", pad_inches=0)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Matplotlib pgf后端是否支持透明性?

来自分类Dev

将matplotlib图形与LaTeX中的文本对齐

来自分类Dev

将matplotlib图形与LaTeX中的文本对齐

来自分类Dev

matplotlib:在文本框中使用LaTeX时覆盖字符串中的下划线

来自分类Dev

matplotlib pgf:OSError:subprocess.py中没有这样的文件或目录

来自分类Dev

在matplotlib文本框中对齐LaTeX数学文本

来自分类Dev

LaTeX无法处理matplotlib文本

来自分类Dev

使用LaTeX使用任意字符串的Matplotlib文本渲染

来自分类Dev

使用\:LaTeX水平间距时matplotlib中的ValueError

来自分类Dev

matplotlib中的非斜体Latex字体

来自分类Dev

更改 matplotlib 中的 Latex 数学字体样式

来自分类Dev

Matplotlib中连字符或减号的使用与Latex的兼容性

来自分类Dev

pgf 和 pstricks 的不透明度未出现在 .ps 和 .pdf 文件中

来自分类Dev

Latex Tikz / Pgf用参数声明形状,并用数字声明锚点

来自分类Dev

这些工具有什么问题:texlive-latex-base texlive-latex-recommended latex-xcolor pgf latex-beamer繁荣texlive tipa?

来自分类Dev

这些工具有什么问题:texlive-latex-base texlive-latex-recommended latex-xcolor pgf latex-beamer繁荣的texlive tipa?

来自分类Dev

如何在matplotlib中显示LaTeX f字符串

来自分类Dev

如何使用LaTeX括号在Matplotlib中标记轴?

来自分类Dev

尝试使用matplotlib Latex保存后记时出错

来自分类Dev

Matplotlib-在LateX表达式中使用变量

来自分类Dev

如何使用LaTeX括号在Matplotlib中标记轴?

来自分类Dev

尝试使用matplotlib Latex保存后记时出错

来自分类Dev

使用sed从LaTeX文件中删除文本

来自分类常见问题

Matplotlib字体与LaTeX共同的字体

来自分类Dev

在输入双击时隐藏透明文本

来自分类Dev

在emacs中从LaTeX编译pdf

来自分类Dev

LaTeX在Verbatim环境中的文本颜色

来自分类Dev

避免文本在Latex中的框外溢出

来自分类Dev

在Matlab中调整图形LaTeX文本大小

Related 相关文章

  1. 1

    Matplotlib pgf后端是否支持透明性?

  2. 2

    将matplotlib图形与LaTeX中的文本对齐

  3. 3

    将matplotlib图形与LaTeX中的文本对齐

  4. 4

    matplotlib:在文本框中使用LaTeX时覆盖字符串中的下划线

  5. 5

    matplotlib pgf:OSError:subprocess.py中没有这样的文件或目录

  6. 6

    在matplotlib文本框中对齐LaTeX数学文本

  7. 7

    LaTeX无法处理matplotlib文本

  8. 8

    使用LaTeX使用任意字符串的Matplotlib文本渲染

  9. 9

    使用\:LaTeX水平间距时matplotlib中的ValueError

  10. 10

    matplotlib中的非斜体Latex字体

  11. 11

    更改 matplotlib 中的 Latex 数学字体样式

  12. 12

    Matplotlib中连字符或减号的使用与Latex的兼容性

  13. 13

    pgf 和 pstricks 的不透明度未出现在 .ps 和 .pdf 文件中

  14. 14

    Latex Tikz / Pgf用参数声明形状,并用数字声明锚点

  15. 15

    这些工具有什么问题:texlive-latex-base texlive-latex-recommended latex-xcolor pgf latex-beamer繁荣texlive tipa?

  16. 16

    这些工具有什么问题:texlive-latex-base texlive-latex-recommended latex-xcolor pgf latex-beamer繁荣的texlive tipa?

  17. 17

    如何在matplotlib中显示LaTeX f字符串

  18. 18

    如何使用LaTeX括号在Matplotlib中标记轴?

  19. 19

    尝试使用matplotlib Latex保存后记时出错

  20. 20

    Matplotlib-在LateX表达式中使用变量

  21. 21

    如何使用LaTeX括号在Matplotlib中标记轴?

  22. 22

    尝试使用matplotlib Latex保存后记时出错

  23. 23

    使用sed从LaTeX文件中删除文本

  24. 24

    Matplotlib字体与LaTeX共同的字体

  25. 25

    在输入双击时隐藏透明文本

  26. 26

    在emacs中从LaTeX编译pdf

  27. 27

    LaTeX在Verbatim环境中的文本颜色

  28. 28

    避免文本在Latex中的框外溢出

  29. 29

    在Matlab中调整图形LaTeX文本大小

热门标签

归档