# xelatex & libertine: \renewcommand\ttdefault no longer works

This MWE

\documentclass{article}

\usepackage{libertine}
\renewcommand\ttdefault{lmtt}

\begin{document}
foo \texttt{bar}
\end{document}


previously (say, about one year ago) typeset bar with lmtt if processed with XeLaTeX. It no longer does. Instead, a member of the Libertine font family gets used. Obviously, changing \ttdefault gets ignored. Why?

Note that processing with pdflatex delivers the expected Latin Modern font.

Used packages (from TeXLive SVN rev 52404, 2019-10-16)

• LateX2e 2019-10-01
• fontspec 2019-10-14
• libertine 2019-02-27

[I regenerated all formats with fmtutil-usr --all.]

• What's the reason for not doing \usepackage[tt=false]{libertine}? – egreg Oct 16 at 7:57
• Quite simple: I wasn't aware of that option :-) Now I know, thanks. However, the OP persists, as far as I can see. – lemzwerg Oct 16 at 8:00
• If I do \usepackage[tt=false]{libertine} and remove \renewcommand{\ttdefault}{lmtt}, I get Latin Modern Mono. Don't you? – egreg Oct 16 at 8:51
• Yes, I do. However, the old way no longer works, see the answers in tex.stackexchange.com/questions/358418/… or ipfs-sec.stackexchange.cloudflare-ipfs.com/tex/A/question/…, for example. Both solutions give \renewcommand\ttdefault... as a possibility – and as mentioned above, it did work previously. – lemzwerg Oct 16 at 9:46
• Note that this is unrelated to libertine.sty and also happens with \usepackage{fontspec} \setmonofont{DejaVu Sans Mono} \renewcommand\ttdefault{lmtt}. – Ralf Stubner Oct 16 at 11:17

## 2019/10/18 update

Will clarified this issue to be an oversight of fontspec in his comment. I propose the following temporary patch before the next release of fontspec. The patch works under pdfLaTeX, XeLaTeX and LuaLaTeX:

\documentclass{article}

\usepackage{libertine}
\renewcommand\ttdefault{lmtt}

% Temporary patch
\makeatletter
\AtBeginDocument{%
\DeclareRobustCommand\ttfamily{%
\fontencoding{\csname g__fontspec_nfss_enc_tl\endcsname}%
\fontfamily{\ttdefault}%
\selectfont
}%
}{%
}%
}
\makeatother

\begin{document}
foo \texttt{bar}
\end{document}


Warning: This patch depends on fontspec’s private variable \g_@@_nfss_enc_tl and thus could stop working with newer releases of fontspec.

Running the following MWE

\documentclass{article}

\usepackage{fontspec}
\setmonofont{Fira Mono}
\renewcommand\ttdefault{lmtt}

\begin{document}
foo \texttt{bar}
\end{document}


with fontspec 2016/01/30 v2.5, I got

But with fontspec 2017/02/12 v2.6 or later, I got

The package libertine calls fontspec internally when running on XeLaTeX or LuaLaTeX. Since fontspec v2.6, the implementation of \setmonofont was changed. With the newer releases, after issuing \setmonofont{<Foo>}, fontspec will redefine \ttfamily to always use the font family <Foo> instead of the ones by \ttdefault.

User level redefinition of \ttdefault is never good syntax IMHO. I recommend using higher level redefinition:

\documentclass{article}

\usepackage{libertine}
% For XeLaTeX or LuaLaTeX
\setmonofont{Latin Modern Mono}[Scale=MatchLowercase]

\begin{document}
foo \texttt{bar}
\end{document}

• Thanks for the answer. What was the reason for this change in fontspec? I consider it quite bad, since (a) it is incompatible to previous versions, and (b) it is incompatible to other TeX engines. In other words, it breaks zillions of documents, because the solution you are tagging as having 'never good syntax' was standard for many years, as shown in the links I gave. – lemzwerg Oct 17 at 5:57
• By the way, using \setmonofont makes the document uncompilable with pdflatex... – lemzwerg Oct 17 at 6:04
• @lemzwerg “because the solution you are tagging as having 'never good syntax' was standard for many years, as shown in the links I gave” I disagree. Just because a solution was provided to the public decades ago does not make it standard in any regards. The font package, libertine in this case, should be responsible for providing an option to configure your mono-spaced family (which it does). I was simply explaining why these hacks from years ago stopped working. – Ruixi Zhang Oct 17 at 13:00
• Ok, let's agree to disagree. However, the two backward incompatibilities are something that can't be discussed away. Additionally, it's not mentioned in fontspec's README.md file that this (mis)feature no longer works. Honestly, I consider the whole issue as something that accidentally happened while Will reorganized and simplified the fontspec code, especially if you read the relevant commit messages. – lemzwerg Oct 17 at 14:37
• @lemzwerg Well, if you use fontspec for your documents, then you must use XeLaTeX or LuaLaTeX and not pdfLaTeX. And fontspec never said “if you want to set up mono-spaced family, then you could redefine \ttdefault by looking at the NFSS specification and do something like \renewcommand\ttdefault{lmtt}”. Instead, fontspec said “use \setmonofont to set up your mono-spaced family”. – Ruixi Zhang Oct 17 at 14:43

This works again with fontspec version 2.7d, released 2019-10-19.

• For my humble opinion this is a comment. – Sebastiano Oct 20 at 19:43
• Hmm. It's not clear to me how to better answer my own question. The OP describes a bug, which has been fixed with the just released fontspec version. Maybe the question should be removed altogether? – lemzwerg Oct 21 at 5:47