Overlays with Code Listings

You cannot include a lstlisting (package listings) in a only or visible command in LaTeX beamer. BUT you can define the listing beforehand and then include that inside the only or visible!

Example (from slides about recursion in Java):

\defverbatim{\Lst}{
\begin{lstlisting}
public int fakultaet(int n) {
   if ( n == 1 ) {
      return 1;
   } else {
      return n * fakultaet( n-1 ) ;
   }
}
\end{lstlisting}
}

\begin{frame}[fragile]
\frametitle{Aufgabe: Fakultät von $n$}

Definition:
\begin{itemize}
   \item \lstinline{fakultaet( 1 ) = 1}
   \item \lstinline{fakultaet( n ) = n * fakultaet( n-1 ) }
\end{itemize}

\bigskip

Java Code: 
\visible<2|handout:0>{\Lst}

\end{frame}
This entry was posted in LaTeX and tagged , , by swk. Bookmark the permalink.

About swk

I am a software developr, data scientist, computational linguist, teacher of computer science and above all a huge fan of LaTeX. I use LaTeX for everything, including things you never wanted to do with LaTeX. My latest love is lilypond, aka LaTeX for music. I'll post at irregular intervals about cool stuff, stupid hacks and annoying settings I want to remember for the future.