[Tex/LaTex] Beamer – Using \pause within lstlisting

beamerlistingspause

I am preparing a beamer presentation and using the lstlisting environment in order to put in code. However, I would like to use the \pause command within lstlisting. By definition that is not possible. I am numbering the lines so it is bad if I just end and begin the environment again in order to insert \pause. Any tips?

Best Answer

You can escape to LaTeX (Section 4.14 Escaping to LaTeX of the listings documentation) and use \pause:

\documentclass{beamer}
\usepackage{listings}
\usepackage{bera}

\lstset{basicstyle=\small\ttfamily,
numbers=left,
escapeinside=||
}
\begin{document}

\begin{frame}[fragile]
\begin{lstlisting}
line1 |\pause|
line2 |\pause|
line3
\end{lstlisting}
\end{frame}

\end{document}