# hyperref option pdfusetitle with thanks

What is the cleanest way to set the title in the document and in the PDF metadata (while not repeating myself) when also using \thanks?

This solution produces an incorrect Title metadata, that includes the content of the \thanks (or warnings from hyperref: “Token not allowed in a PDF string” when not using \maketitle).

\documentclass{article}
\usepackage[pdfusetitle]{hyperref}

\begin{document}
\title{The title\thanks{Thanks}}
\maketitle
Hello.
\end{document}


Therefore I have considered the following “solution” (rather a workaround, I’d say).

\documentclass{article}
\usepackage{hyperref}

\begin{document}
\newcommand{\mytitle}{The title}
\title{\mytitle\thanks{Thanks}}
\hypersetup{pdftitle=\mytitle}
\maketitle
Hello.
\end{document}


But this clutters my document and is not elegant, especially because I can’t hide it in a preamble. Is it possible instead, and would it be cleaner, to patch the way pdfusetitle works? Or is there perhaps some other workaround?

• What prevents you from sticking from \newcommand{}...\hypersetup{} in the preamble for your workaround? – aoi Dec 17 '18 at 15:46
• @aoi I want to hide those complexities (in a different sub-document \included in the main document) from the redactor, who would only have to deal with the main document. Putting these three lines in that sub-document does not work as the redactor now has to open the sub-document to specify the title inside the \newcommand{\mytitle}{}. – Olivier Cailloux Dec 17 '18 at 16:15

hyperref defines the command \pdfstringdefDisableCommands that can be used to redefine certain problematic commands for use in pdfstrings. With

\pdfstringdefDisableCommands{%
\let\thanks\@gobble
}


you can completely remove/ignore \thanks in pdfstrings. With

\pdfstringdefDisableCommands{%
\def\thanks#1{ (#1)}%
}


you would add the argument of \thanks in parentheses.

\documentclass{article}
\usepackage[pdfusetitle]{hyperref}

\pdfstringdefDisableCommands{%
\let\thanks\@gobble
}

\begin{document}
\title{The title\thanks{Thanks}}
\maketitle
Hello.
\end{document}


Note how it is not necessary to enclose the code in \makeatletter...\makeatother as usually required when working with macros with @ in their name (cf. What do \makeatletter and \makeatother do?). \pdfstringdefDisableCommands applies some trickery to avoid the explicit catcode changes, but that trick does not work when \pdfstringdefDisableCommands is used as argument of another command, in that case you must wrap everything into \makeatletter...\makeatother, e.g.

\newtoggle{LCpres}
\togglefalse{LCpres}
\makeatletter
\iftoggle{LCpres}
{\usepackage{hyperref}}
{\usepackage[hypertexnames=false, pdfusetitle]{hyperref}
\pdfstringdefDisableCommands{\let\thanks\@gobble}}
\makeatother


For more complicated constructions there is \texorpdfstring, which could be used as \texorpdfstring{The title\thanks{Thanks}}{The title} with repetitions.

For different use cases something like Redefine \title for possibility of a forced line breaking in a predefined place might also be possible.

• Brilliant. But it does not play well with iftoggle, any idea why? \newtoggle{LCpres} \togglefalse{LCpres} \iftoggle{LCpres}{ \usepackage{hyperref} }{ \usepackage[hypertexnames=false, pdfusetitle]{hyperref} \pdfstringdefDisableCommands{ \let\thanks\@gobble } } – Olivier Cailloux Dec 17 '18 at 16:29
• @OlivierCailloux Could you open a new question with complete MWE about that, please? – moewe Dec 17 '18 at 16:30
• @OlivierCailloux Got it! You'll need \makeatletter...\makeatother. – moewe Dec 17 '18 at 16:34
• Usually \makeatletter...\makeatother is always needed when one uses macros with @ in their name like \@gobble. But hyperref tries to be a user friendly and defines \pdfstringdefDisableCommands in such a way that \makeatletter...\makeatother is not necessary. The trick used to do that, however, only works when pdfstringdefDisableCommands is not passed as argument to a different macro. – moewe Dec 17 '18 at 16:36
• Bug report introduced. – Olivier Cailloux Dec 19 '18 at 15:26