sans serif math font selection using unicode-math

I am typesetting a document in XeLaTeX using a serif font for main text, and a sans serif for everything else (section titles, tables etc). For setting of mathematics, I use TeX Gyre Pagella Math. Using fontspec and unicode-math, this mostly works fine. I use the etoolbox package to automatically switch to sans serif for floats. However, mathematics are still typeset in the serif font. I know how to switch to sans serif math (which is available in TGPM) using \mathsf{…}, but I would like to automate this. I've tried the version=… option of unicode-math (in combination with etoolbox), but I can't figure out how to make that work. I also couldn't figure out how to make this work using etoolbox directly.

Below is an MWE of my current setup; the point is to have the mathematics in the table content in sans serif as well, which is not the case now. To be clear: I use other Komascript options to set the caption labels to sans serif, but left it out here for brevity. Also, in case it matters, I use Minion Pro and Fira Sans as my actual fonts, but not everyone might have those available.

\documentclass[]{scrbook}

\usepackage[T1]{fontenc}
\usepackage{fontspec}
\usepackage{unicode-math}

\setmainfont{TeX Gyre Pagella}[%
Ligatures   = TeX,%
Numbers     = {OldStyle,Proportional},%
]
\setsansfont{TeX Gyre Heros}[%
Ligatures   = TeX, %
Numbers     = {Lining,Monospaced}, %
Scale       = MatchLowercase, %
]

\setmathfont[%
Scale       = MatchLowercase,%
]{TeX Gyre Pagella Math} %

\setmathfont[%
version     = table,%
Scale       = MatchLowercase,%
]{TeX Gyre Pagella Math} %

\usepackage{%
tabularx,%
etoolbox,%
}

% etoolbox
\AtBeginEnvironment{tabularx}{%
\mathversion{table}
}

\begin{document}
\pagestyle{empty}

\begin{table}[t]
\centering
\caption{$\mathsf{a^{2}+b^{2}=c^{2}}$}
\begin{tabularx}{.6\linewidth}{lrr}
some & text & and some math \\
$a^{2}$ & $+b^{2}$ & $=c^{2}$
\end{tabularx}
\end{table}

\begin{displaymath}
(x + a)^n = \sum_{k=0}^n {n \choose k} x^k a^{n-k}
\end{displaymath}

\end{document}

• unicode-math only works with fonts which support mathematics. It works with Pagella because Pagella is one of only a few fonts to support the maths extension for opentype. But Pagella is a serif font, so obviously you won't get sans. Hence, I'm not completely sure that I'm understanding the question.
– cfr
Aug 24 '15 at 16:08
• You are not switching to "sans serif math" with \mathbf, you are only mapping (a rather small) set of glyphs to the sansserif variants. Also imho you should better use \mathsfit{a}^{\mathsf{2}} in the caption to get an slanted a. Regarding the automation I would have expected that range={it->sfit, up->sfup } would do the mapping you want, but I get errors and @WillRobertson will have to look at it. (I don't think that two math versions are really a good idea, imho it can get confusing. Math fonts convey meaning). Aug 24 '15 at 16:09
• @cfr the Pagella Math font includes a few sans serif symbols that can be selected using \mathsf{}; as in the $\mathsf{a^{2}+b^{2}=c^{2}}$ line in my MWE. Aug 24 '15 at 19:52
• @UlrikeFischer Point taken, but I use math in the tables sparingly (but enough that I would like to automate it), and I don't think it would confuse anyone. It's just that a single serif math glyph thrown into sans serif table looks very ugly and unprofessional. The sans serif Pagella Math with my sans serif text font is still not ideal, but better. And I just want to learn how this stuff works… Aug 24 '15 at 19:58
• OK. That doesn't include any specifically-mathematical symbols. I didn't know Pagella offered sans variants. I assume they are intended to be used for cases where the mathematical symbol should be sans serif, even though the main maths font is serif. (Which is why using them otherwise is likely going to be confusing.) Why are you activating the T1 font encoding? Do you need it?
– cfr
Aug 24 '15 at 21:13

To avoid ambiguity, for new readers landing, it may be helpful, for reference, to have a visual on the unicode blocks.

Tex Gyre Pagella Math has Basic Latin (call it "text", but mappable)

Greek and Coptic ("text")

... lots of other blocks ...

and the Mathematical Alphanumeric Symbols (MAS) block:

and

(Sidenote: Because of the organic way Unicode has developed, some of the more famous mathematical symbols are located in the earlier Letter-like Symbols block:

)

By comparison, the Fira Sans Math font's MAS is much smaller:

Other Tex Gyre fonts (Bonum, DejaVu, Schola, Termes) have a math version -- see e.g. ...\texmf-dist\fonts\opentype\public\tex-gyre-math\

An old question, but it deserves an answer.

I’m not sure exactly what you want, here. If you are looking for Termes as your main font, and Fira Sans for everything else, you could use the Fira Math font:

\defaultfontfeatures{Scale=MatchLowercase}
\setmainfont{TeX Gyre Termes}[Scale=1.0]
\setsansfont{Fira Sans}
\setmathfont{Fira Math}


If you mean you want to be able to switch between \mathversion{normal} and \mathversion{sans}, you could do something like this:

\defaultfontfeatures{Scale=MatchLowercase}
\setmainfont{TeX Gyre Termes}[Scale=1.0]
\setsansfont{Fira Sans}
\setmathfont{TeX Gyre Termes Math}
\setmathfont{Fira Math}[version=sans]

\mathversion{sans}


If you want to use letters and numerals from Fira in math mode, but other symbols from Termes, you could try:

\defaultfontfeatures{Scale=MatchLowercase}
\setmainfont{TeX Gyre Termes}[Scale=1.0]
\setsansfont{Fira Sans}
\setmathfont{TeX Gyre Termes Math}
\setmathfont{Fira Math}[range={up,it,bfup,bfit}]


Unfortunately, the range= and version= options are not compatible with each other, as of 2021.