Controlling both main text and math mode fonts

fontsluatexmath-mode

I'm using a scrartcl document class and LuaLatex to write an article which contains math equations. I am required to use Arial as my main text font, which I've set using the package fontspec (to "unlock" the Arial font) and \setmainfont{Arial}. I can use "any" serif font for equations, so I've kept the default one.

Whenever I encounter multi-letter variables I'd like to use \mathit{} to avoid having space between each letter and still keep it in italics, but unfortunately then the font changes to Arial, instead of the math mode font:

result1

As I understand mathit is not supposed to change the font. I've tried to force the math font to change using \setmathrm{Asana-Math} (I don't have to use this one specifically, Asana Math is just the first font I've encountered in this document under "Math fonts"), but then the normal math mode is still in the default font and everything else in Asana-Math WITHOUT italics (even mathit), see result:

result2

\documentclass[12pt,a4paper]{scrartcl} 
\usepackage{fontspec} % get additional fonts
\setmainfont{Arial} % sets the main font for the document
\setmathrm{Asana-Math} % change math mode font; only used in second result
\begin{document} 
    Normal text.
    
    $f = MATHMODE$ 
    
    $\mathit{f = mathit}$
    
    $\mathrm{f = mathrm}$
\end{document}

How can I have control over both my main text font and the math mode font? How can I get both the math mode and the result of \mathit{} in my desired math font and in italics?

Best Answer

Mico’s solution works, but here’s a more comprehensive set of options.

Traditional TeX used commands such as \mathit, \mathrm and \mathbf both for words in math mode and also for math symbols. The unicode-math package attempts to separate these two things. Compare:

\documentclass{article}
\usepackage{unicode-math}

\begin{document}
\noindent\texttt{\textbackslash mathit} \(\mathit{iff}\) \\
\texttt{\textbackslash symit} \(\symit{iff}\)
\end{document}

Latin Modern sample

By default in fontspec, \mathit gives you the first (iff typeset as a word, not the product of the three math symbols i, f and f). As you’ve seen, it defaults to using the text font for \mathrm, \mathit and \mathbf. Here are some ways to override that.

The Simple Fix

You’re using \mathit and want it to behave like \symit. There’s a unicode-math package option for that, mathit=sym.

\documentclass{article}
\usepackage[mathit=sym]{unicode-math}

\setmainfont{Arial}
\defaultfontfeatures{Scale=MatchLowercase}
\setmonofont{Fira Mono}
\setmathfont{Fira Math} % Or your math font of choice.

\begin{document}
\noindent Arial \textit{Italic} \\
\texttt{\textbackslash mathit} \(\mathit{iff} \) \\
\texttt{\textbackslash symit} \(\symit{iff}\)
\end{document}

Arial + Fira sample

Here. I used Fira Math as a sans-serif math font that matches reasonably well. You mentioned that you’re using any serif math font; the default for Office’s Equation Editor is Cambria Math.

If you still want to use words in math mode, and have them appear in the main text font, you can use the commands \textnormal and \textit in math mode.

If you’re using \mathit this way, you might also expect \mathrm and \mathbf to work the same way, so the command you want might in fact be

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

Say Precisely What You Mean

You want the behavior of \symit, rather than the default behavior of \mathit under unicode-math, which isn’t precisely the same as the default behavior of \mathit under other packages. Your code will be much less likely to break unexpectedly when you copy it into another document if you write \symit or \textit in your source, instead of the ambiguous \mathit.

Change the \mathit Font

The \setmathrm command from fontspec also lets you set the \mathit, \mathbf and \mathbfit fonts, as the ItalicFont, BoldFont and BoldItalicFont of \mathrm:

\documentclass{article}
\usepackage{unicode-math}

\setmainfont{Arial}
\defaultfontfeatures{Scale=MatchLowercase}
\setmonofont{Fira Mono}
\setmathfont{Fira Math} % Or your math font of choice.
\setmathrm{FiraSans}[ Extension = .otf ,
                      UprightFont = *-Regular ,
                      ItalicFont = *-Italic ,
                      BoldFont = *-Bold,
                      BoldItalicFont = *-BoldItalic ]

\begin{document}
\noindent Arial \textit{Italic} \\
\texttt{\textbackslash mathit} \(\mathit{iff} \) \\
\texttt{\textbackslash symit} \(\symit{iff}\)
\end{document}

Arial + Fira sample

Use a Legacy Math Font

You don’t have to use unicode-math. If you load fontspec with the no-math option, it will leave the existing definition of \mathit unchanged. Here is one of the few combinations of legacy sans-serif math packages that works, more or less:

\documentclass{article}
\usepackage{newtxsf}
\usepackage[tx]{sfmath}
\usepackage[no-math]{fontspec}

\setmainfont{Arial}

\begin{document}
\noindent Arial \textit{Italic} \\
\texttt{\textbackslash mathit} \(\mathit{iff} \) \\
\texttt{\textbackslash textit} \(\textit{iff}\)

\( \displaystyle\int \frac{\partial y}{\partial t} \sum_{S} y(t) \)
\end{document}

Newtxsf + Arial

Related Question