[Tex/LaTex] How to insert listing inside frame and between enumerate

#enumeratebeamerlistingspresentationsxetex

I'm creating a presentation and I want to add a one line code in the presentation between each itemize element. You can see below what I want to achieve.

\documentclass{beamer}

\mode<presentation> {
\usetheme{Madrid}
}
\usepackage{listings}
\begin{document}
\begin{frame}
\frametitle{Test Slide}

\begin{enumerate}
\item Item 1
\begin{lstlisting}[language=bash]
   echo "Item 1"
\end{lstlisting}

\item Item 2
\begin{lstlisting}[language=bash]
   echo "Item 2"
\end{lstlisting}

\end{enumerate}
\end{frame}
\end{document}

But, when I try to build the PDF, this creates a few errors which I cannot figure. Any idea how to solve the problem?

Best Answer

Add the fragile option to your frame (section 12.9 "Verbatim and Fragile Text", beamer user guide):

\documentclass{beamer}

\mode<presentation> {
\usetheme{Madrid}
}
\usepackage{listings}
\begin{document}
\begin{frame}[fragile]
\frametitle{Test Slide}

\begin{enumerate}
\item Item 1
\begin{lstlisting}[language=bash]
   echo "Item 1"
\end{lstlisting}

\item Item 2
\begin{lstlisting}[language=bash]
   echo "Item 2"
\end{lstlisting}

\end{enumerate}
\end{frame}
\end{document}