LaTeX warning "Token not allowed in a PDF string (Unicode)": cause and fix
hyperref copies every heading into the bookmarks of the PDF, the outline in the side panel of a PDF viewer. A bookmark is plain text, so hyperref removes what plain text cannot hold and names it in the next line of the log: "math shift" is a $, "superscript" a ^, "subscript" a _. The heading on the page is not affected.
Why it happens
- A heading contains a formula, for example
\section{The formula $E = mc^2$}. - A heading contains a line break
\\, a\footnoteor a symbol command such as\alphaor\rightarrow. pdftitleorpdfauthorin\hypersetupcontains math or such commands.
How to fix it
- Give both versions with
\texorpdfstring{for the page}{for the bookmark}:\section{The formula \texorpdfstring{$E = mc^2$}{E = mc2}}. - Or pass a plain version in the optional argument:
\section[The formula E = mc2]{The formula $E = mc^2$}. That text also goes into the table of contents and the running head. - If the bookmark reads well without the removed parts, leave the warning as it is.
A document that produces the message
\documentclass{article}
\usepackage{hyperref}
\begin{document}
\section{The formula $E = mc^2$}
Text.
\end{document}The corrected document
\documentclass{article}
\usepackage{hyperref}
\begin{document}
\section{The formula \texorpdfstring{$E = mc^2$}{E = mc2}}
Text.
\end{document}Both documents were compiled with pdfLaTeX from TeX Live 2023. The first produces the message, the second compiles without an error.
Finding the line in the editor
The editor lists every error and warning of a compile run with file and line. Select an entry and the cursor jumps to that line. See Error list with jump to the line.
Related pages
- Package
hyperref - Package
bookmark - How-to Set PDF metadata in LaTeX
- How-to Sections, subsections and chapters in LaTeX