# Temporarily changing the math font in certain environments

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}{%
}

\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.)

• I think this is a duplicate of a question asked a few minutes ago? You probably want to switch math versions having set up versions for each font set: tex.stackexchange.com/a/231911/1090 – David Carlisle Mar 7 '15 at 21:56
• @DavidCarlisle Thanks for pointing that out! Cannot comment there, so I'll do it here: \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 7 '15 at 22:45
• ah it would have helped id I'd got the right command name, will fix thanks! – David Carlisle Mar 7 '15 at 22:51

Thanks to @DavidCarlisle who pointed me to the discussion here, and the example given here, I've managed to solve my problem. See below.

\documentclass{scrartcl}
\usepackage{unicode-math}
\usepackage{fontspec}
\usepackage{tabularx}
\DeclareMathVersion{table}
\setmainfont[Ligatures=TeX, Numbers={OldStyle, Proportional}]{TeX Gyre Pagella}
\setsansfont[Ligatures=TeX, Numbers={Lining, Proportional}]{TeX Gyre Heros}
\setmathfont[math-style=TeX, version=normal]{TeX Gyre Pagella}
\setmathfont[math-style=upright, bold-style=upright, version=table]{TeX Gyre Heros}

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

\begin{document}
\section{A test with formulas: $\mathbfup{a+b=c}$}
This is a font test.
\section{A test with formulas: $a+b=c$}

test, 1, 2, 3, 5, 7, 9, $5+4=9$, $a+b=c$.

\begin{table}
\begin{tabularx}{.25\linewidth}{lcc}
&   Test & $a=b$    \\
a   &   1, 5 & $9$      \\
b   &   2, 7 & $5$  \\
c   &   3, 9 & $3$  \\
\end{tabularx}
\end{table}
\end{document}