Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 4 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 at 2:05
add comment

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.