7

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.

2
  • 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). Commented Nov 1, 2017 at 7:26
  • I modified my question, and produced a minimal example
    – Massimo
    Commented Nov 1, 2017 at 9:54

5 Answers 5

3

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.

4

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

2
  • A nice solution. Shall I delete my answer or let it stay? Commented Nov 1, 2017 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
    Commented Nov 1, 2017 at 16:30
1

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}

enter image description here

9
  • 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
    Commented Nov 1, 2017 at 14:44
  • @Massimo Oops. Only the 1st solution was supposed to appear. The others are detritus. Commented Nov 1, 2017 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. Commented Nov 1, 2017 at 14:54
  • @Massimo The different engine/font incompatibilities is not my specialty. Perhaps someone more learned will chime in. Commented Nov 1, 2017 at 15:01
  • 1
    @MátéWierdl Yes, that is correct. One could edit the line \DeclareSymbolFont{operators}{\encodingdefault}{\sfdefault}{m}{n} to change the operator font Commented Feb 24 at 21:26
0

The problem seems to be with the interaction between sansmath and newtxmath, and can be seen with pdflatex as well. Having

\documentclass{article}

% \usepackage{kpfonts}
%\usepackage{sansmath}
\usepackage{newtxmath}
\usepackage{sansmath}

\begin{document}

Normal mode $\ln [\sin (xy)]/z$ \sansmath switch to sansmath
$\ln [\sin (xy)]/z$ \unsansmath and back to normal
$\ln [\sin (xy)]/z$.

\end{document}

and compiling with pdflatex we see that ()[]/ are missing

newtxmath sansmath

and if I load sansmath first, then the operatornames are not sans serif after the switch

sansmath newtxmath

I tried a few other packages with mathfonts (like kpfonts) and only newtxmath shows the issue.

0

As it was suggested @Davislor, here is a solution with using mathastext in place of sansmath. It also works with pdflatex and can switch to tt fonts. Works better than I thought it would. I am using cochineal instead of crimson since the latter doesn't have greek fonts to be used by mathastext, so it uses its own.

\documentclass{article}

%%% lualatex
% \usepackage[no-math]{fontspec}
% % Crimson doesn't have greek letters
% \setmainfont{cochineal}
% \setsansfont{cabin}[Scale=MatchLowercase]
% \setmonofont{Inconsolatazi4}[
% StylisticSet={1,2,3},Scale=MatchLowercase]
%%% end lualatex

%%% pdflatex
% Crimson doesn't have greek letters
\usepackage{cochineal}
\usepackage[scale=0.88]{cabin}
\usepackage[varqu,varl,var0,scale=0.93]{inconsolata}
%%% end pdflatex

\usepackage[cochineal,vvarbb,smallerops]{newtxmath}


\usepackage[italic,defaultmathsizes,LGRgreek]{mathastext}

% for the sans switch command \MTversion{cabin}
\MTfamily{\sfdefault}
\MTupgreek % upright greek looks better in sans
\Mathastext[cabin]
\newenvironment{sfswitch}{\MTversion{cabin}}{}

% for the tt switch command \MTversion{inconsolata}
\MTfamily{\ttdefault}
\MTupgreek % upright greek looks better in sans
\Mathastext[inconsolata]

\begin{document}
\noindent
\emph{Serif} mode: $\mathbb{R,1}$ $\alpha\beta\gamma\delta, \Re z$ and
$\ln [\sin^{2} (xy)]/z\cdot\int \sum \frac{1}{x^2-1}$.\medskip

\begin{sfswitch}
  \noindent \emph{Sans} mode using env: $\mathbb{R,1}$
  $\alpha\beta\gamma\delta, \Re z$ and
  $\ln [\sin^{2} (xy)]/z\cdot\int \sum \frac{1}{x^2-1}$.\medskip
\end{sfswitch}

\noindent Normal: $\ln [\sin^{2} (xy)]/z\cdot\int \sum \frac{1}{x^2-1}$
{\MTversion{cabin} Sans serif:
  $\ln [\sin^{2} (xy)]/z\cdot\int \sum \frac{1}{x^2-1}$ }
Back to normal again:
$\ln [\sin^{2} (xy)]/z\cdot\int \sum \frac{1}{x^2-1}$\medskip

\noindent {\MTversion{inconsolata} This is tt mode: test quote and
  zero: "0" and then
  $\ln [\sin^{2} (xy)]/z\cdot\int \sum \frac{1}{x^2-1}$}

\end{document}

pdflatex

The output when using Lualatex looks a bit fifferent due to fatter greek letters and insisting making them upright in serif mode (not bad for consistency with the sans serif mode, though).

lualatex

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .