3

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?

7

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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