LaTeX beamer: Code listings in notes

How can I add a code listing to a beamer note slide?

I tried the following which doesn't work:

\note{{\tiny{This is a note
\begin{lstlisting}
foo bar
\end{lstlisting}}}

Any suggestions?

-
I've never worked with beamer \note. Could you please add a minimal example for your problem which compiles? –  Matten Dec 14 '10 at 8:03
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Mar 1 '14 at 19:20

Welcome! One problem with the snippet you provide is an unmatched brace. But even so, like verbatim, lstlisting does strange things with category codes of characters which usually precludes it from being in the argument of another macro.

Inspired by this post to the beamer mailing list, I found a workaround with the \lstinputlisting command. You have to put your code snippet in a file, but using the linerange options you can excerpt multiple snippets from the same file.

\documentclass{beamer}
\setbeameroption{show notes}
\usepackage{listings}

\begin{document}

\begin{frame}
first frame
\note{\lstinputlisting[language=tex,linerange=7-10]{Untitled.tex}}
\end{frame}

\end{document}
-
Awesome! Thank you very much! Unfortunately I don't have the required reputation to vote your answer up. But I would definitely would… –  Normen Müller Dec 16 '10 at 6:30
@Normen: Glad it worked for you. You can accept the answer anytime. You need 15 rep for upvoting. You get 5 for asking a question that's upvoted and 2 for accepting an answer. So you're nearly there already. :-) –  Matthew Leingang Dec 16 '10 at 13:24

I highly recommend the "minted" package for all code listings. Note that if you use this on a beamer slide, it must be fragile:

\usepackage{minted}
\begin{frame}[fragile]{Python rocks}
\begin{minted}{python}
def foo(self, bar):
return bar
\end{minted}
\end{frame}

Note: The minted package needs Pygments for syntax highlighting.

-
Oh, that sounds interesting. I tried it OMM and I got a message that says that 'pygmentize' has to be installed. What is that and how would one install it? –  Matthew Leingang Dec 14 '10 at 19:44
@Matt: Welcome to tex.sx! A tip: If you indent lines by 4 spaces, then they're marked as a code sample. You can also highlight the code and click the 'code' button (101010). –  Hendrik Vogt Dec 15 '10 at 10:37
@Matt: Does minted'' solve this issue? Or is it just a general recommendation. I can't switch directly, though, I already have created a bunch of specialized listing style files. –  Normen Müller Dec 15 '10 at 14:15
@Matt pygmentize is a program for code highlighting and typesetting. Its also used by Python documentation. minted is the wrapper for LaTeX. You will also need -shell-escape on your command line. –  Matt Knepley Dec 16 '10 at 18:07
@Normen It should do what you want. Did you have problems? –  Matt Knepley Dec 16 '10 at 18:07

Using \lstinputlisting as suggested by Matthew is one possibility. However, if you (like me) prefer to not have dozens of small listing files separated from the LaTeX sources, you could also render the listing into a lrbox box and \usebox this inside the \note:

\newsavebox{\LstA}

% Verbatim material in commands (\note{}) not possible --> box it
\begin{lrbox}{\LstA}
\begin{lstlisting}[language=C]
int fak( int n ) {
for( int i = n-1; i > 1; --i )
n *= i;
return n;
}
\end{lstlisting}
\end{lrbox}

\begin{frame}{Recursive Algorithm}

\note{ Iterative solution:\par\usebox{\LstA}}
\end{frame}
-