7

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.

Clément
  • 4,004

1 Answers1

4

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}
Henri Menke
  • 109,596