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.

3
  • just don't use \char here (see your other question:-) Aug 8, 2016 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, 2016 at 20:56
  • \texorpdfstring{\char`\^}{caret}? Aug 9, 2016 at 12:50

1 Answer 1

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}

You must log in to answer this question.

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