8

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}

MWE rendered with XeLaTeX (TL2016). All symbols are rendered in Linux Libertine except for numbers in math mode that are in CM

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)

4 Answers 4

8

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

\documentclass{article}

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

\AtBeginDocument{%
  \Umathcode`0="7 "0 `0
  \Umathcode`1="7 "0 `1
  \Umathcode`2="7 "0 `2
  \Umathcode`3="7 "0 `3
  \Umathcode`4="7 "0 `4
  \Umathcode`5="7 "0 `5
  \Umathcode`6="7 "0 `6
  \Umathcode`7="7 "0 `7
  \Umathcode`8="7 "0 `8
  \Umathcode`9="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}

enter image description here

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

8
  • 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
    Commented Jan 7, 2017 at 16:12
  • 2
    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
    Commented Jan 7, 2017 at 16:13
  • @ArcturusB That's another possibility, yes.
    – egreg
    Commented Jan 7, 2017 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
    Commented Oct 1, 2017 at 17:29
  • 1
    @lblb Fixed for both engines
    – egreg
    Commented Oct 1, 2017 at 19:51
2

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}

enter image description here

1
  • 1
    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
    Commented Jan 7, 2017 at 15:50
2

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

\usepackage[scr=rsfso]{mathalfa}% helps with loading of math alphabets
\usepackage{bm}% load after all math to give access to bold math

% Now load the otf text fonts using fontspec---won't affect math
\usepackage[no-math]{fontspec} % process with XeLaTeX or LuaLaTeX
\usepackage{libertine} 
2
  • 1
    perhaps you should ping Michael Sharpe about this by sending him an email ? he might indicate other approach or update his packages.
    – user4686
    Commented Sep 7, 2017 at 8:52
  • 1
    @jfbu - Thanks, since I'm not the only one seeing the issue, I'll do that.
    – John
    Commented Sep 7, 2017 at 12:14
1

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.

2
  • 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
    Commented Oct 1, 2017 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
    Commented Oct 1, 2017 at 21:30

You must log in to answer this question.

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