Latex style issues using amsmath and sfmath to mark plots

I have a question related to using TeX in python.

I have the following packages included:

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 found in the previous question .

However, now I can also use the amsmath package fonts. When I include it in params, it does not respond. All I need amsmath for is to designate the x-axis of the graph with "a".

So, to show you what I have:

enter image description here

and what i want (regarding x-tag):

enter image description here

Note that to create a second image, I changed sfmath to amsmath . This will instantly ruin the x- and y-ticks. This is what I do not want.

Is it possible to change the font style of a single letter / word to amsmath style? That way, I could use this font style only when specifying the x-mark of my figure.

Another approach is to replace sfmath with amsmath with params and make sure the ticks look like the first image.

thanks

On a side note, numbers were created using:

 fig = plt.figure() ax1 = fig.add_subplot(1, 1, 1) ax1.set_xlabel(r"$a$", fontsize = 14) plt.show() 
+6
source share
3 answers

We can achieve exactly what you are looking for using our second approach: use amsmath and then change the ticklabels to a different font. This is more of a hack than the other answers listed here, but it most accurately does what you want.

The trick is to use amsmath , and let Matplotlib display everything as usual, and then at the end reset ticks the labels with the original sans-serif font. I'm not sure if this completely undermines usetex , but if so, then you have a strange combination of TeX and Matplotlib processed tags. It looks great!

An important piece of code is

 fontProperties = {'family':'sans-serif', 'weight': 'normal', 'size': 12} ax1.set_xticklabels(ax1.get_xticks(), fontProperties) ax1.set_yticklabels(ax1.get_yticks(), fontProperties) 

Adding this to the end of your script and adding amsmath will give you the following:

 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{amsmath}']} plt.rcParams.update(params) fig = plt.figure(figsize=(8,6)) ax1 = fig.add_subplot(1, 1, 1) ax1.set_xlabel(r"$a$", fontsize = 14) fontProperties = {'family':'sans-serif', 'weight': 'normal', 'size': 12} ax1.set_xticklabels(ax1.get_xticks(), fontProperties) ax1.set_yticklabels(ax1.get_yticks(), fontProperties) 

Figure

+8
source

The sfmath package has the sfmath option, which saves the original font, which will be used for mathematics in italics (which is the most mathematical in LaTeX). Using this option with $\mathit{a}$ gives you something close to what you want, although it’s not perfect, because the math font is not quite the same as the amsmath .

 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

+1
source

I thought that everything that was not placed in $ ... $ is displayed as plain text. Then we could simply define the font of the text and the mathematical font. However, label shortcuts depend on the LaTeX font, just like in your question. This is strange, matplotlib somehow wraps all the label labels in a mathematical environment. Therefore, the main problem here is that label marks as well as axis labels are displayed with the same fonts. Now we can crack it using several mathematical alphabets. Then you have to use something like \mathrm{} everywhere.

However, my assumption is that the original problem is that you do not like the label labels in Computer Modern font. So my suggestion is to change the overall font to something that looks great for label labels as well as axis labels. I found Linux Libertine to be such a font. I am not 100% sure about how matplotlib wraps texts, so I recommend defining the text as well as the mathematical font, combining well with each other. This can be achieved with the newtx LaTeX package. Using lualatex preamble can be clean enough to achieve what we want, and we use a modern tool. According to http://matplotlib.org/users/pgf.html , LuaLaTeX is officially supported for creating PDF data via the PGF backend. So this is the code I suggest:

 import matplotlib as mpl mpl.use('pgf') import matplotlib.pyplot as plt pgf_with_lualatex = { "text.usetex": True, "pgf.rcfonts": False, # Do not set up fonts from rc parameters. "pgf.texsystem": "lualatex", "pgf.preamble": [ r'\usepackage[libertine]{newtxmath}', r'\usepackage[no-math]{fontspec}', r'\usepackage{libertine}', ] } mpl.rcParams.update(pgf_with_lualatex) fig = plt.figure() ax1 = fig.add_subplot(1, 1, 1) ax1.set_xlabel("$abc$", fontsize = 14) plt.savefig('figure.pdf') 

We take absolute control of the fonts by setting rcfonts to False and setting the text and math fonts in the LaTeX preamble. The no-math parameter for the fontspec package requires newtxmath set the math font.

The generated PDF looks good, this is a screenshot:

enter image description here

0
source

Source: https://habr.com/ru/post/971169/


All Articles