# Force Computer Modern Maths Font in Maths Mode

Currently, I'm using LuaLatex and fontspec to manage the fonts in my document. However, I found that when I wanted to use a sans-serif font like Roboto as "normal text", the maths mode text is in Roboto too, although the letters and symbols are in the serif maths font. This is problematic because it can make things harder to read. Below are a few examples of my problem:

Notice how both examples are in math mode, but the text is all in sans serif.

Does anyone know how I can force all text in maths mode to be computer modern math font?

I've read this post here: Force font to computer modern (serif) in math mode although it is set to sans serif, but the solution posted requires me to write \mathrm{} in pretty much every maths environment that I encouter, which would be a pain.

I've also read this post here: Force Computer Modern in math mode but I am not using beamer.

Edit: Code below

\documentclass[15pt]{article}

% define paper margins
\usepackage[
a4paper,
portrait,
top=1.2cm,
bottom=1.2cm,
left=1.5cm,
right=1.5cm,
headheight=15pt, % avoid warning by fancyhdr
heightrounded % to avoid underfull messages
]{geometry}

% Make justifications better.
\usepackage{microtype}

% Allows inclusion of sample text.
\usepackage{blindtext}

% Gets outlines right.
\usepackage{bookmark}

% ams stuff
\usepackage{amsmath, amsthm, amssymb}

% Allows inclusion of special spaces
\usepackage{xspace}

\usepackage{fancyhdr}
\pagestyle{fancy}

% Allows inclusion of custom fonts.
\usepackage[no-math]{fontspec}

% Allows the use of custom enumerators in lists.
\usepackage{enumitem}

% Allows drawing of graphical images.
\usepackage{tikz}

% Allows inclusion of images.
\usepackage{graphicx}

% Automatic paragraph spacing
\usepackage{parskip}

\usepackage{hyperref}

% Allows boxes to be drawn around text and math environments.
\usepackage{tcolorbox}

% Includes extra mathtools.
\usepackage{mathtools}

% Use Unicode math fonts.
\usepackage{unicode-math}

% hyperref setup
\hypersetup {
filecolor=magenta,
urlcolor=cyan
}

% Augmented Matrix environment.
\newenvironment{amatrix}[1]
{\left[\begin{array}{@{}#1@{}}}
{\end{array}\right]}

% Commands
\newcommand{\latex}{\LaTeX\xspace}
\renewcommand\qedsymbol{$\blacksquare$}
\newcommand{\reals}{\mathbb{R}}
\newcommand{\naturals}{\mathbb{N}}
\newcommand{\integers}{\mathbb{Z}}
\newcommand{\rationals}{\mathbb{Q}}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}

% Fixing brackets
%\let\originalleft\left
%\let\originalright\right
%\renewcommand{\left}{\mathopen{}\mathclose\bgroup\originalleft}
%\renewcommand{\right}{\aftergroup\egroup\originalright}

% Line spacing
\renewcommand{\baselinestretch}{1.22}

\DeclareMathOperator{\Tr}{Tr}
\DeclareMathOperator{\sgn}{sgn}
\DeclareMathOperator{\lcm}{lcm}
\DeclareMathOperator{\Span}{Span}
\DeclareMathOperator{\Col}{Col}
\DeclareMathOperator{\Null}{Null}
\DeclareMathOperator{\Row}{Row}
\DeclareMathOperator{\Rank}{Rank}
\DeclareMathOperator{\Nullity}{Nullity}

\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}
\DeclarePairedDelimiter\sqb{[}{]}
\DeclarePairedDelimiter\pr{(}{)}
\DeclarePairedDelimiter\cb{\{}{\}}
\DeclarePairedDelimiter\vbar{|}{|}

\newcommand{\ro}[1]{%
\xrightarrow{\mathmakebox[\rowidth]{#1}}%
}
\newlength{\rowidth}% row operation width
\AtBeginDocument{\setlength{\rowidth}{6.8em}}

% Font
\defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\setmainfont{Roboto}
%\setmathfont{Fira Math}[Color=7a4cef]
\setmonofont{Ubuntu Mono}

% Miscellaneous Variables
\newcommand{\me}{Kookie}

% Title
\title{\Huge Something}
\author{\me}
\date{\today}
\begin{document}
$$\Rank A + \Nullity A = c$$
$$\lim_{h\to 0}\frac{f(x+h) - f(x)}{h}$$
\end{document}

• \usepackage[no-math]{fontspec}. If this doesn't work you will have to show a small complete example. – Ulrike Fischer Jun 4 '20 at 15:41
• @UlrikeFischer Just tried it. It didn't change anything – Kookie Jun 4 '20 at 15:43
• well as I said: you will have to show your code. – Ulrike Fischer Jun 4 '20 at 15:44
• @UlrikeFischer I've added it, sorry I misread your message. – Kookie Jun 4 '20 at 15:51

Use mathrm=sym to force unicode-math to use the math font:

\documentclass{article}

\usepackage[mathrm=sym]{unicode-math}

\DeclareMathOperator{\Rank}{Rank}
\DeclareMathOperator{\Nullity}{Nullity}

\setmainfont{Roboto}

\begin{document}
$\Rank A + \Nullity A = c$
$\lim_{h\to 0}\frac{f(x+h) - f(x)}{h}$
\end{document}


An alternative is to set the mathrm font to a better text font or e.g. to the math font itself:

\documentclass{article}

\usepackage{unicode-math}

\DeclareMathOperator{\Rank}{Rank}
\DeclareMathOperator{\Nullity}{Nullity}

\setmainfont{Roboto}
\setmathrm{Latin Modern Math}

\begin{document}
$\Rank A + \Nullity A = c$
$\lim_{h\to 0}\frac{f(x+h) - f(x)}{h}$
\end{document}

• Yep, it works. Thank you so much! Could you tell me what mathrm is all about in the unicode-math package? – Kookie Jun 4 '20 at 16:12
• I'd probably use \setmathrm{Latin Modern Roman} – egreg Jun 4 '20 at 21:49
• @egreg Normally I prefer to ensure "math unicode" in math. But for mathrm it shouldn't make much difference (as long as only ascii is used) - the unicode values are the same. – Ulrike Fischer Jun 4 '20 at 22:15
• @UlrikeFischer \mathrm and \operatorname are only to be used with text. – egreg Jun 4 '20 at 22:19
• @egreg I agree for \mathrm, but operatornames are math for me. Also if operatorname uses \symrm, stuff like lím don't work ;-). – Ulrike Fischer Jun 4 '20 at 22:30

LaTeX documents have traditionally used \mathrm to do two distinct things: typeset upright math symbols (such as constants in ISO style), and typeset short passages of text in math mode. The fontspec package, by default, sets \mathrm to the main font, but it provides a \setmathrm command to override that. So, if you wanted to use Roboto in text mode but Latin Modern as \mathrm, you might write:

\defaultfontfeatures{Scale = MatchLowercase}
\setmainfont{Roboto}[Scale = 1.0]
\setmathrm{Latin Modern Roman}


It provides \setboldmathrm for the \mathrm font in \boldmath, which is distinct from the \mathbf font you get with \setmathrom[BoldFont = .... It also has \setmathsf and \setmathtt.

The unicode-math package, in addition to these, provides another set of math alphabets for math symbols. Normally, you would access these with the alphabets \symup, \symit, etc. However, since authors traditionally used \mathrm, \mathit, and so on, the package has the option mathrm=sym to make \mathrm a synonym for \symup. It also allows you to declare an arbitrary number of math alphabets with \setmathfontface.

By default, operators such as \sin and \log will use \mathrm, but unicode-math lets you override this with \setoperatorfont. You will definitely want to do this if you have redefined \mathrm to \symup.

Here is an example of Euler’s equations set in ISO style, with the upright math symbols in Hermann Zapf’s Euler. The other math symbols are from a clone of his Palatino, and for the operators sin and cos, I use a clone of his Optima. You’ll notice the difference between the imaginary constant i and the i in sin.

\documentclass{minimal}
\usepackage[math-style=ISO,mathup=sym]{unicode-math}

\defaultfontfeatures{Scale=MatchLowercase}
\setmainfont{TeX Gyre Pagella}[Scale=1.0]
\setsansfont{URW Classico}
\setmathfont{Asana Math}
\setmathfont[range={up/{Latin,latin,Greek,greek},
bfup/{Latin,latin,Greek,greek},
cal,bfcal},
script-features={},sscript-features={}]
{Neo Euler}
\setmathsf[Ligatures={Common, TeX}]{URW Classico}
\setoperatorfont{\mathsf}

\begin{document}
\begin{align*}
\mathup{e}^{\mathup{i} \theta}  &= \sin \theta + \mathup{i} \cos \theta \\
\mathup{e}^{\mathup{i \pi}} - 1 &= 0
\end{align*}
\end{document}


Here is a version that more literally does what you ask, setting upright math constants as Computer Modern Upright Italic, but math operators as Computer Modern Roman:

\documentclass{minimal}
\usepackage[math-style=ISO,mathup=sym]{unicode-math}

\defaultfontfeatures{Scale=MatchLowercase}
\setmathfont{Latin Modern Math}
\setmathfont[range=up/{Latin,latin,Greek,greek}]{CMU Serif Upright Italic}
\setmathfontface\mathcmr{CMU Serif}
\setoperatorfont{\mathcmr}

\begin{document}
\begin{align*}
\mathup{e}^{\mathup{i} \theta}  &= \sin \theta + \mathup{i} \cos \theta \\
\mathup{e}^{\mathup{i \pi}} - 1 &= 0
\end{align*}
\end{document}


You would use commands such as \lim, \textnormal{otherwise}, \operatorname{Nullity} or \DeclareMathOperator\Rank{Rank} to get your output.

If you want all in Sans Serif:

\documentclass{article}

\usepackage{firamath-otf}
\setmainfont{Roboto}
\DeclareMathOperator{\Rank}{Rank}
\DeclareMathOperator{\Nullity}{Nullity}

\setmainfont{Roboto}

\begin{document}

$\Rank A + \Nullity A = c$
$\lim_{h\to 0}\frac{f(x+h) - f(x)}{h}$

\end{document}


• Yeah, so I think this is a good idea too. Unfortunately, I've done this before and I found that the text was harder to read because it was more difficult to tell if some text was inline math or not and vice versa. It got to a point where I started colouring the math environment text purple just to make it more distinguishable. But by then it was a complete mess. So I came back to using sans serif lettering with serif math font. Thanks for your answer! – Kookie Jun 4 '20 at 16:24
• You should use it the other way round: All in serif text! This is the font which is easier to read especially for long texts. – user187802 Jun 4 '20 at 17:57
• Unfortunately, I'm not a fan of serif font. I actually find it harder to read. – Kookie Jun 5 '20 at 0:53