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



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


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?

  • 1
    Put the definition outside of the frame.
    – user2478
    May 8 '14 at 10:55
  • 1
    Put definitions into the preamble!
    – Keks Dose
    May 8 '14 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 '14 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 '16 at 0:40

Adding fragile should solve the problem:



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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.