6

I am trying to generate pseudocode in both presentation slides and handout using Beamer.

I can use the listings package in the slides by adding [fragile] to the frame environment. However, how can I use the lstlistings environment when in article mode? Below is a minimal example.

File: slides.tex

\documentclass{beamer}
\input{mwe-body.tex}

File: notes.tex

\documentclass{article}
\usepackage{beamerarticle}
\input{mwe-body.tex}

File: mwe-body.tex

\usepackage{listings}
\begin{document}

\begin{frame}[fragile]
\frametitle{Frame 1}
test 1
\begin{lstlisting}
    int sum = 0;
\end{lstlisting}
\end{frame}

\mode<article>{
test 2

\begin{lstlisting}
    int sum = 1;
\end{lstlisting}
}
\end{document}

pdflatex slides.tex works to generate the presentation.

pdflatex notes.tex has the following error:

Runaway argument?
! Paragraph ended before \lst@next was complete.
<to be read again>
               \par
l.18 }

Any solutions?

1 Answer 1

5

This is typical of verbatim-like arguments - you cannot pass them as argument to other macros without care. As noted, [fragile] provides the necessary care when using lstlisting inside the frame environment. For using lstlisting inside <article> mode, I'd suggest capturing the listing inside a box first:

enter image description here

\documentclass{article}

\usepackage{beamerarticle}
\usepackage{listings}

\newsavebox{\codebox}

\begin{document}

\begin{frame}[fragile]

  \frametitle{Frame 1}

  test 1
\begin{lstlisting}
int sum = 0;
\end{lstlisting}

\end{frame}

% Store listing inside \codebox
\begin{lrbox}{\codebox}
\begin{lstlisting}
int sum = 1;
\end{lstlisting}
\end{lrbox}

\mode<article>{
  test 2

  \noindent
  \usebox{\codebox}% Print stored listing
}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .