19

I have a document where the main font is Gotham Book. So in my preamble I have:

\usepackage{fontspec}
\setmainfont{Gotham Book}

As the fontspec manual describes, this sets the math roman font to also be Gotham Book. But the math italic font remains from the Computer Modern family.

I know that \setmathrm will change the math roman font, and there are cousins \setmathtt, \setmathsf, and \setboldmathrm. But there's no \setmathit.

What am I missing?

lockstep
  • 250,273
Matthew Leingang
  • 44,937
  • 14
  • 131
  • 195

3 Answers3

22

If you do not wish to otherwise change your maths symbols, the best solution is to use the mathspec package:

\documentclass{article}
\usepackage{mathspec}
\setallmainfonts(Digits,Latin){Georgia}
\begin{document}
Hello $a+\mathrm{b}=c$
\end{document}

Here, Georgia will be used for the body text and \mathrm, and Georgia Italic will be used for the italic math glyphs.

The unicode-math package (which I kinda wrote) can also do this, but it's somewhat overkill for your purposes and has the additional downside that you need to also load a Unicode mathematics font. Here's an example:

\documentclass{article}
\usepackage{unicode-math}
\setmainfont{Georgia}
\setmathfont{xits-math.otf}
\setmathfont[range=\mathit]{Georgia Italic}
\begin{document}
Hello $a+b=c$
\end{document}

Note that mathspec will not run (yet) on LuaTeX, so if you need a LuaLaTeX solution then you'll need to use unicode-math for now.

  • Thanks, @Will. Do I not need \setmathrm as well? It seems like with my Gotham Book example where the main font is sans serifed it didn't change the operator font without it. – Matthew Leingang Feb 14 '11 at 14:56
  • @Matthew Yes, \setmathrm is probably a good idea as well, but that's not what your original question at the top of the page asks :) – Will Robertson Feb 14 '11 at 14:59
  • \setmainfont with fontspec was changing the math roman font automatically. I didn't mention it because it was doing the thing I wanted anyway, and I was not aware of mathspec. But \setmainfont with mathspec does not change the math roman font without an additional call to \setmathrm. – Matthew Leingang Feb 14 '11 at 15:31
  • @Will I am using the code \usepackage{mathspec} along with \setmathsfont(Digits,Latin,Greek){Latin Modern Math}. Strangely all the characters present inside the math environment are not italicized. Can you please help me fix this? Here is my question. – Aim Jan 01 '22 at 14:12
7

Thanks to Will and Leo for pointing out the mathspec package. So now I use:

\usepackage{mathspec} %loads fontspec as well
\setmainfont{Gotham Book}
\setmathrm{Gotham Book}
\setmathfont(Digits,Latin){Gotham Book}

This snippet may need optimization, but works.

Matthew Leingang
  • 44,937
  • 14
  • 131
  • 195
5

The Literal Answer

As of 2019, fontspec should find the bold and italic fonts, and load \mathrm, \mathbf and \mathit when you \setmathrm.

If you need additional set-up for \mathit, the ItalicFont and ItalicFeatures options of \setmathrm set up \mathit, just as the Boldfont and BoldFeatures options set up \mathbf.

\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{fontspec}
\usepackage[paperwidth=10cm]{geometry}

\setmainfont{TeX Gyre Schola} \setmathrm{TeX Gyre Schola}[ ItalicFeatures = {Color = green}]

\begin{document} Text \textit{italic} (\mathrm{math}) (\mathit{italic}) \end{document}

TeX Gyre Schola sample

What You Also Might Have Meant

In practice, someone asking about how to set the \mathit alphabet likely means the alphabet used for $x$.

To set that, you want to load the unicode-math package, set a math font that matches your main font and then, if necessary, add a line such as

\setmathfont[range=it]{Gotham Book Italic}

You can do likewise for up, bfup, bfit and the other math alphabets.

In unicode-math, you would normally use \symit, \symup, etc. for individual letters used as variables, and \mathit, \mathrm, \mathup, etc. for complete words. However, you can give a package option to interpret \mathit as \symit and make migrated code compile correctly.

Davislor
  • 44,045