# Error using \newcommand within a beamer frame

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?

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

\documentclass{beamer}

\begin{document}

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

\end{document}

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