[Tex/LaTex] Error using \newcommand within a beamer frame

beamermacros

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?

Best Answer

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}
Related Question