I have a question related to using TeX within python.
I have the following packages enabled:
import numpy
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rc('text', usetex = True)
matplotlib.rc('font', **{'family' : "sans-serif"})
params = {'text.latex.preamble' : [r'\usepackage{siunitx}', r'\usepackage{sfmath}']}
plt.rcParams.update(params)
The reason for this can be looked up in a previous question of mine.
However, I would now also be able to use the fonts of the amsmath
package. When I include it in params, it does not respond. All I need amsmath
for is to label the x-axis of a plot with "a".
So to show you what I have:
and what I want (regarding the x-label):
Please note that to produce the second image, I changed sfmath
into amsmath
. This immediatly messes up the x- and y-ticks. This is something I do not want to happen.
Is it perhaps possible to change the font style of a single letter/word to that of amsmath
? This way I would be able to only use that font style when indicating the x-label of my figure.
A different approach would be to replace sfmath
by amsmath
in params
and make sure the ticks look like the first image.
Thanks
On a side note, the figures were created using:
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
ax1.set_xlabel(r"$a$", fontsize = 14)
plt.show()
The sfmath
package has an option mathitOrig
which retains the original font that would be used for math in italics (which is most math in LaTeX). Using this option along with $\mathit{a}$
gives you something close to what you want, though it's not perfect, as the math font is not quite the same as the amsmath
font.
import numpy
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rc('text', usetex = True)
matplotlib.rc('font', **{'family' : "sans-serif"})
params = {'text.latex.preamble' : [r'\usepackage{siunitx}', r'\usepackage[mathitOrig]{sfmath}']}
plt.rcParams.update(params)
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
ax1.set_xlabel(r"$\mathit{a}$", fontsize = 14)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With