4

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?

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
\usepackage{hyperref}

\begin{document}
\ttfamily
\meaning\textasciitilde\\
\meaning\textcelsius\\
\meaning\"\\
\meaning\NG\\
\meaning\k\\

\end{document}

Are there any "gotchas" to watch?

7

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

\ifx\protect\@typeset@protect
  \@inmathwarn#1%
  \expandafter\ifx\csname\cf@encoding\string#1\endcsname\relax
    \expandafter\ifx\csname ?\string#1\endcsname\relax
      \expandafter\def\csname ?\string#1\endcsname{\TextSymbolUnavailable#1}
    \fi
    \global\expandafter\let\csname\cf@encoding\string#1\expandafter\endcsname 
      \csname ?\string#1\endcsname
  \fi
  \csname\cf@encoding\string#1\expandafter\endcsname
\else
  \noexpand#1%
\fi

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

\EU1\textasciitilde

(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.

3
  • So in a nutshell hyperref always uses its own encoding (PU) for bookmarks? Aug 31 '14 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 '14 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 '14 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.