# When should package fontenc be used with pdflatex?

I have been using package fontenc with the following invocation, mainly because of what I read here.

\usepackage[T1]{fontenc}


But I've noticed that I lose some of the ligatures with the libertine package when I do this. For example, with the following code I get a number of ligatures (maybe more?):

\documentclass{article}
\usepackage{libertine}
\begin{document}
fi ff fl ffl ffi fj Th Qu
\end{document}


The output looks like this:

But if I add fontenc like this:

\documentclass{article}
\usepackage{libertine}
\usepackage[T1]{fontenc}
\begin{document}
fi ff fl ffl ffi fj Th Qu
\end{document}


I lose the fj, Th, and Qu ligatures:

So I'm guessing that fontenc shouldn't be loaded with libertine. Is that correct? And if so, are there guidelines as to when to use it?

(And I've forgotten how to add high-quality example images. A link to instructions would be great.)

• I was going to suggest reading the documentation, but the manual doesn't tell you any of the details you might want to know here. It does recommend using the T1 or LY1 encoding. However, only OT1 appears to include the additional ligatures. This is not necessarily a reason to prefer OT1, of course. – cfr Dec 27 '16 at 16:57

There is no simple answer to this. Or, rather, you already know the simple answer. In general, I would recommend

\usepackage[T1]{fontenc}


unless you know you need something different. In general, this will give good results which will be better than the alternatives in most cases.

This is because the T1 encoding will get you everything the default OT1 encoding gets you, plus a wide range of pre-composed accented characters and some additional characters needed in various Western European languages. Meanwhile, English will be at least as good, possibly better than with OT1.

However, it is possible to alter an encoding when creating a font support package so that the T1 encoding or the OT1 encoding or whatever is not quite the same as the official one. This only works straightforwardly for certain kinds of characters. Most notably, it works for ligatures.

This means that it is possible to add additional ligatures into spots in the encoding which would otherwise be empty or to substitute them into spots which would otherwise be used for other characters (perhaps for characters the font doesn't include anyway).

The result of this is that

\usepackage[<encoding>]{fontenc}


may not get you exactly <encoding>. It may get you a hacked version of <encoding>.

The font support files for the libertine package are produced using the autoinst script which can be configured to utilise empty slots in an encoding for additional ligatures. When fonts are prepared in this way, slots will not be reassigned to ligatures, but if an encoding has empty slots, these may be used for ligatures beyond those normally supported by the encoding.

Here's a mapping line from the .map file fragment for the package

LinLibertineT-tlf-ot1--base LinLibertineT "AutoEnc_c7kyj5lv7lwhdgytk3lalexyxf ReEncodeFont" <[lbtn_c7kyj5.enc <LinLibertineT.pfb


lbtn_c7kyj5.enc is the encoding file the line is using. When we exam,ine this file, we find

%00
/Gamma /Delta /Theta /Lambda /Xi /Pi /Sigma /Upsilon
/Phi /Psi /Omega /.notdef /.notdef /.notdef /.notdef /.notdef


and

%80
/f_i /f_f_i /f_f /f_l /f_f_l /f_b /f_h /f_j
/f_k /f_t /t_t /Q_u /T_h /f_f_h /f_f_j /f_f_k
%90
/f_f_t /exclamdbl /question_question /question_exclam /exclam_question /ellipsis /.notdef /.notdef
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef


In contrast, here are the corresponding parts of 7t.enc which corresponds to the standard OT1 encoding:

%00
/Gamma /Delta /Theta /Lambda /Xi /Pi /Sigma /Upsilon
/Phi /Psi /Omega /ff /fi /fl /ffi /ffl


and

%80
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
%90
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef


This is obviously not the standard OT1 encoding.

Here, autoinst has used slots in the encoding beyond 128. That is, it is essentially treating the encoding as 8 bit (256 slots) rather than 7 bit (128 slots), whereas the original TeX encodings were all 7 bit (128 slots), including OT1.

However, the T1 encoding is an 8 bit encoding (256 slots) already. Moreover, it does not have a single spare slot. Hence, autoinst cannot assign additional ligatures to spare slots because there are none.

In contrast, the LY1 encoding is an 8 bit encoding with some spare slots. In this case, autoinst assigns additional ligatures to spare slots until it exhausts the supply of slots. This means that it uses some of the additional ligatures, but not all. In particular, the fj ligature works with this encoding, whereas the Th and Qu ligatures do not.

The only real answer here is that you should choose the encoding which best suits your needs. Which depends on the content of your document and, in some unusual cases, the specifics of hacked encodings used by the fonts you load.

• A polite way of saying 'the libertine package is defective in OT1: slots are abused' :) – Joseph Wright Dec 27 '16 at 18:03
• @JosephWright Er ... well, I can't really say that, can I? Have you looked at what my packages do? That said, I do think that treating OT1 as 8 bit is strange and moving slots for the standard ligatures seems egregious. – cfr Dec 27 '16 at 18:22
• @JosephWright It is not defective. One (imho the only one) advantage of OT1 is that there are empty/unused slots which can be used for special effects. Alan Hoenig is making use of these slots in TeX Unbound a lot. – Ulrike Fischer Dec 27 '16 at 19:36
• @UlrikeFischer I don't see any reason to change the slots of the standard ligatures. But I would say that the documentation is defective. It ought to explain. Instead, it just tells users to use T1 or LY1, which isn't even necessarily the best choice here. – cfr Dec 27 '16 at 20:43
• Thanks for the explanation, which makes sense. I think I'll stick with T1, then, as I don't like the Th ligature, don't need the Qu one, and imagine that my only need for fj is the word fjord, which I don't often use. – dedded Dec 29 '16 at 16:19

At David's suggestion, I will turn my comment into an answer.

First: The above answers, regarding font encoding and modification, do work. A few years ago I hand-modified the LY1 encoding so that it would accomodate the "Th" ligature, and also pull in an alternate form of the em-dash, using Adobe Garamond Pro font. Worked great, but is tedious.

The more modern method is to forget about fontenc and OT1. Switch to LuaTeX. Most packages that work with ordinary pdfLaTeX also work with LuaTeX, right out of the box, without any special effort.

With LuaTeX, and with all text in utf-8, the fontspec package handles TrueType and Open Type fonts directly. If the desired ligatures are part of a font's "liga" (standard ligatures) feature set, then they will automatically be enabled. If instead they are "discretionary" ligatures (dlig feature) then it is easy to ask for them.

You don't need to know anything about LuaTeX, or the Lua language. The heavy lifting is done in the background. Have a look at the fontspec package documentation, to see what it can do. If you make a test run, be sure to specify that you want TeX Ligatures (described in fontspec) so that your previous text input will be compatible.

Once I discovered this, I'll never go back to the pre-LuaTeX methods again.

Works with XeTeX too (but I happen to need something that XeTeX cannot do).

% !TeX program = LuaLaTeX
% !TeX encoding = UTF-8
\documentclass{article}
\RequirePackage{fontspec}
\title{MWE}
\begin{document}
“This is a baffling fjord,” said Sven.
\end{document}


Result: Notice that ligatures Th ffl and fj are there. Also note that ordinary curly quotes were typed into the document, since it understands utf-8. If you try this with libertine, be sure to request "Linux Libertine O" (see that capital O) as the font.

• obviously +1 for doing as I suggested:-) – David Carlisle Dec 28 '16 at 17:17
• One day, I hope, I'll be using luatex. But it seems like I always have trouble when I try. (Most recently, attempting to use an answer from egreg, I couldn't get font scaling to work. Seems like I occasionally have troubles getting equations to look right, too.) In any case, that's why I put "pdflatex" right in the title of the question; I figured the easiest answer would be to use luatex. – dedded Dec 29 '16 at 16:22
• The simplest way is to use the libertine package in this case, since it is Lua/XeTeX aware. – cfr Dec 29 '16 at 17:00
• @dedded Yes, there are bugs and it is not as stable, either. – cfr Dec 29 '16 at 17:02
• @dedded However, avoiding unicode-math helps, at least. (But don't load the libertine package in this case - the simple way breaks it again.) – cfr Dec 29 '16 at 17:05

So I'm guessing that fontenc shouldn't be loaded with libertine. Is that correct?

Possibly, or possibly not depending on the requirements. The pdftex format as configured in miktex and texlive only has hyphenation patterns loaded for T1 encoding, so if you use an OT1 encoded font then hyphenation will only be correct in so far as OT1 and T1 coincide, which essentially means that it will be incorrect unless you are using a language such as English which makes no use of accented letters.

If you are writing in English, and would like the extra ligatures, then using OT1, is quite reasonable, in other languages losing correct hyphenation may be too high a price to pay for the ligatures and perhaps you would need to investigate a more complicated setup with a custom encoding.

• Perhaps the OP should switch to LuaTeX with fontspec? Then instead of using or requiring the libertine package, the Open Type fonts (Linux Libertine O) would be called directly, using \setmainfont. Now, we know that D.C. is a big fan of LuaTeX, so I wonder why it hasn't already been suggested. – user103221 Dec 28 '16 at 3:36
• @RobtA there is room for another answer:-) – David Carlisle Dec 28 '16 at 8:52