有人建议我在这里发表评论我在 Stack Overflow 上发表的帖子。
我正在使用 LaTeX 在 Python 包 matplotlib 中将文本渲染成图形。我的问题是,当我将 siunitx 添加到 LaTeX 序言中时,一些文本元素垂直位移。我通过恢复到 siunitx 的先前版本解决了这个问题,但我不知道最初问题的原因。使用包颜色时也会发生这种情况。
并非所有人都重现了同样的问题:我在 Windows 10 上使用 python 3.8.12、matplotlib 3.3.2 和 siunitx 3.0.32(通过 MiKTeX 安装)。
我被要求将 .matplotlib\tex.cache 文件夹中的文件上传到这里,以防它有助于调试问题,我已经在下面完成了。
感谢您的帮助!
使用 siunitx 进行不正确的对齐
Python 脚本
import numpy as np
import matplotlib.pyplot as plt
# Test data
x = np.linspace(0, 2*np.pi, 64)
y = np.sin(x)
# Use latex for text
plt.rcdefaults()
plt.rcParams['text.usetex'] = True
plt.rcParams['text.latex.preamble'] = '\n'.join([
r'\usepackage{siunitx}',
])
# Generate figure
fig, ax = plt.subplots()
ax.plot(x, y, label='some string')
ax.legend()
ax.set(xticks=[], yticks=[]) # Reduces number of text items in MWE
fig.tight_layout()
fig.savefig('siunitx_v3.png')
plt.show()
.matplotlib\tex.cache 文件
\documentclass{article}
\newcommand{\mathdefault}[1]{#1}
\usepackage{type1cm}
\usepackage{type1ec}
\usepackage{type1ec}
\usepackage{type1ec}
\usepackage[utf8]{inputenc}
\DeclareUnicodeCharacter{2212}{\ensuremath{-}}
\usepackage[papersize=72in,body=70in,margin=1in]{geometry}
\usepackage{siunitx}
\makeatletter\@ifpackageloaded{textcomp}{}{\usepackage{textcomp}}\makeatother
\pagestyle{empty}
\begin{document}
% The empty hbox ensures that a page is printed even for empty inputs, except
% when using psfrag which gets confused by it.
\fontsize{10.000000}{12.500000}%
\ifdefined\psfrag\else\hbox{}\fi%
{\sffamily lp}
\end{document}
和
\documentclass{article}
\newcommand{\mathdefault}[1]{#1}
\usepackage{type1cm}
\usepackage{type1ec}
\usepackage{type1ec}
\usepackage{type1ec}
\usepackage[utf8]{inputenc}
\DeclareUnicodeCharacter{2212}{\ensuremath{-}}
\usepackage[papersize=72in,body=70in,margin=1in]{geometry}
\usepackage{siunitx}
\makeatletter\@ifpackageloaded{textcomp}{}{\usepackage{textcomp}}\makeatother
\pagestyle{empty}
\begin{document}
% The empty hbox ensures that a page is printed even for empty inputs, except
% when using psfrag which gets confused by it.
\fontsize{10.000000}{12.500000}%
\ifdefined\psfrag\else\hbox{}\fi%
{\sffamily some string}
\end{document}
(+ 2 个 dvi 文件)
通过强制版本 2 来纠正对齐
Python 脚本
import numpy as np
import matplotlib.pyplot as plt
# Test data
x = np.linspace(0, 2*np.pi, 64)
y = np.sin(x)
# Use latex for text
plt.rcdefaults()
plt.rcParams['text.usetex'] = True
plt.rcParams['text.latex.preamble'] = '\n'.join([
r'\usepackage{siunitx}[=v2]',
])
# Generate figure
fig, ax = plt.subplots()
ax.plot(x, y, label='some string')
ax.legend()
ax.set(xticks=[], yticks=[]) # Reduces number of text items in MWE
fig.tight_layout()
fig.savefig('siunitx_v2.png')
plt.show()
生成的图像
.matplotlib\tex.cache 文件
\documentclass{article}
\newcommand{\mathdefault}[1]{#1}
\usepackage{type1cm}
\usepackage{type1ec}
\usepackage{type1ec}
\usepackage{type1ec}
\usepackage[utf8]{inputenc}
\DeclareUnicodeCharacter{2212}{\ensuremath{-}}
\usepackage[papersize=72in,body=70in,margin=1in]{geometry}
\usepackage{siunitx}[=v2]
\makeatletter\@ifpackageloaded{textcomp}{}{\usepackage{textcomp}}\makeatother
\pagestyle{empty}
\begin{document}
% The empty hbox ensures that a page is printed even for empty inputs, except
% when using psfrag which gets confused by it.
\fontsize{10.000000}{12.500000}%
\ifdefined\psfrag\else\hbox{}\fi%
{\sffamily lp}
\end{document}
和
\documentclass{article}
\newcommand{\mathdefault}[1]{#1}
\usepackage{type1cm}
\usepackage{type1ec}
\usepackage{type1ec}
\usepackage{type1ec}
\usepackage[utf8]{inputenc}
\DeclareUnicodeCharacter{2212}{\ensuremath{-}}
\usepackage[papersize=72in,body=70in,margin=1in]{geometry}
\usepackage{siunitx}[=v2]
\makeatletter\@ifpackageloaded{textcomp}{}{\usepackage{textcomp}}\makeatother
\pagestyle{empty}
\begin{document}
% The empty hbox ensures that a page is printed even for empty inputs, except
% when using psfrag which gets confused by it.
\fontsize{10.000000}{12.500000}%
\ifdefined\psfrag\else\hbox{}\fi%
{\sffamily some string}
\end{document}
(+ 2 个 dvi 文件)
编辑
发布由工作和非工作 dvi 文件创建的 png。两者看起来一样。(图像比文本大,但在白色 Stack Exchange 背景下是白色的)。
工作(siunitx 版本 2)
不工作(siunitx 版本 3)