# Math digits are rendered in CM when using libertine and newtxmath with XeLaTeX in TeX Live 2016

I’m trying to use Linux Libertine in both text and math mode with XeLaTeX in TeX Live 2016.

Back in TeX Live 2014, I was able to achieve this using libertine and newtxmath packages, as suggested in this answer:

\usepackage[libertine]{newtxmath}
\usepackage[no-math]{fontspec}
\usepackage{xltxtra}
\usepackage{libertine}


However, this solution is broken in TeX Live 2016, since numbers and roman text in math mode are displayed in Computer Modern. Interestingly, removing the no-math option from fontspec fixes the issue with roman text, leaving only numbers in math mode to be rendered in CM. Here is a MWE:

\documentclass{article}
\XeTeXdefaultencoding utf-8
\usepackage[libertine]{newtxmath}
\usepackage{xltxtra} % loads fontspec and xunicode
% note that removing xltxtra does not affect how the output
\usepackage{libertine}

\begin{document}

Quite some text 123, $123$.

$3 \sin x \alpha$

\begin{displaymath}
\int_0^{+\infty} \left(\frac{\alpha}{\sqrt{\Omega_\Lambda - 1}}\right)^{\frac{1}{x}}
\mathrm{d}x
\end{displaymath}

\end{document}


As you can see, arabic numbers are rendered in CM, and other symbols are in Linux Libertine.

So here’s the big question: How can I get both text and math to render in Linux Libertine using newtxmath and libertine packages?

I would rather not use alternatives such as mathspec (last updated in 2009), unicode-math (last time I checked, it gave terrible results like these), or other \setmathfont / \setmainfont.

Besides, I’ve done some serious browsing on TeX.sx, and tried stuff proposed in many different answers, without any luck. These include:

• \setmainfont and newtxmath: numbers and roman text are rendered as CM (see here and here)
• \setmathsfont and mathspec: most math is rendered as CM (see here, here)
• newtxmath and mathspec: numbers and roman text are rendered as CM (see here)

Somewhere in the various passages, the mathcode of the digits gets assigned wrongly.

\documentclass{article}

\usepackage{fontspec}
\usepackage{libertine}
\usepackage[libertine]{newtxmath}

\AtBeginDocument{%
\Umathcode0="7 "0 0
\Umathcode1="7 "0 1
\Umathcode2="7 "0 2
\Umathcode3="7 "0 3
\Umathcode4="7 "0 4
\Umathcode5="7 "0 5
\Umathcode6="7 "0 6
\Umathcode7="7 "0 7
\Umathcode8="7 "0 8
\Umathcode9="7 "0 9
}

\begin{document}

Quite some text 123, $123$.

$3 \sin x \alpha$

\begin{displaymath}
\int_0^{+\infty} \left(\frac{\alpha}{\sqrt{\Omega_\Lambda - 1}}\right)^{\frac{1}{x}}
\mathrm{d}x
\end{displaymath}

\end{document}


Note that \XeTeXdefaultencoding is not necessary. Also loading xltxtra is not recommended: favor fontspec directly.

• Thanks, this works just fine! Is it certain that the digits are the only symbols that get wrong mathcodes? I’m curious, why is xltxtra not recommended? – Arcturus B Jan 7 '17 at 16:12
• I’ve also found that using fontspec with no-math, and using \DeclareSymbolFont{operators}{\encodingdefault}{\familydefault}{m}{n} gives a similar output. Any comments on that? – Arcturus B Jan 7 '17 at 16:13
• @ArcturusB That's another possibility, yes. – egreg Jan 7 '17 at 16:18
• Your code gives me several errors c:\texlive\2017\texmf-dist\tex\latex\base\article.cls:20: Invalid math code during > 0x8000 mathcode fallback. [\begin{document}] with LuaLaTeX under TeXLive 2017. – lblb Oct 1 '17 at 17:29
• @lblb Fixed for both engines – egreg Oct 1 '17 at 19:51

I've also encountered this issue using the newtxmath package with the libertine option and then adding OTF text fonts.

The newtxmath package is supposed to load "minlibertine" if no text font has been loaded, but it seems to fail to do this sometimes. Under LuaLaTeX, forcing it to load ensures that the CM figures are replaced by libertine figures in math. Most of the following just comes from Michael Sharpe's documentation (ex. 3), but the \renewcommand... is the key addition that solved the problem for me:

% load text components other than libertine text to be used in math
\usepackage[T1]{fontenc}
% \usepackage[scaled=.85]{beramono}% used only by \mathtt
% \usepackage[type1]{cabin}% used only by \mathsf
\renewcommand{\rmdefault}{minlibertine} %% This needs to be forced, else doesn't load.
% (Suspect cmr flag in package vs. lmr default in LuaTeX, but have not investigated...)
\usepackage[libertine,upint]{newtxmath}
% newtxmath should load minlibertine because no other Roman text package was specified; however, for me, it does not detect the need.
% \mathrm and \mathbf use minlibertine

% Now load the otf text fonts using fontspec---won't affect math
\usepackage[no-math]{fontspec} % process with XeLaTeX or LuaLaTeX
\usepackage{libertine}

• perhaps you should ping Michael Sharpe about this by sending him an email ? he might indicate other approach or update his packages. – user4686 Sep 7 '17 at 8:52
• @jfbu - Thanks, since I'm not the only one seeing the issue, I'll do that. – John Sep 7 '17 at 12:14

Use the superior Libertinus fonts. They look like Libertine, in fact

»This is a fork of the Linux Lib­er­tine and Linux Bi­olinum fonts that started as an OpenType math com­pan­ion of the Lib­er­tine font fam­ily, but grown as a full fork to ad­dress some of the bugs in the fonts.« — (from CTAN)

\documentclass{article}
\usepackage{unicode-math}
\setmainfont{Libertinus Serif}
\setsansfont{Libertinus Sans}
\setmonofont{Libertinus Mono}
\setmathfont{Libertinus Math}
\begin{document}

Quite some text 123, $123$.

$3 \sin x \alpha$

\begin{displaymath}
\int_0^{+\infty} \left(\frac{\alpha}{\sqrt{\Omega_\Lambda - 1}}\right)^{\frac{1}{x}}
\mathrm{d}x
\end{displaymath}

\end{document}


• Thanks for your answer! Although I don’t quite like the output: the integral is different from Linux Libertine (probably a result of this new font?); the exponent $x$ is either too small, or too low (looks like a result of unicode-math); and what happened to the Q glyph? – Arcturus B Jan 7 '17 at 15:50

Michael Sharpe has just posted an update to newtxmath.sty (version 1.527, 11 Sept 2017), which corrects this problem. The update is on CTAN and also through TexLive.

• I'm using the current version of newtxmath under TeXLive 2017 and with OP's MWE I get the same wrong output with CM figures and I struggle with this problem with my own projects. – lblb Oct 1 '17 at 17:35
• @lblb -- Using newtxmath v1.527 with TexLive2017 worked for me for using the Libertine fonts, following the code I posted above on 7 Sept 2017 (without the line forcing "minlibertine") and running under LuaLaTeX. I have not tried OP's MWE, however, since these other solutions were sufficient for the objective. – John Oct 1 '17 at 21:30