TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 6 down vote accepted

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}

enter image description here

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}
share|improve this answer
    
This works perfectly. Thank you. I can accept only in 5 minutes. – Yang Feb 13 '14 at 2:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.