3

I am currently writing a thesis which is full of math equations and I always get the warning

Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):

I have read that the solution is by using \texorpdfstring for each section, however, I was wondering if it could be possible to use one command for the whole document which will definitely improve the readability of the .tex file.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • 4
    it is only important for the bookmarks. If you do not really need it then igore it. –  Jul 30 '14 at 12:27
  • Did the mentioned solution work for you? If yes, we can introduce some "for every section: \texorpdfstring"-magic, I guess. But for that, you have to post an example as minimal as possible which results in this warning. – LaRiFaRi Jul 30 '14 at 13:30
  • I think you can use \pdfstringdefDisableCommands to add code that disable commands when generating the bookmark. – Nicola Talbot Jul 30 '14 at 13:37
  • where should this "\pdfstringdefDisableCommands" be added? – user45114 Jul 31 '14 at 13:28

1 Answers1

7

Since you mentioned equations, here's a MWE containing some:

\documentclass{report}

\usepackage[colorlinks]{hyperref}

\begin{document}

\tableofcontents

\chapter{A Sample Chapter \ensuremath{\sin a+\cos b=c}}

\chapter{Another Sample Chapter $\sin a + \cos b = c$}

\end{document}

This produces a load of warnings such as:

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


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


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

In each case the warning identifies the problematic token. You can use \pdfstringdefDisableCommands to disable these tokens while the bookmarks are being created.

In the MWE, the first heading uses \ensuremath (which isn't recommended, but this is for illustrative purposes). This can be disabled so that it ignores its argument like this:

\documentclass{report}

\usepackage[colorlinks]{hyperref}

\pdfstringdefDisableCommands{\let\ensuremath\@gobble}

\begin{document}

\tableofcontents

\chapter{A Sample Chapter \ensuremath{\sin a+\cos b=c}}

\chapter{Another Sample Chapter $\sin a + \cos b = c$}

\end{document}

This now suppresses the warnings for the first \chapter (but not the second) and the equation is discarded from the bookmark. You can use a similar method for other problematic commands. You can use \@gobble in the \let assignment to discard the argument (as in the above) or \@firstofone to discard the command and just put the argument in the bookmark or \@empty to discard a command that doesn't have an argument.

The second \chapter is a bit more problematic. Do you want to completely discard the equation (as in the first case with \ensuremath above) or do you want to try to approximate the equation in the bookmark? The following will make \sin and \cos just display sin and cos but you'll still get a warning about the math shift:

\documentclass{report}

\usepackage[colorlinks]{hyperref}

\pdfstringdefDisableCommands{%
 \let\ensuremath\@gobble
 \def\sin{sin }%
 \def\cos{cos }%
}

\begin{document}

\tableofcontents

\chapter{A Sample Chapter \ensuremath{\sin a+\cos b=c}}

\chapter{Another Sample Chapter $\sin a + \cos b = c$}

\end{document}

This just leaves the warnings:

Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref)                removing `math shift' on input line 17.


Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref)                removing `math shift' on input line 17.

However, you do at least get a slightly more intelligible set of bookmarks as shown below:

Image of bookmarks and table of contents

Nicola Talbot
  • 41,153