# Use different math font in inline-style and display-style formulas

I would like to use a different font in inline-style math mode ($...$) and displayed equations (e.g., $$...$$). 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?

• I'm not familiar with the fonts in question but in the general case this is a bad idea. If you say $$E=mc^2$$ where $E$ stands for energy you want the 2 'E' characters to match. In particular there are some notations in which sans-serif characters have a different meaning, aren't there? Jun 11, 2015 at 13:50
• I know that this is the generall idea behind this behaviour, but if I write something like $2.8 \pm 0.5 \mu m$ in the middle of a line, it just looks better if it is in the same font as the rest, and it is pretty annoying that up to now I have to type 2.8 $\pm$ 0.5 $\mu$m to get what I want, and the \mu is still in the wrong font. Jun 11, 2015 at 13:57
• Where 'wrong' means consistent with all other equations in the document. Jun 11, 2015 at 14:02
• Ok, so we settled that what I try is not considered best practice and that there's a sensible idea behind the default behavior. Can we now return to the original question of how to do it instead of discussing why one should or should not try something like this? Jun 11, 2015 at 14:05
• The tradition on this site is to point out bad choices in comments, and I think we're quite happy with that tradition, cf. the meta question on this. Jun 11, 2015 at 14:47

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}
$$$$E=mc^2$$$$ where $E$ stands for energy $$$$E=mc^2$$$$
\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}
$$E=mc^2$$

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}


• Thanks for the answer. As said above, I know this is not the way it's usually done. However, I keep getting the ! LaTeX Error: Can be used only in preamble. test §abc \pm 55§ error (I switched out the catcode so I still have both options, but I think that should not be the problem?) Jun 11, 2015 at 14:30
• +1 for answering the question while still agreeing with me! Jun 11, 2015 at 14:30
• @user3696412 you are welcome. XeLaTeX can be used as well. If you prefer. Redefining the  command might not be stable. I would not recommend that. I do not know, how to set the math font globally. But I just edited to work with versions which is more efficient. Jun 11, 2015 at 14:39
• @LaRiFaRi I recommend editing your answer to say \SI{2.8 \pm 0.5}{\micro\metre} or \SI{2.8 \pm 0.5}{\um}. These commands fit the spirit of siunitx better and should result in an upright mu symbol instead of the italic one in your answer currently. Sep 27, 2018 at 19:24

Here's a solution that uses the \everymath and \everydisplay primitives to switch between two pre-defined math versions -- "Asana Math" for inline math and "XITS Math" for displayed equations.

The code needs to be run under LuaLaTeX; for some reason, I get a "TeX capacity exceeded, sorry" error message if I try to compile it under XeLaTeX.

Caveat: The code has been tested with several display-style math environments, but definitely not with all conceivable ones.

% !TEX TS-program = lualatex
\documentclass{article}

\usepackage{amsmath}

\usepackage[no-math]{fontspec}

\usepackage{unicode-math}
\setmathfont[version=XITS]{XITS Math}
\setmathfont[version=Asana]{Asana Math}
\mathversion{Asana}
\everymath{\mathversion{Asana}}
\everydisplay{\mathversion{XITS}}

\setlength\textwidth{7cm} % just for this example
\begin{document}
$E=mc^2$, where $E$ stands for energy \dots
$$E=mc^2$$

$E=mc^2$, where $E$ stands for energy \dots
\begin{gather}$$E=mc^2$$\end{gather}
\end{document}