LaTeX beamer: Code listings in notes

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}

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}

For minted things are a tiny bit more complicated, which, however is covered in this question: workaround for minted environments in in Beamer notes


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.