# otf sans serif math only in figures

I was going to use a serif font (TG Pagella) for most of the text (including captions) and a sans serif font (Candara) for my (tikz) figures. For text this works nice, but not for maths. First I used only unicode-math with the version key to switch to the sans version via \mathversion{...}. The problem is that unicode-math expects an otf math font which does not exists for Candara. Using the normal Candara font results in up-right math, but I want it to be italic. With the italic font everything in math becomes italic even numbers.

So next I tried to use mathspecs. There the sans math works nice. The problem is that I don't know how to switch between sans and serif, because there is no version option for \setmathsfont and \setmathsfont can only be called from the preamble. So I only used the version key for Pagella and tried to switch to the sans font via \mathversion{normal} but this does not really work. Some how the math font is then always Candara (see below).

Is there any way to get that work?

\documentclass[a5paper]{memoir}

\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{units}
\pgfplotsset{compat=1.8}

\usepackage{mathspec}

\usepackage{unicode-math}
\setmainfont[Numbers=OldStyle]{TeX Gyre Pagella}
\setmathfont[version=tgp]{TeX Gyre Pagella Math}
\setsansfont[Scale=MatchUppercase]{Candara}
\setmathsfont(Digits,Latin,Greek)[Scale=MatchUppercase]{Candara}

\usepackage{siunitx}
\SendSettingsToPgf

\tikzstyle{every picture}+=[font=\small\sffamily,
execute at begin picture=\mathversion{normal}
]
\pgfplotsset{unit code/.code 2 args={
\sisetup{detect-family}
\si{#1#2}}}
\pgfplotsset{general plot/.style={
xtick pos=left,
ytick pos=left,
enlarge x limits=false,
minor x tick num=1,
every x tick/.style={color=black, thin},
every y tick/.style={color=black, thin},
tick align=outside,
xlabel near ticks,
ylabel near ticks,
unit markings=parenthesis
}
}

\usepackage{blindtext}
\parindent=0pt
\mathversion{tgp}
\begin{document}
$V_{\textup{DS}}$ in \si{V}\\
1234567890\\
\textsf{1234567890}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
general plot,
xlabel=Voltage $V_{\text{DS}}$,
x unit=\volt,
change y base=true,
y SI prefix=micro,
ylabel=Current $I$,
y unit=\ampere]
\end{axis}
\end{tikzpicture}
\caption{The current $I$ in \si{\micro\ampere} versus the voltage $V_\textup{DS}$ in \si{\volt}}
\end{figure}
\end{document}


Edit: Following the comments of Khaled Hosny I tried to use only unicode-math. There was one problem the range option does not work for version-tagged fonts. So I made two commands to switch between the settings. And indeed this works fine for math in the body. But if I use them in conjunction with the tikz key execute at begin picture I get building errors:

I can only handle fonts at positive sizes that are
less than 2048pt, so I've changed what you said to 10pt.

LaTeX Font Info:    Font shape EU1/CandaraItalic(0)/m/n' will be
(Font)              scaled to size 0.0pt on input line 57.
! Improper at' size (0.0pt), replaced by 10pt.
relax
l.57 ^^I^^I^^I\end{axis}


One other error message is:

*************************************************
* fontspec warning: "script-not-exist-latn"
*
* Font 'Candara' does not contain script 'Math'.
*************************************************
! Font \nullfont has only 7 fontdimen parameters.


And here the source code for that:

\documentclass[a5paper]{memoir}

\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{units}
\pgfplotsset{compat=1.8}

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

\newcommand{\sfmath}{
\setmathfont[Numbers=Lining, Scale=MatchUppercase]{Candara}
\setmathfont[range=\mathit, Scale=MatchUppercase]{Candara Italic}
}

\usepackage{siunitx}
\sisetup{detect-family}
\SendSettingsToPgf

\tikzstyle{every picture}+=[font=\small\sffamily,
% This does not compile:
execute at begin picture=\sfmath,
execute at end picture=\rmmath
]
\pgfplotsset{unit code/.code 2 args={
\si{#1#2}}}
\pgfplotsset{general plot/.style={
xtick pos=left,
ytick pos=left,
enlarge x limits=false,
minor x tick num=1,
every x tick/.style={color=black, thin},
every y tick/.style={color=black, thin},
tick align=outside,
xlabel near ticks,
ylabel near ticks,
unit markings=parenthesis
}
}

\parindent=0pt

\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
general plot,
xlabel=Voltage $V_{\text{DS}}$,
x unit=\volt,
change y base=true,
y SI prefix=micro,
ylabel=Current $I$,
y unit=\ampere]
\caption{The current $I$ in \si{\micro\ampere} versus the voltage $V_\textup{DS}$ in \si{\volt}}
{\sffamily V    \sfmath $5 V_{\mathrm{DS}}$ in \si{V}}\\
V \rmmath $5 V_{\textup{DS}}$ in \si{V}\\

• When using unicode-math with Candara, you just need to tell it to use Candara Italic font (or whatever the name for its italic font is). – Khaled Hosny Jul 11 '13 at 0:08
• @KhaledHosny, but then also the the numbers are italic, here for example in the plot, because pgfplots seems to use a math mode for the tick labels. Or same if I want A^{-1} for the axis label. – quinmars Jul 11 '13 at 0:36