A follow-up question to my earlier post here. In my document, I want to typeset the tables in a different font from the main text. I've managed to make that work for regular input. However, I occasionally need to use math in the tables. At that point, the font switches back to the regular font for the main text. I've tried things with the mathspec package, but cannot make it work. How can I change the math font temporarily for tables?
See the example below. I am not very strictly bound to fontspec/mathspec/xetex solutions, but since the rest of my setup depends quite a bit on it, I would prefer not to have to change that too much, or add many more other packages.
\documentclass{scrartcl}
\usepackage{mathspec}
\usepackage{tabularx}
\setmainfont[Ligatures=TeX, Numbers={OldStyle, Proportional}]{TeX Gyre Pagella}
\setsansfont[Ligatures=TeX, Numbers={Lining, Proportional}]{TeX Gyre Heros}
\setmathfont(Digits,Latin,Greek)[Numbers={Lining, Proportional}]{TeX Gyre Pagella}
\setmathsf[Numbers={Lining, Monospaced}]{TeX Gyre Heros}
\usepackage{etoolbox}
\AtBeginEnvironment{tabularx}{%
\sffamily\addfontfeature{Numbers={Monospaced, Lining}}%
}
\begin{document}
\section{A test}
This is a font test.
test, 1, 2, 3, 5, 7, 9, $1, 2, 3, 5, 7, 9,\mathsf{9}$.
\begin{table}
\begin{tabularx}{.25\linewidth}{lcc}
& Test & $a=b$ \\
a & 1, 5 & $\mathsf{9}$ \\
b & 2, 7 & $5$ \\
c & 3, 9 & $3$ \\
\end{tabularx}
\end{table}
\end{document}
(I saw this discussion, which is related but does not quite give a solution to my problem.)
\newmathversion{}doesn't work (for me), but\DeclareMathVersion{}does. I've managed to make it work; I'll answer my own question below. – Matthijs Mar 07 '15 at 22:45