Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
    
So in a nutshell hyperref always uses its own encoding (PU) for bookmarks? –  Yiannis Lazarides Aug 31 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 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. –  Heiko Oberdiek Aug 31 at 17:58
    
@HeikoOberdiek Gents thanks I am accepting the answer. –  Yiannis Lazarides Aug 31 at 18:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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