11

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} 
\begin{equation}
a^2 + b^2 = c^2
\end{equation}

\end{document}

and gives the following output:

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

5
  • Try to shift \rmfamily\setmathfont{Latin Modern Math} after the figure environment. Does it helps? Commented Aug 3, 2012 at 6:20
  • 4
    If you want to switch between math versions you should better declare a \mathversion. Imho the newest unicode-math contains the necessary code. Commented Aug 3, 2012 at 6:26
  • 1
    You can't use TeX Gyre Heros as math font, because it lacks the basic math functionalities.
    – egreg
    Commented Mar 2, 2013 at 21:37
  • @UlrikeFischer: We need an answer ;-) Commented Mar 3, 2013 at 14:33
  • 1
    @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. Commented Mar 3, 2013 at 17:21

1 Answer 1

3

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.

Update. I have added the subdued option to mathastext to prevent errors in processing in newer versions.

Sample output

\documentclass{article}

\usepackage{pgfplots}\usepackage[no-math]{fontspec}
\usepackage{lmodern}
\usepackage[LGRgreek,subdued]{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.
\begin{equation}
  a^2 + b^2 = c^2
\end{equation}

\end{document}
3
  • you might have been the first ever non-sponsored user of \MTversion command, besides the package author ;-).
    – user4686
    Commented Oct 29, 2015 at 22:37
  • extra info for people who are debugging why this ain't gonna work -- a simplest way to get this working for me with lualatex was to load fontspec with no-math, then set the font, and then load mathastext.
    – exa
    Commented Jun 16 at 11:43
  • 1
    @exa Thanks. That is what my code was doing anyway, but an extra is now needed for the mathastext package. Code and output updated. Commented Jun 16 at 14:51

You must log in to answer this question.

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