[Tex/LaTex] \title after \begin{document}: When to do it

beamercatcodestikz-pgftitles

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}

\title{Hello \protect\alert{World}!} %<- before \begin{document}
\begin{document}
\maketitle
\end{document}

and next one works

\documentclass{beamer}
\usepackage{tikz}

\begin{document}
\title{Hello \protect\alert{World}!}%<- after \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?

Best Answer

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

enter image description here

while exampleB.pdf yields

enter image description here

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

\ifbeamer@autopdfinfo%
  \g@addto@macro\beamer@firstminutepatches
  {
    \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
\title{Hello \texorpdfstring{\protect\alert{world}}{world}!}

\begin{document}

\maketitle

\end{document}

Of course, you can also go overboard and use

\let\oldalert\alert% Store \alert in \oldalert
\let\alert\relax% Make \alert a null-op
\AtBeginDocument{\let\alert\oldalert}% Restore \alert at \begin{document}

or something similar for anything that may be problematic for inclusion in the PDF properties.