How to set a font everywhere (including math mode) in XeLaTeX?

Iteration 1

\documentclass[10pt]{article}
\usepackage[MnSymbol]{mathspec}  % Includes amsmath.
\setmathsfont(Digits,Latin,Greek,Symbols)[Numbers={Lining,Proportional}]{Calibri}
\setmainfont[Mapping=tex-text, Ligatures={NoRequired,NoCommon,NoContextual}]{Calibri}
\usepackage[italic]{mathastext}

\begin{document}
ABCDEFGHIJKLMNOPQRSTUVWXYZ
  \begin{align}
    \dfrac{5a + 10b}{10} &= c \times |XY| - \alpha\beta\gamma^{2}\\
    x &= 5~\mathsf{°C}
  \end{align}
\end{document}

This ticks the first two boxes in my requirements, and here is what you get out:

Iteration 1 You can notice this approach has multiple problems though:

  1. The fraction line is not typeset in Calibri "style".
  2. The \times symbol is not typeset using Calibri.
  3. The \circ (degree) symbol is completely missing.
  4. The "C" in the equation is not typeset using Calibri. because I used \mathsf. Therefore, the Computer Modern sans font was used.

Iteration 2

My first solution was good-enough, so I stuck with it for a few years. However, during that time the great unicode-math package emerged.

The key ingredient (or hack) that satisfied my third requirement was the line \setmathfont[range={"0000-"FFFF}]{Calibri} which means: "Use Calibri for all Unicode characters 0x0000 to 0xFFFF, i.e. the vast majority of symbols I would ever want to use in equations."

Also note the \setmathfont[slash-delimiter=frac]{Cambria Math}. This is necessary, as Calibri is not available as a LaTeX math font. So we use Cambria Math, because it matches Calibri in style better, e.g. it has more matching fraction lines. However, we use the trick described above to use Calibri for the Unicode range of most of the characters anyone will see.

\documentclass[10pt]{article}
\usepackage{amsmath}  % Same for amsmath.
\usepackage{fontspec}
\usepackage{unicode-math}
\setmainfont[Mapping=tex-text,Ligatures={NoRequired,NoCommon,NoContextual}]{Calibri}
\setmathfont[slash-delimiter=frac]{Cambria Math}
\setmathfont[range={"0000-"FFFF}]{Calibri}
\setmathfont[range=up]{Calibri}
\setmathfont[range=sfup]{Calibri}
\setmathfont[range=it]{Calibri Italic}
\setmathfont[range=bfup]{Calibri Bold}
\setmathfont[range=bfit]{Calibri Bold Italic}
\setsansfont{Calibri}  % Make \mathsf and \textsf also Calibri

\begin{document}
ABCDEFGHIJKLMNOPQRSTUVWXYZ
  \begin{align}
    \dfrac{5a + 10b}{10} &= c \times |XY| - \alpha\beta\gamma^{2}\\
    x &= 5~\mathsf{°C}
  \end{align}
\end{document}

Here is the lovely result:

Iteration 2

Final notes

  • I verified that this works with XeLaTeX. It should work with LuaTeX as well.
  • This should work with any other font.
  • I hope this helps you typesetting beautiful documents.