Packages: hyperref’s \texorpdfstring

If I use an equation in a section heading like this:

\documentclass[a4paper,12pt]{article}
\usepackage[hidelinks]{hyperref}

\begin{document}

\tableofcontents

\section{A section about $E=mc^2$}

One of the most well-known formulae has to be $E=mc^2$.

\end{document}

I will get this warning:

Package hyperref Warning: Token not allowed in a PDF string

The problem is not the formatting of a maths string in LaTeX, but rather in the creation of a bookmark in the PDF viewer, which the hyperref package will do for you. These bookmarks allow the reader to navigate through the document; this is especially useful for long documents – but not if you can’t have equations or other complex text in headings!

To allow hyperref to create bookmark-friendy text in section titles and things, use the \texorpdfstring{LaTeX text}{Bookmark Text} command, provided by hyperref [1]. Use alphanumeric and basic punctuation and spaces with no fancy formatting to create a meaningful title for your bookmark:

\documentclass[a4paper,12pt]{article}
\usepackage[hidelinks]{hyperref}

\begin{document}

\tableofcontents

\section{\texorpdfstring{A section about $E=mc^2$}{A section about E=mc2}}

One of the most well-known formulae has to be $E=mc^2$.

\end{document}

It doesn’t matter in this context that your equation isn’t formatted as prettily in the PDF bookmarks; what does matter is getting your bookmark there in the first place. The equation will still look beautiful in the document, which is where it counts.

References

  1. LaTeX/Hyperlinks at Wikibooks.org.

Add a comment?

This site uses Akismet to reduce spam. Learn how your comment data is processed.