# Subfigure with lstlisting: what am I doing wrong?

It keeps telling me: \begin{document} ended by \end{figure}, but I do not understand why. I tried to follow the guidelines for the subfigure package. Here is my MWE:

\documentclass[10pt,journal,a4paper]{IEEEtran}
\usepackage{listings}
\usepackage[pdftex]{graphicx}
\usepackage{subfig}
\usepackage{algorithmic}
\begin{document}

\begin{figure}%
\centering
\begin{subfigure}
nothing in it
\end{subfigure}
\begin{subfigure}
\begin{lstlisting}
The code
With a line
\end{lstlisting}
\end{subfigure}
\caption{A program and its corresponding trace.\label{figuretrace}}
\end{figure}
\end{document}


I also tried the command \subfigure without success. I just would like to be able to embed a lstlisting environment inside a subfigure.

What is wrong?

EDIT

Another environment, algorithm, does not work either:

\begin{subfigure}
\begin{algorithm}
The code
With a line
\end{algorithm}
\end{subfigure}


Nor does this work with a table, tabular... Why not?

• I'm pretty sure \lstlisting is a verbatim environment. In that case, it can't be stuffed inside another environment. Perhaps the verbatimbox package would give an acceptable alternative to lstlisting for your situation. May 30 '13 at 13:48
• David Carlisle may have a solution to the algorithm problem at tex.stackexchange.com/questions/110206/…. Once you save it in a box, you can regurgitate the box in the subfigure. May 30 '13 at 14:10
• Please read the IEEEtran-HOWTO. The usage of subfigure is not recommended; you should use subfig instead (pags 9 and 10 of the HOWTO). Also the algorithm floating environment shouldn't be used with this class (page 10 of the HOWTO document). May 30 '13 at 14:10
• subfig yields the same errors in my case. I updated the question. May 30 '13 at 14:14

using my verbatimbox suggestion from the comment:

\documentclass[10pt,journal,a4paper]{IEEEtran}
\usepackage{verbatimbox}
%\usepackage{listings}
\usepackage[pdftex]{graphicx}
\usepackage{subfigure}
\begin{document}

\begin{verbbox}
The code
With a line
\end{verbbox}
\begin{figure}%
\centering
\begin{subfigure}
nothing in it
\end{subfigure}
\begin{subfigure}
\theverbbox
\end{subfigure}
\caption{A program and its corresponding trace.\label{figuretrace}}
\end{figure}
\end{document}


• Good solution. I need a refinement (see my question) May 30 '13 at 13:55
• @MikaëlMayer In my toolbox, I have a hammer with verbatimbox written on it, but I can't find my algorithm hammer at the moment. May 30 '13 at 14:04

Please follow the recommendation from the IEEEtran-HOWTO document

1. The use of the obsolete subfigure package is not recommended; you should use subfig instead (pags 9 and 10).

2. The algorithm floating environment from the algorithm or algorihtm2e packages shouldn't be used with this class (page 10).

In the following example I used the subfig package (loaded as recommended in the HOWTO document) to include the subfigures; the listing was saved in a lrbox first, and then the box was used inside the \subfloat command:

\documentclass[10pt,journal,a4paper]{IEEEtran}
\usepackage{listings}
\usepackage{graphicx}
\ifCLASSOPTIONcompsoc
\usepackage[caption=false,font=normalsize,labelfont=sf,textfont=sf]{subfig}
\else
\usepackage[caption=false,font=footnotesize]{subfig}
\fi

\lstset{basicstyle=\ttfamily\small}

\begin{document}

\newsavebox\mybox
\begin{lrbox}{\mybox}
\begin{lstlisting}
The code
With a line
\end{lstlisting}
\end{lrbox}

\begin{figure}
\centering
\subfloat{%
nothing in it