# Code listing in subfigure using lstlisting

I am trying to use the subfigure package of LaTeX to display a piece of SQL code. The purpose is similar to Code listing in subfigure, but I'd like syntax highlighting using lstlisting environment rather than verbatim. Here is what I'm trying:

\documentclass{article}
\usepackage{subfigure}
\usepackage{listings}

\begin{document}

\begin{table*}[ht]
\subfigure[Query 1]{
\begin{lstlisting}[language=SQL]
SELECT * FROM Table;
\end{lstlisting}
}
\end{table*}

\end{document}


It doesn't compile since the subfigure and verbatim packages clash. The linked post suggests a workaround for the verbatim environment, but what if I'd like to use lstlisting?

Box the listing using a lrbox:

\documentclass{article}
\usepackage{subfigure}
\usepackage{listings}

\begin{document}

\newbox\mybox
\begin{lrbox}{\mybox}
\begin{lstlisting}[language=SQL]
SELECT * FROM Table;
\end{lstlisting}
\end{lrbox}

\begin{table*}
\subfigure[Query 1]{\usebox\mybox}
\end{table*}

\end{document}


By the way, subfigure is an obsolete package; you should use subfig or subcaption instead. Your code using subfig:

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

\begin{document}

\newbox\mybox
\begin{lrbox}{\mybox}
\begin{lstlisting}[language=SQL]
SELECT * FROM Table;
\end{lstlisting}
\end{lrbox}

\begin{table*}
\subfloat[Query 1]{\usebox\mybox}
\end{table*}

\end{document}

• This works perfectly. Thank you. I can accept only in 5 minutes.
– Yang
Feb 13 '14 at 2:05