\title after \begin{document}: When to do it?

My answer to Problem using \alert{} in Beamer title with metropolis theme solves an incompatibility problem between TiKZ and \title with \protect-ed contents in beamer documents.

Following code fails:

\documentclass{beamer}
\usepackage{tikz}

\begin{document}
\maketitle
\end{document}


and next one works

\documentclass{beamer}
\usepackage{tikz}

\begin{document}
\maketitle
\end{document}


If the title is \title{Hello World!}, there's no problem placing it in preamble.

I found the answer by trial and error and because I remembered some similar answer in TeX.SX. I would like to know why the error happens and why the solution works.

While I was writting this question Should I place \title, \author, \date in the preamble or after \begin{document}? appeared in my browser. My resume from it is that it doesn't matter if title commands are declared in preamble or inside the document before \maketitle except when it matters. Then my question is when it matters? Only in beamer? when using \protect? when using TiKZ? Is there any general recipe?

• Notice the babel answer to the other question. A summary of that discussion could be: if the title is plain text there is no problem with the placement, if it contains formatting commands, shorthands, etc. then it can be necessary to put the \title command in the document body. – Andrew Swann Apr 19 '16 at 8:46
• Try \title{Hello \texorpdfstring{\alert{World}}{World}!} with the help of hyperref. – Ruben Apr 19 '16 at 9:00
• I guess the point is that beamer automatically uses the title as \hypersetup{pdftitle={\inserttitle\ifx\insertsubtitle\@empty\else\ - \insertsubtitle\fi}}. So if it tries to write the pdf info at the start of the document it encounters problems if control sequences etc. are part of the title. If the title is only defined after the informations are written to the pdf, it does not matter if the title contains something crazy. – user36296 Jul 14 '16 at 0:11

Ultimately the issue here, I think, boils down to two things:

1. If you use \title before \begin{document}, it ends up being incorporated in the PDF properties. After \begin{document} and such inclusion is too late.

2. PDF properties are sensitive since they can handle some TeX-related macros, but not everything. And, the beamer class does a lot to allow for its overlay specifications and templates. Specifically, \alert and everything defined with an overlay specification under \newcommand-and-friends will surely cause issues if they end up in the PDF properties.

To examine (1), consider the following minimal examples:

exampleA.tex:

\documentclass{beamer}

\title{Hello world!}% title before \begin{document}

\begin{document}

\maketitle

\end{document}


exampleB.tex:

\documentclass{beamer}

\begin{document}

\title{Hello world!}% title after \begin{document}

\maketitle

\end{document}


Both yield the same output, but the PDF properties are different. exampleA.pdf yields

while exampleB.pdf yields

This is because beamer sets the document option \beamer@autopdfinfotrue by default in beamer.cls and calls

\ifbeamer@autopdfinfo%
{
\begingroup
\let\beamer@saved@hook\pdfstringdefPreHook
\pdfstringdefDisableCommands{%
\let\\=\
\let\newline=\\%
}%
\let\thanks=\@gobble%
\hypersetup{pdftitle={\inserttitle\ifx\insertsubtitle\@empty\else\ - \insertsubtitle\fi}}
\global\let\pdfstringdefPreHook\beamer@saved@hook
\endgroup
}
\fi%


in beamerbasetitle.sty. Note the call to \hypersetup{pdftitle={...}} that \inserts the title.

In support of (2), you can redefine \alert to not use an overlay specification nor templates (say, \def\alert#1{\textcolor{red}{#1}}) and see that the number of errors during compilation decreases (or it actually compiling).

The proposed solution here would be to distinguish between PDF-related content and TeX-related content using \texorpdfstring{<tex>}{<pdf>}:

\documentclass{beamer}

%\usepackage{tikz}% With or without tikz

\begin{document}

\maketitle

\end{document}


Of course, you can also go overboard and use

\let\oldalert\alert% Store \alert in \oldalert