Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latex font style issues using amsmath and sfmath for plot labeling

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:

enter image description here

and what I want (regarding the x-label):

enter image description here

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()
like image 738
The Dude Avatar asked Jun 22 '14 11:06

The Dude


1 Answers

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)

With matitOrig

like image 182
tbekolay Avatar answered Nov 14 '22 18:11

tbekolay