TransWikia.com

Package fontspec and unicode-math change mathsf default in beamer

TeX - LaTeX Asked on February 26, 2021

EDIT: it turned out, that my real file problem was caused by another package.

(This question is closely related to a prior question of mine Package dcolumn displays different fonts as that still didn’t solve my original problem (see the comments).)

Im am using the beamer class. Hence the sans-serif font is the default in this document.

I have/want to use an OTF font, so my file relies on package fontspec.

There is a table, which incorporates a D-colunm from dcolumn package, as well as regular r or l columns. I was very surprised to find out, that the D column does not use sans-serif fonts!

I could boil down the problem to package fontspec.

Here is a MWE producing a correct output:

documentclass{beamer}
usepackage{dcolumn}
newcolumntype{E}{D{,}{,}{-3}}
begin{document}
begin{frame}
  frametitle{Correct Math Font}
  begin{center}
    With PDFLaTeX{} or LuaLaTeX{} \
    but without package texttt{fontspec}\
    begin{tabular}{rE}
      1,23 & 1,23
    end{tabular}
  end{center}
end{frame}
end{document}

And this is the output, as expected.

Correct output

Now, simply add the line usepackage{fontspec} to that MWE and it gets weird.

EDIT: Ulrike Fischer did answer and was able to solve the problem given in that MWE above. But using that solution on my real file, did not work. It turned out, that another package requires the package unicode-math which in turn brings up the same problem again. I edited the MWE below by inserting usepackage{unicode-math} as well.

documentclass{beamer}
usepackage{dcolumn}
usepackage{fontspec}
usepackage{unicode-math}
newcolumntype{E}{D{,}{,}{-3}}
begin{document}
begin{frame}
  frametitle{Incorrect Math Font}
  begin{center}
    With LuaLaTeX{} and \
    with package texttt{fontspec}\
    begin{tabular}{rE}
      1,23 & 1,23
    end{tabular}
  end{center}
end{frame}
end{document}

This time, you get this output:

Incorrect output with rm fonts in the second column

As you can clearly see, the right column is in normal, upright roman font. Why is that and—even more important—how to fix this?

EDIT: Ulrike Fischers answer did solve my first question, before I had to edit it. So, technically speaking, her answer deserves the green check mark also (at least partially). After having upgraded my question with regard to the unicode-math package, the answer from Davislor solves my question completely!

2 Answers

In a comment, you followed up and said you need a version that works with unicode-math. Since your column package appears to switch to math mode, one way to make this work is to turn on professionalfonts and load the sans-serif faces for your math alphabets yourself.

documentclass[professionalfonts]{beamer}
usepackage{unicode-math}
usepackage{dcolumn}
newcolumntype{E}{D{,}{,}{-3}}

setmainfont{NewComputerModernBook}
setsansfont{NewComputerModernSansBook}
setmonofont{NewComputerModernMonoBook}
setmathfont{NewCMMath-Book.otf}
setmathfont{NewCMSans10-Book}[range={up,"2C}]
setmathfont{NewCMSans10-BookOblique}[range=it]
setmathfont{NewCMSans10-Bold}[range=bfup]
setmathfont{NewCMSans10-BoldOblique}[range=bfit]
setmathrm{NewComputerModernSansBook}

begin{document}
begin{frame}
  frametitle{Incorrect Math Font}
  begin{center}
    With LuaLaTeX{} and \
    with package texttt{fontspec}\
    begin{tabular}{rE}
      1,23 & 1,23
    end{tabular}
  end{center}
end{frame}
end{document}

New Computer Modern Sans sample

The range={up} option takes care of letters and digits. You also need to set any punctuation you need, such as, in this case, the comma (U+2C).

A simpler alternative would be to load a sans-serif math font, such as setmathfont{KpMath-Sans.otf}. This lets you skip overriding its math alphabets. You would still want to setmathrm or setmainfont to a sans-serif font.

Correct answer by Davislor on February 26, 2021

If you don't want fontspec to affect the math setup you can use the no-math option

documentclass{beamer}
usepackage{dcolumn}
usepackage[no-math]{fontspec}
newcolumntype{E}{D{,}{,}{-3}}
begin{document}
begin{frame}
  frametitle{Correct Math Font}
  begin{center}
    With LuaLaTeX{} and \
    with package texttt{fontspec}\
    begin{tabular}{rE}
      1,23 & 1,23
    end{tabular}
  end{center}
end{frame}
end{document}

Answered by Ulrike Fischer on February 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP