I was trying to use a custom LaTeX preamble with matplotlib, but it raises
<code>ValueError: Key text.latex.preamble: Could not convert ['\usepackage{siunitx}' ...] to str
</code>
<code>ValueError: Key text.latex.preamble: Could not convert ['\usepackage{siunitx}' ...] to str
</code>
ValueError: Key text.latex.preamble: Could not convert ['\usepackage{siunitx}' ...] to str
For some reason, the are converted into
\
, but I don’t understand why.
Here is the MWE (inspired from this post):
<code>import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['text.usetex'] = True
rcParams['text.latex.preamble'] = [
r'usepackage{siunitx}', # i need upright micro symbols, but you need...
r'sisetup{detect-all}', # ...this to force siunitx to actually use your fonts
r'usepackage{helvet}', # set the normal font here
r'usepackage{sansmath}', # load up the sansmath so that math -> helvet
r'sansmath' # <- tricky! -- gotta actually tell tex to use!
]
fig, ax = plt.subplots()
fig.show()
</code>
<code>import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['text.usetex'] = True
rcParams['text.latex.preamble'] = [
r'usepackage{siunitx}', # i need upright micro symbols, but you need...
r'sisetup{detect-all}', # ...this to force siunitx to actually use your fonts
r'usepackage{helvet}', # set the normal font here
r'usepackage{sansmath}', # load up the sansmath so that math -> helvet
r'sansmath' # <- tricky! -- gotta actually tell tex to use!
]
fig, ax = plt.subplots()
fig.show()
</code>
import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['text.usetex'] = True
rcParams['text.latex.preamble'] = [
r'usepackage{siunitx}', # i need upright micro symbols, but you need...
r'sisetup{detect-all}', # ...this to force siunitx to actually use your fonts
r'usepackage{helvet}', # set the normal font here
r'usepackage{sansmath}', # load up the sansmath so that math -> helvet
r'sansmath' # <- tricky! -- gotta actually tell tex to use!
]
fig, ax = plt.subplots()
fig.show()