# Switch math font to sans serif in caption

I use the opentype version of Alegreya for the main text including math and the opentype version of Alegreya Sans for headings and captions. Sometimes I have some math in captions of figures and tables, which I would like to have in the sans serife font, too. I know that I can declare a sans-version with unicode-math which can be used with symsf{...}, etc. But since some of my math is "hidden" in commands for chemical compounds, this is quite a bad option for me. So I tried to define two math commands and add them to \rmfamily and \sffamily. The following MWE works for me:

\documentclass{scrbook}
\usepackage{xpatch}
\usepackage{amsmath}
\usepackage{fontspec}
\setmainfont[
%Path={Alegreya/},% add appropriate directory
Extension = .otf,
UprightFont = *-Regular,
ItalicFont = *-Italic,
BoldFont = *-Bold,
BoldItalicFont = *-BoldItalic,
]{Alegreya}

\setsansfont[
%Path={AlegreyaSans/},% add appropriate directory
Extension = .otf,
UprightFont = *-Regular,
ItalicFont = *-Italic,
BoldFont = *-Bold,
BoldItalicFont = *-BoldItalic,
]{AlegreyaSans}

\usepackage{unicode-math}
\setmathfont{latinmodern-math.otf}
\newcommand{\rmmath}{
\setmathfont{Alegreya-Regular.otf}[Numbers={Lining, Proportional}]
\setmathfont{Alegreya-Italic.otf}[range=it/{latin,Latin,greek,Greek}]
\setmathfont{Alegreya-Regular.otf}[range=up/{latin,Latin,greek,Greek,num,},Numbers={Lining, Proportional}]
\setmathfont{Alegreya-BoldItalic.otf}[range=bfit/{latin,Latin,greek,Greek}]
\setmathfont{Alegreya-Bold.otf}[range=bfup/{latin,Latin,greek,Greek,num},Numbers={Lining, Proportional}]
}

\newcommand{\sfmath}{
\setmathfont{AlegreyaSans-Regular.otf}[Numbers={Lining, Proportional}]
\setmathfont{AlegreyaSans-Italic.otf}[range=it/{latin,Latin,greek,Greek}]
\setmathfont{AlegreyaSans-Regular.otf}[range=up/{latin,Latin,greek,Greek,num,},Numbers={Lining, Proportional}]
\setmathfont{AlegreyaSans-BoldItalic.otf}[range=bfit/{latin,Latin,greek,Greek}]
\setmathfont{AlegreyaSans-Bold.otf}[range=bfup/{latin,Latin,greek,Greek,num},Numbers={Lining, Proportional}]
}

\xapptocmd{\rmfamily}{\rmmath}{}{}
\xapptocmd{\sffamily}{\sfmath}{}{}

\usepackage{chemmacros}
\usepackage{caption}
\captionsetup{
labelfont={sf,bf},
textfont={sf},
}

\newcommand{\compound}{\ch{A_{$x$}B_{$x-1$} * $n$ H2O}}

\begin{document}
\rmfamily
Text in rm with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \compound\\
\sffamily
Text in sf with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \compound
\end{document}


The problem arises when I use the switch more than once. So this...

\begin{document}
\rmfamily
Text in rm with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \compound\\
\sffamily
Text in sf with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \compound\\
\rmfamily% ---> fails at this point (! LaTeX Error: Too many symbol fonts declared.)
Text in rm with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \compound
\end{document}


... and this ...

\begin{document}
\rmfamily
Text in rm with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \compound\\
\sffamily
Text in sf with math $3a + b_2 = c - \alpha\times x \frac{3}{4}$ and chemical formula \compound\\
\begin{figure}
%   \caption{Figure.}% ---> fails at this point (! LaTeX Error: Too many symbol fonts declared.)
\end{figure}
\end{document}


... or just this ...

\begin{document}
\begin{figure}
\caption{Figure.}% ---> fails at this point (! LaTeX Error: Symbol font __um_fam1' not defined.)
\end{figure}
\end{document}


... all fail with the errors given in the code. This behavior was already mentioned some months ago.

Other solutions suggest to use mathversion with unicode-math. But this lacks the range= argument which I need for italic variables and upright numbers. I also gave sansmath-package a trial but could not get it to work with my fonts.

I wonder if there is really no workaround or solution for the unicode-math` issue. I would appreciate if somebody had some suggestions for this or maybe for a complete different solution which I am not aware of.

PS: I would prefer a XeLaTeX solution, since LuaLaTeX is extremely slow on my system.