为什么使用graphicx packag的dvips选项调用pdflatex时reflectbox不工作

2024-05-19 00:21:02 发布

您现在位置:Python中文网/ 问答频道 /正文

概述

我想生成一个包含latex样式翻转字符的pdf图像,matplotlib与graphicx包关联。 下面是我的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编译的同一个latex命令工作正常,并给出了一个水平翻转字符。显然matplotlib通过调用dvi latex编译器来生成latex样式,该编译器强制对graphicx包使用[dvips]选项。在

实施测试

我直接在latex中尝试了相同的语法,发现graphicx包的[dvips]选项禁用了\reflectbox命令的翻转行为。在

此代码适用于pdflatex编译器:

^{pr2}$

但是这段代码不适用于同一个编译器:

\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文件中?我知道通过latex生成.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')

但是这个解决方案看起来不像是一个健壮的解决方案。。。在

提前感谢您的解释!在


Tags: 代码textimportpdfmatplotlibpltparamssymbol

热门问题