概述

我想生成一个包含乳胶样式翻转字符的pdf图像,其中matplotlib与包图形x相关联 . 这是我的python代码:

import matplotlib
params = {'backend':'pdf',
          'text.latex.preamble':['\usepackage{amsmath}',
                                 '\usepackage{graphicx}'],
          'text.usetex':True}
matplotlib.rcParams.update(params)
import matplotlib.pyplot as plt
fig = plt.figure()
axe = fig.add_subplot(111)
axe.set_xlabel('\text{\reflectbox{$\boldsymbol{\Gamma}$}}')
axe.savefig('image.pdf',bbox_inches='tight')

我得到一个非翻转字符,而使用pdflatex编译的相同乳胶命令工作正常,并提供了一个水平翻转字符 . 显然matplotlib通过调用dvi latex编译器生成乳胶样式,该编译器强制使用imagex包的[dvips]选项 .

实施测试

我在latex中直接尝试了相同的语法,并观察到package graphicx的[dvips]选项禁用了 \reflectbox 命令的翻转行为 .

此代码适用于pdflatex编译器:

\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}

\newcommand\rvrs[1]{\text{\reflectbox{$#1$}}}
\def\myusersymb{\boldsymbol{\Gamma}}
\DeclareRobustCommand{\myrvrssymb}{\rvrs{\myusersymb}}

\begin{document}
\section{symbol in text}
symbol : $\myusersymb$\\
reversed symbol : $\myrvrssymb$
\section{symbol in caption}
\begin{figure}[!h]
   \caption{symbol : $\myusersymb$}
\end{figure}
\begin{figure}[!h]
   \caption{reversed symbol : $\myrvrssymb$}
\end{figure}
\end{document}

而此代码不适用于相同的编译器:

\documentclass{article}
\usepackage{amsmath}
\usepackage[dvips]{graphicx}

\newcommand\rvrs[1]{\text{\reflectbox{$#1$}}}
\def\myusersymb{\boldsymbol{\Gamma}}
\DeclareRobustCommand{\myrvrssymb}{\rvrs{\myusersymb}}

\begin{document}
\section{symbol in text}
symbol : $\myusersymb$\\
reversed symbol : $\myrvrssymb$
\section{symbol in caption}
\begin{figure}[!h]
   \caption{symbol : $\myusersymb$}
\end{figure}
\begin{figure}[!h]
   \caption{reversed symbol : $\myrvrssymb$}
\end{figure}
\end{document}

问题

为什么此选项会影响翻转命令 \reflectbox ?是否可以直接生成该字符并将其导出为pdf文件?我知道通过乳胶生成.dvi并将其转换为pdf是可能的,但matplotlib似乎不允许这个过程 .

备注

我找到了第一个目标的工作,用于以下代码:

import os
import matplotlib
params = {'backend':'ps',
          'text.latex.preamble':['\usepackage{amsmath}',
                                 '\usepackage[dvips]{graphicx}'],
          'text.usetex':True}
matplotlib.rcParams.update(params)
import matplotlib.pyplot as plt
fig = plt.figure()
axe = fig.add_subplot(111)
axe.set_xlabel('\text{\reflectbox{$\boldsymbol{\Gamma}$}}')
axe.savefig('image.eps',bbox_inches='tight')
os.system('epstopdf image.eps')

但是这个解决方案看起来并不像一个强大的...

在此先感谢您的解释!