簡體   English   中英

在LateX中進行編譯時,使用matplotlib + pgf后端獲取半透明文本

[英]Getting semi-transparent text with matplotlib+pgf backend when compiling in LateX

因此,我在matplotlib中使用了pgf backend ,以在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樣式呢?

多虧了@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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM