6

If I use \char in a section title, hyperref gives we either warnings or errors:

  • This breaks compilation:

    \subsection{Some thoughts about {\char`\\} {\char`\^} {\char`\$} {\char`\~} 
                {\char`\%} {\char`\&} {\char`\{} {\char`\}} {\char`\_} {\char`\#}}
    

    ./minimal.tex:9: Argument of \PU\^ has an extra }.
    <inserted text> 
                \par 
    
  • This compiles, with warnings:

    \subsection{Some thoughts about {\char`\\} {\char`\$} 
                {\char`\%} {\char`\&} {\char`\{} {\char`\}} {\char`\_} {\char`\#}}
    

    Package hyperref Warning: Token not allowed in a PDF string (Unicode):
    (hyperref)                removing `\char' on input line 9.
    

Can I teach hyperref about \char, so as to get Some thoughts about \^&~%&{}_# in my PDF bookmarks?

MWE:

\documentclass{article}

\usepackage{fontspec}
\usepackage[bookmarks=true]{hyperref}

\begin{document}
\subsection{Some thoughts about {\char`\\} {\char`\^} {\char`\$} {\char`\~} {\char`\%} {\char`\&} {\char`\{} {\char`\}} {\char`\_} {\char`\#}}
\end{document}

I am aware of Hyperref warning - Token not allowed in a PDF string and the \texorpdfstring command, but \texorpdfstring is confused about \char as well. I am also aware of other ways to quote individual characters (\textasciitilde etc.), but I would rather avoid them, if possible: I have trouble remembering them, and parts of my document are automatically exported from other formats.

  • just don't use \char here (see your other question:-) – David Carlisle Aug 8 '16 at 20:46
  • Exactly: I asked that other qestion for this reason. But is there really no way? Some of this code is auto-generated, and I'd love to tweak things on the LaTeX thing instead of patching a compiler. – Clément Aug 8 '16 at 20:56
  • \texorpdfstring{\char`\^}{caret}? – Henri Menke Aug 9 '16 at 12:50
3

Use texorpdfstring to provide an alternative definition for hyperref. To have a caret showing up in the PDF bookmarks, use \^{}.

\documentclass{article}
\usepackage[bookmarks]{hyperref}
\begin{document}
\section{\texorpdfstring{\char`\^}{\^{}}}
\end{document}

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.