# Switching math fonts with 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}


• – Mico Aug 23 '13 at 13:34
• @Mico, that's right. The version option and \mathversion would be the best, unfortunately the version option does not work with range. – quinmars Aug 23 '13 at 16:21

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

• That sounds at least like a approach that works, although very hackish. How did you replaced the characters? – quinmars Aug 23 '13 at 16:58
• On my Mac I used a trial version of glyphsapp.com. I guess something like FontForge would also work. It needs some manual intervention though as you have to identify the parts to be replaced and then manually (I used a Python script) adjust the glyph names and unicode IDs. – thomasius Aug 25 '13 at 19:52
• Thanks alot, i'll give it at try, when I have some time. – quinmars Aug 25 '13 at 20:34
• Actually I even managed to retain the full math functionality by also transferring the MATH table entries by copying the relevant directly from one Spline Font Database Format (.sdf) file produced by FontForge to the other. – thomasius Aug 26 '13 at 20:35