[Tex/LaTex] Use different math font in inline-style and display-style formulas

equationsfontsmath-modexetex

I would like to use a different font in inline-style math mode ($...$) and displayed equations (e.g., \begin{equation} ... \end{equation}). Currently, I set my fonts with

\setmainfont{Frutiger 45 Light}
\setmathrm{Cambria Math}
\setmathfont(Digits,Latin){Cambria Math}

but I would like to use Frutiger in mathmode as well so there is no break in font style for in-line math. I tried defining a new command

\newcommandx{\textvar}[1]{
{
\setmathrm{Frutiger 45 Light}
\setmathfont(Digits){Frutiger 45 Light}
#1
}
\setmathrm{Cambria Math}
\setmathfont(Digits,Latin){Cambria Math}
}

but \setmath* seem to work only in the preamble and not in the document itself.

Is there any way to do this?

Best Answer

As we are fans of good typography, the discussion about best practises is always mandatory and important.

I want to second what Chris says above:

Please do not try that at home!

% arara: lualatex

\documentclass{article}
\usepackage{unicode-math}
\setmathfont[version=lm]{Latin Modern Math}
\setmathfont[version=asana]{Asana Math}

\begingroup
\catcode`\$=\active
\protected\gdef$#1${\mathversion{asana}\(#1\)\mathversion{lm}}
\endgroup
\AtBeginDocument{\catcode`\$=\active}

\begin{document}
    \mathversion{lm}
    \begin{equation}E=mc^2\end{equation} where $E$ stands for energy \begin{equation}E=mc^2\end{equation}
\end{document}

Edit:

In comment you mention that you want to keep the old behaviour. In this case you should (highly recommended) define a new command for such things:

\newcommand*{\myinlinemath}[1]{{\mathversion{asana}\ensuremath{#1}}}

Edit 2:

For your very example I would just recommend to use the siuntix package:

% arara: xelatex

\documentclass{article}
\usepackage{fontspec}
\setmainfont{linux libertine o}
\usepackage{siunitx}

\begin{document}
\begin{equation}E=mc^2\end{equation}

something like \SI{2.8 \pm 0.5}{\mu\metre} in the middle of a line

\sisetup{detect-all}
something like \SI{2.8 \pm 0.5}{\mu\metre} in the middle of a line
\end{document}

enter image description here