I'm using LaTeX to render text in my matplotlib figures. When I add the package siunitx to my LaTeX preamble, some text elements are no longer vertically aligned correctly.
I've only encountered this problem recently: figures which were previously rendered correctly are now broken. It affects multiple python environments, so I believe it may be due to a recent version of siunitx?
Here is my minimum working example:
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()
For the minimum working example I'm using python 3.8.12, matplotlib 3.3.2 and siunitx 3.0.32 (installed via MiKTeX) on Windows 10.
The two png images generated are linked below:
You can see that the legend text and the y-axis tick labels are misaligned after using siunitx.
I'll be really grateful to anybody who can help me solve this issue, as it affects most of the figures for my thesis. Thanks very much!
CodePudding user response:
If it worked correctly before, you can rollback to version 2 of siunitx with the following syntax:
\usepackage{siunitx}[=v2]