# How can I put lstlisting block into subfloat block?

I'm trying to compile my document but it's exploding. I've narrowed it down to this section:

\begin{figure}[h]
\caption{These are my awesome code snippets}
\subfloat[This code runs every cycle.]{
\begin{lstlisting}
static uint64_t i = 0;
void every_cycle()
{
if (i > 0)
i--;
}
\end{lstlisting}
}
\hfill
\subfloat[This code runs whenever.]{
\begin{lstlisting}
uint64_t next_num()
{
return (i += 0x100);
}
\end{lstlisting}
}

\label{fig:algOffset}
\end{figure}


But, when I try to compile, I get

! Argument of \lst@next has an extra }.
<inserted text>
\par
l.181     }


From the lstlisting and subfloat documentation, I can't see anything I'm doing that should be disallowed. Why does this code fail to compile? Is there a better package to use than lstlisting for displaying code snippets?

-
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 7 at 10:56

It is possible to box the listings first, before using them in a \subfloat. Technically, this is probably similar to @Torbjørn's solution. Boxing is achieved via an lrbox environment.

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{subfig}% http://ctan.org/pkg/subfig
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\begin{document}

\lipsum[1]

% ======= STORE/BOX LISTINGS =======
\newsavebox{\firstlisting}
\begin{lrbox}{\firstlisting}% Store first listing
\begin{lstlisting}
static uint64_t i = 0;
void every_cycle()
{
if (i > 0)
i--;
}
\end{lstlisting}
\end{lrbox}
\newsavebox{\secondlisting}
\begin{lrbox}{\secondlisting}% Store second listing
\begin{lstlisting}
uint64_t next_num()
{
return (i += 0x100);
}
\end{lstlisting}
\end{lrbox}

\begin{figure}[h]
\caption{These are my awesome code snippets} \label{fig:algOffset}
\subfloat[This code runs every cycle.]{\usebox{\firstlisting}} \hfill%
\subfloat[This code runs whenever.]{\usebox{\secondlisting}}
\end{figure}

\lipsum[2]
\end{document}​

-

I do not know why the lstlistingsenvironment fails, but having the code in an external file and using \lstinputlisting{filename} instead works.

The filecontents* environment will write its content to code.txt (and overwrite the file should it exist). Thanks Werner, for the suggestion.

\documentclass{article}
\usepackage{listings}
\usepackage{subfig}
\usepackage{filecontents}

\begin{filecontents*}{code.txt}
static uint64_t i = 0;
void every_cycle()
{
if (i > 0)
i--;
}

uint64_t next_num()
{
return (i += 0x100);
}
\end{filecontents*}
\begin{document}
\begin{figure}[h]
\caption{These are my awesome code snippets}
\subfloat[This code runs every cycle.]{\lstinputlisting[lastline=6]{code.txt}}
\hfill
\subfloat[This code runs whenever.]{\lstinputlisting[firstline=8]{code.txt}}
\label{fig:algOffset}
\end{figure}
\end{document}


firstline=n and lastline=n does what you would think, it defines the first/last line in the file to be included in the listing.

-
"I do not know why the lstlistingsenvironment fails, but having the code in an external file and using \lstinputlisting{filename} instead works." I think that this is because subfloat and co do not like verbatim environments. –  WaelJ Jun 14 at 0:22