10

For some reason my beamer presentation does not compile when \newcommand is placed inside the frame environment. Here is a minimal working example:

\documentclass{beamer}

\begin{document}

\begin{frame}{First slide}
  \newcommand{\asdf}[1]{What is the #1 problem?}
\end{frame}

\end{document}

I get the following error:

Illegal parameter number in definition of \test.

Note that the \asdf macro is not even used after it is defined. Strangely, when I place the definition outside of the frame environment, everything works fine. What is the reason for this strange behavior?

4
  • 1
    Put the definition outside of the frame.
    – user2478
    May 8, 2014 at 10:55
  • 1
    Put definitions into the preamble!
    – Keks Dose
    May 8, 2014 at 13:54
  • That's what I did when I realized that this is what was causing my presentation not to compile. I just wonder why is it the case and whether it should be considered as a bug in beamer. May 8, 2014 at 13:56
  • For what it's worth - this doesn't cause an issue if the newcommand has no optional arguments. (as for putting the definition into the preamble - this makes it hard to use particular graphics repeatedly in different presentations - each time you have to hunt through the preamble to get the relevant commands). I'm putting the commands right before \begin{frame}
    – Joel
    May 5, 2016 at 0:40

1 Answer 1

6

Adding fragile should solve the problem:

\documentclass{beamer}

\begin{document}

\begin{frame}[fragile]{First slide}
  \newcommand{\asdf}[1]{What is the #1 problem?}
\end{frame}

\end{document}
2
  • 2
    Since when can't you use overlays when the frame is fragile?
    – daleif
    May 8, 2014 at 10:49
  • 1
    Pity that this behavior is not documented in the beamer manual.
    – Yossi Gil
    Apr 27, 2015 at 16:04

You must log in to answer this question.

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