Overlays with lstlistings

View: New views
2 Messages — Rating Filter:   Alert me  

Overlays with lstlistings

by pgbackup :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

How does one do overlays with listings? For example I would like the C code to be displayed from slide 3 onwards. I have tried putting the lstlisting in \uncover<3>{..} and \onslide<3>{} but it doesn't work. Also have looked at the documentation where it says it needs to be treated like verbatim but I don't understand. Any ideas? Thanks.

\documentclass{beamer}
\usepackage{listings}

\begin{document}

\begin{frame}[fragile]
        \frametitle{Example: Fibonacci Sequence (C)}
        \begin{lstlisting}[language=C,showstringspaces=false, frame=lines]
        int fibonacci()
        {
          int n = 8;            // nth Fibonacci number
          int f1 = 1, f2 = -1;  // last Fibonacci numbers
          while (n != 0)      // count down to n = 0
          {
            f1 = f1 + f2;
            f2 = f1 - f2;
            n = n - 1;
          }
          return f1;
        }
        \end{lstlisting}
        \begin{itemize}[<+->]
        \item $f_0 = 0$, $f_{1} = 1$
        \item $f_n = f_{n-1} + f_{n-2}$
        \item $f = 1, 1, 2, 3, 5, 8, 13, \dots$
        \end{itemize}
\end{frame}


\end{document}

Re: Overlays with lstlistings

by pgbackup :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


pgbackup wrote:
How does one do overlays with listings? For example I would like the C code to be displayed from slide 3 onwards. I have tried putting the lstlisting in \uncover<3>{..} and \onslide<3>{} but it doesn't work. Also have looked at the documentation where it says it needs to be treated like verbatim but I don't understand. Any ideas? Thanks.

\documentclass{beamer}
\usepackage{listings}

\begin{document}

\begin{frame}[fragile]
        \frametitle{Example: Fibonacci Sequence (C)}
        \begin{lstlisting}[language=C,showstringspaces=false, frame=lines]
        int fibonacci()
        {
          int n = 8;            // nth Fibonacci number
          int f1 = 1, f2 = -1;  // last Fibonacci numbers
          while (n != 0)      // count down to n = 0
          {
            f1 = f1 + f2;
            f2 = f1 - f2;
            n = n - 1;
          }
          return f1;
        }
        \end{lstlisting}
        \begin{itemize}[<+->]
        \item $f_0 = 0$, $f_{1} = 1$
        \item $f_n = f_{n-1} + f_{n-2}$
        \item $f = 1, 1, 2, 3, 5, 8, 13, \dots$
        \end{itemize}
\end{frame}


\end{document}

I was able to solve this problem by using \uncover<#>{\lstinputlisting[..]{filename}} so its working now.
LightInTheBox - Buy quality products at wholesale price!