簡體   English   中英

為什么 siunitx LaTeX 包會為 matplotlib 圖形中的某些文本元素添加垂直偏移?

[英]Why does the siunitx LaTeX package add a vertical offset to some text elements in matplotlib figures?

我正在使用 LaTeX 在我的 matplotlib 圖形中渲染文本。 當我將包 siunitx 添加到我的 LaTeX 序言中時,某些文本元素不再正確垂直對齊。

我最近才遇到這個問題:以前正確渲染的數字現在壞了。 它影響多個python環境,所以我相信這可能是由於siunitx的最新版本?

這是我的最低工作示例:

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

# Correctly behaving text
fig0, ax0 = plt.subplots()
ax0.plot(x, y, label='some string')
ax0.legend()
fig0.tight_layout()
fig0.savefig('without_siunitx.png')
plt.show()

# Use siunitx
plt.rcParams['text.latex.preamble'] = '\n'.join([
    r'\usepackage{siunitx}',
])

# Now some text has incorrect y position
fig1, ax1 = plt.subplots()
ax1.plot(x, y, label='some string')
ax1.legend()
fig1.tight_layout()
fig1.savefig('with_siunitx.png')
plt.show()

對於最小工作示例,我在 Windows 10 上使用 python 3.8.12、matplotlib 3.3.2 和 siunitx 3.0.32(通過 MiKTeX 安裝)。

生成的兩個 png 圖像鏈接如下:

沒有 siunitx 的正確對齊的圖形

與 siunitx 不正確對齊的圖形

您可以看到使用 siunitx 后圖例文本和 y 軸刻度標簽未對齊。

我將非常感謝任何能幫助我解決這個問題的人,因為它影響了我論文的大部分數字。 非常感謝!

如果之前運行正常,您可以使用以下語法回滾到 siunitx 的第 2 版:

\usepackage{siunitx}[=v2]

暫無
暫無

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

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