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,
]
\setsansfont{texgyreadventor}[
Extension = .otf,
UprightFont = *-regular,
BoldFont = *-bold,
ItalicFont = *-italic,
BoldItalicFont = *-bolditalic,
]
\setmathfont{Latin Modern Math}
\begin{document}
\begin{equation}
\int_{t = 0}^{x^2} t dt
\end{equation}
\setmathfont[range=up/{greek,Greek,latin,Latin,num}]{texgyreadventor-regular}
\setmathfont[range=it/{greek,Greek,latin,Latin,num}]{texgyreadventor-italic}
\setmathfont[range = {}]{Latin Modern Math}
\begin{equation}
\int_{t = 0}^{x^2} t dt
\end{equation}
\setmathfont{Latin Modern Math}
\begin{equation}
\int_{t = 0}^{x^2} t dt
\end{equation}
% {
% \setmathfont[range=up/{greek,Greek,latin,Latin,num}]{texgyreadventor-regular}
% \setmathfont[range=it/{greek,Greek,latin,Latin,num}]{texgyreadventor-italic}
% \setmathfont[range = {}]{Latin Modern Math}
% \begin{equation}
% \int_{t = 0}^{x^2} t dt
% \end{equation}
% }
\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}
\begin{equation}
\int_{t = 0}^{x^2} t dt
\end{equation}
\setmathfont{Asana-Math}
\begin{equation}
\int_{t = 0}^{x^2} t dt
\end{equation}
\setmathfont{Latin Modern Math}
\begin{equation}
\int_{t = 0}^{x^2} t dt
\end{equation}
% {
% \setmathfont{Asana-Math}
% \begin{equation}
% \int_{t = 0}^{x^2} t dt
% \end{equation}
% }
\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}
\begin{equation}
\int_{t = 0}^{x^2} t dt
\end{equation}
\mathversion{asana}
\begin{equation}
\int_{t = 0}^{x^2} t dt
\end{equation}
\mathversion{lm}
\begin{equation}
\int_{t = 0}^{x^2} t dt
\end{equation}
{
\mathversion{asana}
\begin{equation}
\int_{t = 0}^{x^2} t dt
\end{equation}
}
\end{document}
unicode-mathis 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 01:48unicode-mathshould not fail quite so confusingly and spectacularly if you try to useversionhere - and I certainly would consider that a bug, even if a satisfactory fix would be to fail with meaningful errors - but it cannot be expected to actually work. It just isn't meant to. – cfr Aug 28 '16 at 01:50versionandrangeat all, even if you are trying to restrict the range for a font with a maths table. Perhaps @JosephWright knows if this is a bug or a known limitation and whether there's a workaround. – cfr Aug 28 '16 at 14:45