# Using two different math fonts with LuaLaTeX

In a document I need to use both TrueType and some Type1, so I switched to LuaLatex. But I found a problem when setting math fonts. Here is what I need:

• the document uses a TrueType serif font (Crimson) as its main font, but apparently the only suitable match for math is the Type1 font Cochineal, available through newtxmath

• in certain parts of the document I need to switch to a sans font (Asap), and there is some math, which I want to be printed with the same font. I rely on the package sansmath for this.

The problems is that the no-math option in fontspec seems to be an all-or-nothing option.

Here is my setup (I use my specific fonts, but I guess it would be the same with any other similar font setup)

\documentclass{article}
\usepackage[cochineal]{newtxmath}
\usepackage{sansmath}
\usepackage[no-math]{fontspec} % luatex
\setmainfont{Crimson-Roman}[Ligatures=TeX,
ItalicFont = {Crimson-Italic},
BoldFont = {Crimson-Semibold},
BoldItalicFont = {Crimson-SemiboldItalic}]
\setsansfont{Asap-Regular}[
ItalicFont ={Asap-Italic},
BoldFont = {Asap-SemiBold},
BoldItalicFont = {Asap-SemiboldItalic}]
\DeclareSymbolFont{operators}{\encodingdefault}{\familydefault}{m}{n}

\begin{document}

This is a text with the main font, and some math: $\sum_ix_i^2$

\sffamily\sansmath and here is some other text in sans, with some math too $\sum_ix_i^2/2$.

\end{document}


However, because I set the math font with newtxmath (and the no-math option in fontspec) the result is that not all symbols are printed properly when the formula is sans (namely, the "/" symbol disappears from the formula).

On the other hand, if I don't set newtxmath (or, equivalently, if I don't set the option no-math), everything is fine with the math in sans, but the main math formulas (in serif) are printed in ComputerModern instead of Crimson.

It seems there is no way to tell LuaTex to rely selectively on a Type1 font only for one math font (the one matching the main/serif font) and not the other (the one used with the sans font).

Or maybe there is something wrong with my setup.

There's an additional problem with \DeclareSymbolFont{operators}...: this command seems necessary, otherwise numbers are not printed properly with the (cochineal) math font; however, the command affect all math fonts.

• Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See minimal working example (MWE). – Henri Menke Nov 1 '17 at 7:26
• I modified my question, and produced a minimal example – Massimo Nov 1 '17 at 9:54

A bit late, but it’s a good question.

To use a Type1 math font with an OpenType text font, you can \usepackage{mathspec} after \newtxmath, and \setmathsfont to use your text font in math mode. As long as the text font matches the math symbols, you’re golden.

Another method is mathastext, which now supports fontspec.

Finally, in unicode-math, use \defaultfontfeatures{Scale=MatchLowercase} and consider whether any of the available OpenType math fonts would match well if you loaded \setmathfont[range=it]{Crimson Roman Italic}, \setmathfont[range=up]{Crimson Roman}, etc., after it.

I found a workaround to the problem. I am not using any more the Type1 font for math; instead, with unicode-math, I create a custom math font based on Latin Modern and Crimson (not as nice as newtx, but it's ok). For math in sans, I create a new command \mathsans which allows me to select the sans math font everytime I need it. There may be some better way, but it works.

\documentclass{article}
\usepackage{fontspec,unicode-math}
\setmainfont{Crimson-Roman}[Ligatures=TeX,
ItalicFont = {Crimson-Italic},
BoldFont = {Crimson-Semibold},
BoldItalicFont = {Crimson-SemiboldItalic}]
\setsansfont{Arial}
\setmathfont{Latin Modern Math}
\setmathfont[range=\mathup/{num}]{Crimson-Roman}  % for numbers
\setmathfont[range=\mathit/{latin}]{Crimson-Italic}

\newcommand{\mathsans}{\setmathfont{Arial}
\setmathfont[range=\mathit]{Arial-Italic}}

\begin{document}

Here the main font, with some math: $\sum_ix_i^2$

\sffamily\mathsans and here is some other text in sans, with some math too $\sum_ix_i^2/2$.

\end{document}


EDIT: Although this solution works in the minimal example, it gives an error message for longer files. Specifically, it doesn't seem to allow for multiple switches to/from the sans font. The error is:

! LaTeX Error: Symbol font __um_fam3' not defined.


Somethind relate is reported here: Switching math fonts with unicode-math

• A nice solution. Shall I delete my answer or let it stay? – Steven B. Segletes Nov 1 '17 at 16:22
• No please, leave it. Yours is still a good solution when one wants to use a Type1 font. Moreover, I am finding some problem with my solution in a longer document (still debugging) – Massimo Nov 1 '17 at 16:30

Load newtxmath after sansmath, to recover the /.

\documentclass{article}
\usepackage[no-math]{fontspec} % luatex
\usepackage{sansmath}
\usepackage[cochineal]{newtxmath}
\setmainfont{Crimson-Roman}[Ligatures=TeX,
ItalicFont = {Crimson-Italic},
BoldFont = {Crimson-Semibold},
BoldItalicFont = {Crimson-SemiboldItalic}]
\setsansfont{Arial}
\DeclareSymbolFont{operators}{\encodingdefault}{\familydefault}{m}{n}

\begin{document}

This is a text with the main font, and some math: $\sum_ix_i^2$

\sffamily\sansmath and here is some other text in sans, with some math too $\sum_ix_i^2/2$.

\end{document}


• Thanks. The first setup somehow solves my problem (although the sans math font is not the same sans font of the regular text). On the other hand, the second and third solution set up the right sans math font, but I can't understand how can I use the newtxtext font (cochineal) in this case, as the option no-math is not used. More specifically: I need no-math to use a Type1 math font, but this is not possible if I want to use the unicode-math package, or is it? – Massimo Nov 1 '17 at 14:44
• @Massimo Oops. Only the 1st solution was supposed to appear. The others are detritus. – Steven B. Segletes Nov 1 '17 at 14:51
• @Massimo As to why Arial does not show in the math font, I asked David about that here, tex.stackexchange.com/questions/231873/…, and he thinks its a "feature" not a bug LOL. – Steven B. Segletes Nov 1 '17 at 14:54
• @Massimo The different engine/font incompatibilities is not my specialty. Perhaps someone more learned will chime in. – Steven B. Segletes Nov 1 '17 at 15:01
• I see. I thought that a package like mathastext` might do the job, but I have no idea how to use it with LuaLaTeX – Massimo Nov 1 '17 at 15:01