0

I am creating the plots and figures for my thesis using matplotlib and save them as .pgf files that I include using

\begin{figure}
    \input{myfigure.pgf}
    \caption{A caption}
\end{figure}

which by default typesets all math and labels in the figure using the same fonts as in the document. But I want my plots and figures to use sans serif fonts while using palatino as the main document font.

So far I tried the following: Add mpl.rcParams["font.family"] = "sans-serif" to the plot generation code to get all the non-math text typeset in a sans serif font and to get the math in a sans serif font I used the sansmath package as follows:

% preamble
\usepackage{sansmath}
% more preamble
\begin{document}

\begin{figure}
    \begin{sansmath}
    \input{myfigure.pgf}
    \end{sansmath}
    \caption{A caption}
\end{figure}

\end{document}

This works well for all labels and math in the figure except for greek letters. Using the option \usepackage[eulergreek]{sansmath} also doesn't help.

Does anyone have a good solution to include pgf files with all sans-serif fonts? Preferably a solution that does not require changing the matplotlib rcParams, because I might want to reuse the figures elsewhere with different formatting.

0

1 Answer 1

0

Okay, I found a solution that works well enough. Basically, the main problem was that the math fonts don't obey the surrounding font family. I.e. math that is in a paragraph typeset in sans-serif font is not typeset in sans-serif. So one needs to make sure, that math is also properly typeset in a sans family font, by adding the following to the preamble:

\DeclareMathVersion{sans}
\SetSymbolFont{operators}{sans}{OT1}{kurierm}{m}{n}
\SetSymbolFont{letters}{sans}{OML}{kurier}{m}{it}
\SetSymbolFont{symbols}{sans}{OMS}{kurier}{m}{n}
\SetMathAlphabet{\mathit}{sans}{OT1}{kurier}{m}{sl}
\SetMathAlphabet{\mathbf}{sans}{OT1}{kurier}{bx}{n}
\SetMathAlphabet{\mathtt}{sans}{OT1}{kurier}{m}{n}
\SetSymbolFont{largesymbols}{sans}{OMX}{kurier}{m}{n}

\DeclareMathVersion{boldsans}
\SetSymbolFont{operators}{boldsans}{OT1}{kurierm}{b}{n}
\SetSymbolFont{letters}{boldsans}{OML}{kurier}{b}{it}
\SetSymbolFont{symbols}{boldsans}{OMS}{kurier}{b}{n}
\SetMathAlphabet{\mathit}{boldsans}{OT1}{kurier}{b}{sl}
\SetMathAlphabet{\mathbf}{boldsans}{OT1}{kurier}{bx}{n}
\SetMathAlphabet{\mathtt}{boldsans}{OT1}{kurier}{b}{n}
\SetSymbolFont{largesymbols}{boldsans}{OMX}{kurier}{bx}{n}

\newif\IfInSansMode
\let\oldsf\sffamily
\renewcommand*{\sffamily}{\oldsf\mathversion{sans}\InSansModetrue}
\let\oldmd\mdseries
\renewcommand*{\mdseries}{\oldmd\IfInSansMode\mathversion{sans}\fi\relax}
\let\oldbf\bfseries
\renewcommand*{\bfseries}{\oldbf\IfInSansMode\mathversion{boldsans}\else%
   \mathversion{bold}\fi\relax}
\let\oldnorm\normalfont
\renewcommand*{\normalfont}{\oldnorm\InSansModefalse\mathversion{normal}}
\let\oldrm\rmfamily
\renewcommand*{\rmfamily}{\oldrm\InSansModefalse\mathversion{normal}}

and keeping the mpl.rcParams["font.family"] = "sans-serif" to make the plots all sans-serif.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .