292

Possible Duplicate:
Hyperref warning - Token not allowed in a PDF string

The following code:

\subsection{The classes $\mathcal{L}(\gamma)$}

generates the errors:

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


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


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

What do I have to do to keep \gamma in the subsection, so that it is also listed in the table of contents?

Chris
  • 4,965
  • 8
  • 22
  • 19

1 Answers1

428

The PDF bookmarks are a different thing than the table of contents. The bookmarks are not typeset by TeX: they simply are strings of characters, so no math or general formatting instructions are allowed.

The easiest method to avoid the warnings is to use \texorpdfstring:

\subsection{The classes \texorpdfstring{$\mathcal{L}(\gamma)$}{Lg}}

where in the second argument you put the best approximation possible; after all the bookmarks are only a guide for consulting the document.

egreg
  • 1,121,712
  • 9
    Ahh, I see, those are PDF bookmarks, thanks! – Chris Apr 26 '12 at 23:14
  • 3
    Thanks for answering this, even though the question was a duplicate: I couldn't tell if maths mode in section titles would work the same as in the other answers to this problem. – theHigherGeometer May 07 '14 at 00:39
  • In which package is texorpdfstring defined? – Eduardo J. Sanchez Jun 07 '16 at 13:52
  • @EduardoSanchez: there is no package to import. – galath Jun 20 '16 at 07:47
  • so, @egreg, why didn't you put parens around the "g" in \texorpdfstring{$\mathcal{L}(\gamma)$}{Lg}? they're there around the (\gamma) – barbara beeton Sep 23 '16 at 20:04
  • 4
    @barbarabeeton The best approximation possible is left to the reader to decide. – egreg Sep 23 '16 at 20:09
  • But PDF files even support JavaScript, and the bookmark texts can be in italic, bold or color. Why can't it support images? – Ooker Oct 31 '17 at 10:43
  • 1
    @Ooker I'm not saying it's impossible: but it takes someone who implements it. – egreg Oct 31 '17 at 10:46
  • 8
    Note that unicode, with the unicode option set for hyperref, allows a closer representation like ℒ(γ) for the pdf table of content. \subsection{The classes \texorpdfstring{$\mathcal{L}(\gamma)$}{ℒ(γ)}} works as expected, at least on my setup. This works even if unicode characters are not accepted in math mode. – Frédéric Grosshans Mar 20 '19 at 17:59
  • @FrédéricGrosshans this is quite nice (tested by adding a \PassOptionsToPackage{unicode}{hyperref} before starting my beamer document --- it should have a Q&A by itself, it's so useful. – Rmano Sep 06 '19 at 13:04
  • I have the same problem with a $...$ environment inside the title of a beamer template. What else can I do if the \texorpdfstring{$...$} does not work? – Diger May 04 '20 at 17:04
  • @Diger In what sense “does not work”? – egreg May 04 '20 at 17:12
  • When I use it, it still shows the error. – Diger May 04 '20 at 17:20
  • @Diger I used an underscore in the text option: \texorpdfstring{$t_2$}{t_2}. I changed it to \texorpdfstring{$t_2$}{t2} which creates no error. – Qbyte Sep 19 '20 at 14:18
  • 4
    @Qbyte Maybe \texorpdfstring{$t_2$}{t\_2}? – egreg Sep 19 '20 at 16:15
  • @egreg Thank you this worked perfectly! – Qbyte Sep 19 '20 at 16:37
  • put \usepackage{hyperref} and \hypersetup{pdfauthor={Name}} source (https://albertsk.org/2019/10/10/how-to-avoid-warning-messages-from-hyperref-package-in-latex/) – affernan Apr 14 '21 at 13:15
  • @Alex Not sure what your example should show. – egreg Apr 14 '21 at 15:46
  • Does this solution change the output or does it just hide the warning (I think this information would be great to include in the answer). – Marten Sep 22 '21 at 13:52
  • @Kvothe The solution uses different instructions for the LaTeX part and the bookmark. – egreg Sep 22 '21 at 15:41
  • @egreg, so will the output (i.e. the .pdf) be identical or not? – Marten Sep 22 '21 at 16:11
  • @Kvothe Identical to what? – egreg Sep 22 '21 at 16:16
  • @egreg, I now understand better what the answer does and that my question was kind of bad. I meant what is the difference in output between this solution and just ignoring the warnings, which still outputs a valid pdf with something for the bookmarks. The answer is that if you do not use \texorpdfstring some automatic converion seems to happen which is often not good. Some things come out reasonably well in plain text. Other expressions just disappear as apparently there was no good default plaintext alternative. – Marten Sep 23 '21 at 17:26
  • Great solution! I like the fact that it removes LaTeX warnings, which I want to keep at 0 (to get the green line at the end of my compile script :-)). – AstroFloyd Nov 15 '21 at 11:12