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:
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.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
:exampleB.tex
:Both yield the same output, but the PDF properties are different.
exampleA.pdf
yieldswhile
exampleB.pdf
yieldsThis is because
beamer
sets the document option\beamer@autopdfinfotrue
by default inbeamer.cls
and callsin
beamerbasetitle.sty
. Note the call to\hypersetup{pdftitle={...}}
that\insert
s thetitle
.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>}
:Of course, you can also go overboard and use
or something similar for anything that may be problematic for inclusion in the PDF properties.