[Tex/LaTex] Switching math fonts with unicode-math

unicode-math

I want to use a different math font in figures (namely tikzpictures). I already asked a question about this topic where I tried to mix mathspec and unicode-math. Now after some reseach I realized that mathspec does not allow to switch the font at all and I should use only unicode-math.

But there I also get build errors in my MWE (or should I say MnWE, because it does not work):

% !TEX TS-program = xelatex
\documentclass{article}

\usepackage{etoolbox}
\usepackage{tikz}
\usepackage{unicode-math}
\setmainfont{TeX Gyre Pagella}
\newcommand{\rmmath}{\setmathfont{TeX Gyre Pagella Math}}
\rmmath
\setsansfont[Scale=MatchUppercase]{Candara}

\newcommand{\sfmath}{ %
    \setmathfont{TeX Gyre Pagella Math} %
    \setmathfont[range=\mathup, Numbers=Lining, Scale=MatchUppercase]{Candara} %
    \setmathfont[range=\mathit, Scale=MatchUppercase]{Candara Italic} %
    \setmathfont[range=\mathbf, Scale=MatchUppercase]{Candara Bold} %
}

\BeforeBeginEnvironment{tikzpicture}{\sfmath}
\AfterEndEnvironment{tikzpicture}{\rmmath}

\begin{document}
% This would work, but I actually do not need sans math in running text
%   \sfmath $I$
%   \rmmath $I$
%   \sfmath $I$
%   \rmmath $I$
%   \sfmath $I$
%   \rmmath $I$
%   \sfmath $I$
%   \rmmath $I$

    Text $I$
    \begin{figure}
    \begin{tikzpicture}
        \node {Node: $I$};
    \end{tikzpicture}
    \caption{Caption text with math $I$}
    \end{figure}
    Text
% If you remove the following lines it will compile
    \begin{figure}
    \begin{tikzpicture}
        \node {Node: $I$};
    \end{tikzpicture}
    \caption{with math $I$}
    \end{figure}
\end{document}

Some interesting notes: if I remove the figure environment, i.e. only use tikzpicture it will build. If I use center instead of figure it also does not work. Funnily enough, if I have only one figure it will build.

The first error I get:

.................................................
. fontspec info: "defining-font"
. 
. Font family 'Candara(1)' created for font 'Candara' with options
. [BoldItalicFont={},ItalicFont={},Script=Math,SizeFeatures={{Size=10-},{Size=7
.40005-10,Font=Candara,Style=MathScript},{Size=-7.40005,Font=Candara,Style=Math
ScriptScript}},Numbers={Lining},Scale={MatchUppercase}].
. 
. This font family consists of the following shapes:
.................................................

! LaTeX Error: Symbol font `um_fam1' not defined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.43 ^^I\begin{tikzpicture}

Your command was ignored.

Best Answer

I had exactly the same problem. You can partly circumvent it by using math versions, i.e.

\setmathfont[version=default]{TeX Gyre Pagella Math} %
\setmathfont[version=sansserif, Numbers=Lining, Scale=MatchUppercase]{Candara}

and then use

\mathversion{sansserif}

to switch back and forth.

Math versions are however not compatible with the range= argument, which probably renders this approach useless. For myself I actually quickly put together a version of Tex Gyre Pagella Math which has all serif latin letters and numbers replaced by sans serif versions from Tex Gyre Heros. Not everything works, e.g. \sqrt has a problem, but for simple math it seems to be fine. You can download it here: http://n.ethz.ch/~thomasu/download/TeXGyrePagellaMathSans-Normal.otf