# Using some packages adds a vertical offset when using LaTeX to render text in matplotlib/python figures

I was advised to post here in the comments on a post I made on Stack Overflow.

I'm using LaTeX to render my text in figures in the Python package matplotlib. My problem was that when I added siunitx to my LaTeX preamble, some elements of text were displaced vertically. I've solved that issue for myself by reverting to a previous version of siunitx, but I don't know the cause of the initial problem. It also occurs when using the package color.

The same problem wasn't reproduced by everybody: I'm using python 3.8.12, matplotlib 3.3.2 and siunitx 3.0.32 (installed via MiKTeX) on Windows 10.

I was asked to upload the files in the .matplotlib\tex.cache folder here, in case it helps to debug the problem, which I have done below.

Incorrect alignment by using siunitx

Python script

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()


Image produced

.matplotlib\tex.cache files

\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}
\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}


and

\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}
\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 files)

Correct alignment by forcing version 2

Python script

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()


Image produced

.matplotlib\tex.cache files

\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]
\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}


and

\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]
\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 files)

Edit

Posting pngs created from both working and non working dvi files. Both look the same. (Images are larger than the text, but are white against the white Stack Exchange background).

Working (siunitx version 2)

Not working (siunitx version 3)

• could you compile one of the broken files with latex and then call dvipng ? How does the png look? Is it different to the png of the other version? Oct 20, 2021 at 15:03
• Welcome to TeX.SE! Oct 20, 2021 at 15:04
• @UlrikeFischer Hello again, thanks for your help! In both cases they look the same: a rectangle with bottom-right aligned text. I'll add the images to the bottom of the question so you can see them. Oct 20, 2021 at 15:17
• @TomVincentUK, I have tried in chat and I have (I think) the same as you. Puzzling. chat.stackexchange.com/transcript/message/59406419#59406419 Oct 20, 2021 at 15:22
• @UlrikeFischer I'm afraid I don't know, sorry. I'll have a bit of a dig through the matplotlib source, but I can't promise I'll find anything. Oct 20, 2021 at 15:55