If you compile the following document with hyperref using XeLaTeX you will see that the encoding of the text commands is changed by thehyperref package to "PU" encoding. Why is this necessary?

\setmainfont{Linux Libertine O}



Are there any "gotchas" to watch?

1 Answer 1


If you don't load hyperref, then you'd get

macro:->\T1-cmd \textasciitilde \T1\textasciitilde

although the default encoding is EU1 (or EU2 with LuaLaTeX).

With hyperref you get

macro:->\PU-cmd \textasciitilde \PU\textasciitilde

because puenc.def has been loaded later. The macro \PU-cmd looks for two arguments and does

    \expandafter\ifx\csname ?\string#1\endcsname\relax
      \expandafter\def\csname ?\string#1\endcsname{\TextSymbolUnavailable#1}
      \csname ?\string#1\endcsname

The arguments are \textasciitilde and \PU\textasciitilde (which is one token).

In normal typesetting, \ifx\protect\typeset@protect is true, so \@inmathwarn\textasciitilde is expanded, but it does

\ifmmode \@latex@warning {Command \protect #1 invalid in math mode}\fi

so nothing happens unless we're in math mode. Then LaTeX checks whether


(one token) is defined which it is: \cf@encoding is expanded to the current output encoding. If it weren't, in the usual sense that \EU1\textasciitilde is equivalent to \relax or undefined, LaTeX would look for \?\textasciitilde (a default text command) and, if a definition for it is missing, an error message would be defined for further usage \EU1\textasciitilde would be globally made equivalent to \?\textasciitilde.

Finally, \EU1\textasciitilde is expanded.

In case the first conditional is false (that is, we're writing to files or in other special places), just \noexpand\textasciitilde would remain in the input stream.

You see that PU never intervenes, unless \cf@encoding is PU, which happens when hyperref is doing its work of writing the bookmarks.

  • So in a nutshell hyperref always uses its own encoding (PU) for bookmarks?
    – yannisl
    Aug 31, 2014 at 17:52
  • Yes, PU or PD1, depending on the engine or options to hyperref (with the unicode option PU is forced also for pdflatex). Also hyperlinks use the internal hyperref encoding, I guess.
    – egreg
    Aug 31, 2014 at 17:57
  • Yes, PD1 (PDFDocEncoding) or PU (Unicode/UTF16-BE). Bookmarks are written as simple strings into the PDF file without access to the font machinery of TeX. Aug 31, 2014 at 17:58

You must log in to answer this question.

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