0

I'm using the listings package liberally in an article. However, I want to show some code listings side-by-side and for those i use the subfigure package:

\begin{figure*}
    \begin{subfigure}{0.49\textwidth}
        \begin{lstlisting}
            ...
        \end{listing}
    \end{subfigure}            
    \begin{subfigure}{0.49\textwidth}
        \begin{lstlisting}
            ...
        \end{listing}
    \end{subfigure}
\end{figure*}

The problem here is that this is a figure, consequently the caption will be prefixed with "Figure X:" and LaTeX will use the figure environment's float counter rather than the listing environment's counter.

How do I fix this? I want LaTeX to treat the above code as essentially one (floating) listing.

3
  • 2
    start by showing a small but complete example that can be used to test the issue and solutions. Commented Jun 24 at 12:25
  • Since lstlisting supports float and caption options, it must create its own float type. The problem with a "one environment does everyting" approach is separating the float from the display. IIRC, they used to have separate listing and lstlisting environments. Commented Jun 24 at 13:33
  • Right, I'm using listings's builtin floats for simple listings. But the environment is insufficient for side-by-side code listings. Commented Jun 24 at 14:31

1 Answer 1

2

I tried using the built in lstlisting float type (\@float{lstlisting}[#1]) but could not get it to work with subcaptions. Specifically, every lstlisting increments the lstlisting counter, reseting the subcounter. So instead I created a separate float type for listing.

Note that this causes \lstlistoflistings to stop working.

\documentclass{article}
\usepackage{listings}
\usepackage{newfloat}
\usepackage{subcaption}

\DeclareFloatingEnvironment[fileext=lst, listname={List of Listings}]{listing}
\DeclareCaptionSubType{listing}

\begin{document}
\listoflistings

\begin{listing}[h]
\caption{Test}
\begin{sublisting}{\dimexpr 0.5\textwidth-0.5\columnsep}
  \begin{lstlisting}
  left listing
  \end{lstlisting}
  \caption{}
\end{sublisting}\hfill
\begin{sublisting}{\dimexpr 0.5\textwidth-0.5\columnsep}
  \begin{lstlisting}
  right listing
  \end{lstlisting}
  \caption{}
\end{sublisting}
\end{listing}

\end{document}

Of course, if you don't want subcaptions, the original solution works fine.

\documentclass{article}
\usepackage{listings}

\makeatletter
\newenvironment{listing}[1][htp]{\@float{lstlisting}[#1]}{\end@float}
\newenvironment{listing*}[1][tp]{\@dblfloat{lstlisting}[#1]}{\end@dblfloat}
\makeatother

\begin{document}
\lstlistoflistings

\begin{listing}
\begin{minipage}{\dimexpr 0.5\textwidth-0.5\columnsep}
  \begin{lstlisting}[caption=Test A]
  left listing
  \end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{\dimexpr 0.5\textwidth-0.5\columnsep}
  \begin{lstlisting}[caption=Test B]
  right listing
  \end{lstlisting}
\end{minipage}
\end{listing}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .