# How can I change math font with \setmathfont inside a group?

Consider the following test file and compile it with LuaTeX (TeXLive 2016):

\documentclass{article}
\usepackage[math-style = TeX]{unicode-math}

\setmainfont{texgyretermes}[
Extension = .otf,
UprightFont = *-regular,
BoldFont = *-bold,
ItalicFont = *-italic,
BoldItalicFont = *-bolditalic,
]

Extension = .otf,
UprightFont = *-regular,
BoldFont = *-bold,
ItalicFont = *-italic,
BoldItalicFont = *-bolditalic,
]

\setmathfont{Latin Modern Math}

\begin{document}

$$\int_{t = 0}^{x^2} t dt$$

\setmathfont[range = {}]{Latin Modern Math}

$$\int_{t = 0}^{x^2} t dt$$

\setmathfont{Latin Modern Math}

$$\int_{t = 0}^{x^2} t dt$$

% {
%   \setmathfont[range = {}]{Latin Modern Math}

%   $$% \int_{t = 0}^{x^2} t dt %$$
% }

\end{document}


If I uncomment the last lines, LuaTeX hangs forever. How can I change math font inside a group?

EDIT

I'm well aware that I should not use a non math font to do math but, unfortunately, a sans serif math font does not yet exist, AFAIK.

The problem does not appear to be linked with the fact that I use a non math font.

\documentclass{article}
\usepackage[math-style = TeX]{unicode-math}

\setmathfont{Latin Modern Math}

\begin{document}

$$\int_{t = 0}^{x^2} t dt$$

\setmathfont{Asana-Math}

$$\int_{t = 0}^{x^2} t dt$$

\setmathfont{Latin Modern Math}

$$\int_{t = 0}^{x^2} t dt$$

% {
%   \setmathfont{Asana-Math}

%   $$% \int_{t = 0}^{x^2} t dt %$$
% }

\end{document}


However, as @cfr suggested, I should use \mathversion: the following works.

\documentclass{article}
\usepackage[math-style = TeX]{unicode-math}

\setmathfont[version=lm]{Latin Modern Math}
\setmathfont[version=asana]{Asana-Math}

\mathversion{lm}

\begin{document}

$$\int_{t = 0}^{x^2} t dt$$

\mathversion{asana}

$$\int_{t = 0}^{x^2} t dt$$

\mathversion{lm}

$$\int_{t = 0}^{x^2} t dt$$

{
\mathversion{asana}

$$\int_{t = 0}^{x^2} t dt$$
}

\end{document}

• Can you explain why you want to do this? Why not define another maths version and use that instead? – cfr Aug 27 '16 at 22:50
• @cfr My tcolorbox example environments are in sans serif font. I'd like to have a math font that fits. But I don't know how to do with mathversion. Would you mind writing à code showing how it works with opentype non math fonts? – cjorssen Aug 27 '16 at 22:54
• Please make sure your code examples compile before posting them unless the error is the problem. – cfr Aug 27 '16 at 23:52
• It would work if you had a suitable sans serif font, but I don't think there are any to be had. – cfr Aug 28 '16 at 1:37
• The problem really is that unicode-math is designed to work with opentype maths fonts. It isn't designed to work with other kinds of fonts. While you can persuade it to do some things with them by a combination of brute force and ignorance (in my case, anyway, though probably not yours), there is a real limit to that. unicode-math needs a maths table to work properly. I would suggest simply not using unicode maths fonts if you need to switch to sans serif for maths. Stick to traditional TeX fonts for maths in that case. – cfr Aug 28 '16 at 1:48

The first is that unicode-math really wants opentype fonts with a proper math table. It only works with fonts lacking this at all, as I understand it, because the configuration falls through to setting the applicable range to up, despite the font lacking anything which usually enables the setting of ranges etc.
The usual way to set an alternative set of maths fonts is to declare an alternative maths version e.g. bold versus normal. unicode-math supports this. This basically works, as your example shows.
However, the second problem is that it seems to be impossible to combine the use of range with version. Setting a range affects all maths versions rather than just the specified one, as I demonstrate in this question.