211

When compiling a beamer presentation and using the following \author command

\author{Name \\ \texttt{my.email@domain.com}}

I get the following hyperref warning in my logfile

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


Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref)                removing `\new@ifnextchar' on input line 15.

I understand that this has to do with hyperref setting the PDF metadata, where the linebreak does’t make much sense and should be removed.

Trying to set

\hypersetup{pdfauthor={Name}}

does’t change the situation, seems like hyperref is still looking at the author command.

How do I get the desired display of author name with email and still keep hyperref happy?

lockstep
  • 250,273

3 Answers3

213

There's the aptly, if verbosely, named macro \texorpdfstring, which takes two arguments and uses the first for (La)TeX and the second for pdf, so something like

\author{A.U. Thor\texorpdfstring{\\ foo@bar.baz}{}} 

should work.

The command is not defined in the document preamble, so \author{} must be specified after \begin{document} in this case.

(Yes, I've avoided the issue that I don't know off the top of my head if \url is allowed to go inside another argument...)

  • 1
    It's nice. There ought to exist an option in hyperref to automatically replace all non-allowed tokens by {} for the pdf string. Doing this for all my section titles is a hassle. – Arnaud Sep 03 '22 at 19:20
  • 1
    I used it in \author, before \begin{document}, and it worked. I also used a shorter syntax - \texorpdfstring{\\} – Zvika Jan 16 '23 at 12:13
101

While Ulrich's answer is correct and works, there is a more general and transparent way to work around the issue.

The \pdfstringdefDisableCommands command from the hyperref package can be used to redefine commands that are usually not supported in PDF bookmark strings.

In the OP example, the offending commands are \\ and \texttt, which can be redefined to do something else in this way:

\pdfstringdefDisableCommands{%
  \def\\{}%
  \def\texttt#1{<#1>}%
}

Then, the author can be specified in the document without any special care:

\author{Name \\ \texttt{my.email@domain.com}}

and will be formatted as is, but then put as Name <my.email@domain.com> in the PDF info strings.

Heiko Oberdiek
  • 271,626
  • 2
    Best answer. For me, the offender was glossaries. I had to do \pdfstringdefDisableCommands{% \def\Glsxtrshort{}% \def\glsxtrshort{}} – Marcel Jun 01 '22 at 17:29
  • What would be the syntax for the "math" command, e.g. \section{Local $d$-Privacy}? – Carlos Pinzón Sep 28 '23 at 09:51
  • In this case you can use the LaTeX way of inline math, i.e. \(d\). Then you can redefine \( and \) inside the command to expand to nothing – Nicola Gigante Sep 28 '23 at 20:35
44

One can also add

\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{naturalnames}{hyperref}

before \documentclass{beamer} to remove many messages generated due to national (non-English) section titles, like

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

P.S. Usually one may get up to few thousands of those even for a simple presentation, and parsing of them takes few extra seconds for many IDEs even on a modern box.