O2_AC
O2_AC

Reputation: 121

matplotlib fonts not matching LaTeX fonts

I'm currently starting to write up my PhD thesis and am now in the middle of setting up everything. One of the objectives is to use the same font in schemes and figures as in the text. However, when selecting bold text in matplotlib the fonts are not matching the latex font.

At the moment I am using these rc_params:

new_rc_params = {'text.usetex': True,
         'svg.fonttype': 'none',
         'text.latex.preamble': r'\usepackage{libertine}',
         'font.size': 10,
         'font.family': 'Linux Libertine',
         'mathtext.fontset': 'custom',
         'mathtext.rm': 'libertine',
         'mathtext.it': 'libertine:italic',
         'mathtext.bf': 'libertine:bold'
         }

mpl.rcParams.update(new_rc_params)

This will lead to the right font in normal text as far as I can tell.
Matplotlib:
matplotlib1

LaTeX comparison:
latex-comparison1

If I now, however, select bold text in matplotlib via:

$\mathbf{TS_{I,II}}$

I get the following output:
matplotlib2

For comparison the bold and normal pure LaTeX output:
latex-comparison2

Now, I have read that for better math support in LaTeX I should add \usepackage[libertine]{nextxmath} to text.latex.preamble in rc_params. If I do this, matplotlib generates the following output:

matlplotlib3

Which still is not the same font as I get from my LaTeX document. Does anyone have any ideas what I could additionally change to make the fonts identical?

(Can't post inline images...)

Upvotes: 2

Views: 1016

Answers (1)

O2_AC
O2_AC

Reputation: 121

I figured it out, with the help of the comment from @Patol75

So it is indeed rather a "pythonic" problem. For some reason if you try to use:

$\textbf{...}$

in a matplotlib label, it gets rendered like the following:

matplotlib4

For some reason that I did not look into further, LaTeX commands starting with \b... spit out an error. Which prevented the easy use of \bm. However, if one uses an r' in front of the command everything works fine. Therefore, one can use:

r'\textbf{TS$_{\textbf{I,II}}$}'

This produces the correct/expected output:
matplotlib5

Upvotes: 0

Related Questions