4
\documentclass{beamer}

\begin{document}
\begin{frame}[t]
%
{\gdef\x{Hello}}%
%
\x

\end{frame}
\end{document}

This code does not compile. xelatex 2023 gives error

Undefined control sequence. \end{frame}

But if I remove the percent signs, the code compiles.

I thought comments are non-essential. Why does it matter here?

1
  • 2
    That's because you've put the \gdef in the frame's title.
    – cfr
    Dec 1, 2023 at 3:14

1 Answer 1

7
\begin{frame}[t]
%
{\gdef\x{Hello}}%
%
\x

is equivalent to

\begin{frame}[t]{\gdef\x{Hello}}\x

which is starts a frame with option t, sets the frame's title to \frametitle{\gdef\x{Hello}} and puts \x in the body.

If you modify it to

  %
  {\gdef\x{Hello}\x}%
  %

you will get a frame with no body but the title 'Hello'. But this definition is not effective in the body of the frame. The same effect can be seen without the comment sign.

\begin{frame}[t]
  {\gdef\x{Hello}}%
  %
  \x

will also give the same error because \x is defined in the title rather than the body.

To avoid this, leave a blank line, remove the (here pointless) braces around \gdef, provide empty arguments for the frame title and subtitle

\documentclass{beamer}

\begin{document}
  \begin{frame}[t]{}{}
  {\gdef\x{Hello}}%
  %
  \x
  
\end{frame}
\end{document}

or stop TeX searching for arguments

\documentclass{beamer}

\begin{document}
  \begin{frame}[t]\relax
  {\gdef\x{Hello}}%
  %
  \x
  
\end{frame}
\end{document}

or otherwise make clear the definition is not the frame's title.

4
  • Oh oh, I'm blind. I forgot it is the place for title!
    – Gqqnbig
    Dec 1, 2023 at 3:34
  • 2
    @Gqqnbig If you used \frametitle{} you might not even have known. But it is easy not to see these things once you've convinced yourself the difference is some other thing.
    – cfr
    Dec 1, 2023 at 4:37
  • 1
    I dream of a time when “optional braced arguments” are declared abominable and banned from the face of earth. 🤭
    – egreg
    Dec 1, 2023 at 11:36
  • @egreg I think they're especially bad in cases like this: frame doesn't even limit itself to one. Even \begin{frame}{title}{\gdef\x...}\x produces an error. But I suppose it was hard to find alternative delimiters in this particular case.
    – cfr
    Dec 1, 2023 at 21:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .