# Switching math fonts and back to normal fails in figures?

For my current document I want to use a normal serif font for the main text. Most diagrams from other sources and myself are with sans serif fonts (TeX Gyre Heros). Now I want to use pgfplots to create figures using these fonts but keep the math in the main text in the normal font. Something similar has been achieved with sansmath here but this does not switch to completely different font, as I am trying here. I thought with unicode-math I can switch back and forth easily but the switching back part fails.

My MWE looks as follows:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{fontspec}
\usepackage{unicode-math}
\setmathfont{Latin Modern Math}

\begin{document}

\begin{figure}
\sffamily
\setmathfont{TeX Gyre Heros}
\setmathfont[range=\mathit/{latin, Latin, greek, Greek}]{texgyreheros-italic.otf}
\begin{tikzpicture}
\begin{axis}
\addplot [blue] coordinates { (0,0) (1,2) };
\end{axis}
\node at (4, 1) { $\Delta T_{\mathrm S}$};
\node at (2, 5) {normal sans serif text};
\end{tikzpicture}
\rmfamily
\setmathfont{Latin Modern Math}
\caption{Some caption text.}
\end{figure}

Normal text in Latin Modern Roman here but math fails unless the next line is used.
%\setmathfont{Latin Modern Math}
$$a^2 + b^2 = c^2$$

\end{document}


and gives the following output:

Thank you for any hints, why the switching back fails and comments in general how this might be improved.

• Try to shift \rmfamily\setmathfont{Latin Modern Math}  after the figure environment. Does it helps? – Claudio Fiandrino Aug 3 '12 at 6:20
• If you want to switch between math versions you should better declare a \mathversion. Imho the newest unicode-math contains the necessary code. – Ulrike Fischer Aug 3 '12 at 6:26
• You can't use TeX Gyre Heros as math font, because it lacks the basic math functionalities. – egreg Mar 2 '13 at 21:37
• @UlrikeFischer: We need an answer ;-) – Marco Daniel Mar 3 '13 at 14:33
• @MarcoDaniel: I don't have one: It is not difficult to set up a mathversion with another math font, but the use of TeX Gyre and the range part has some side effects. – Ulrike Fischer Mar 3 '13 at 17:21

As mentioned in the comments TeX Gyre Heros is not a math fonts and this is what is causing the strange errors. However, you can use the mathastext package overcome this. The following demonstrates its use with lualatex. The order in the preamble is crucial.

\documentclass{article}

\usepackage{pgfplots}\usepackage[no-math]{fontspec}
\usepackage{lmodern}
\usepackage[LGRgreek]{mathastext}
\setmainfont{TeX Gyre Heros}\Mathastext[Heros]
\setmainfont{Latin Modern Roman}

\pgfplotsset{compat=1.11}

\begin{document}

\begin{figure}
\centering
\MTversion{Heros}
\begin{tikzpicture}
\begin{axis}[font=\sffamily]
\addplot [blue] coordinates { (0,0) (1,2) };
\end{axis}
\node at (4, 1) { $\Delta T_{\mathrm S}$}; \node at (2, 5)
{normal sans serif text};
\end{tikzpicture}
\MTversion{normal}
\caption{Some caption text.}
\end{figure}

Normal text in Latin Modern Roman here but math fails unless the next
line is used.
$$a^2 + b^2 = c^2$$

\end{document}

• you might have been the first ever non-sponsored user of \MTversion command, besides the package author ;-). – user4686 Oct 29 '15 at 22:37