3

I want to have 2 listings side-by-side in a float environment with its own name (i.e. Listing 1 instead of Figure 1) and counter (so that I can have a separate listoflistings besides listoffigures). I also want the subcaptions aligned and not the listings themselves.

This is about what I'd imagine:

Small picture to show what I have in mind.

I don't see how to get the captions aligned with the subfigure package, but it works fine with the subcaption package:

\begin{figure}
\centering
\subcaptionbox{Subcaption a.}%
  [.47\textwidth]{\includegraphics[width=.45\textwidth]{imga}}
\subcaptionbox*{}[.03\textwidth]{}
\subcaptionbox{Subcaption b.}
  [.47\textwidth]{\includegraphics[width=.45\textwidth]{imgb}}
\caption{Main caption.}
\end{figure}

The problem is that I can't include lstlistings in the subcaption and that I still don't know how to get the separate counter and name from figure.

Related question about putting 2 listings side-by-side, which isn't what I have in mind.

5

I put this together to demonstrate how you can do this. Since you did not give a full example, I can't guarantee that it will fit right into what you are doing.

I used newfloat to create a new float type listing, as you seem to have. minipages are used to set the listings themselves into the sub-floats. Depending on what you have already, this may be enough to fix your issues.

I also augmented the example to show referencing and a list of listings. Within this bigger example, two problems still present themselves:

  1. The counter on subrefs is not set correctly; I have added a \refstepcounter to temporarily add 1 to the count to correct this.

  2. The \listoflistings puts the two sublisting captions above the main caption. I'm not sure yet why this is happening of if it can be corrected. The fix/hack would be to put the main caption at the top of the float, but that would move it in the final typesetting to the top as well, which may not be desired.

Even with these irregularities, a very nice result is obtained:

page

The MWE code:

\documentclass{article}

\usepackage{listings}
\usepackage{newfloat,caption}
\usepackage{subcaption}
\usepackage[demo]{graphicx}

\DeclareFloatingEnvironment[fileext=frm,placement={!ht},name=Listing,within=section]{listing}


\begin{document}

\tableofcontents
\listoflistings

\section{One}
\section{Two}

\begin{listing}
\refstepcounter{listing}
\noindent\begin{minipage}[b]{.45\textwidth}
    \begin{lstlisting}
add x1, x1, 5
stp fp, lr, [sp, #-16]!
add x5, x6, x7
    \end{lstlisting}
    \captionof{sublisting}{Some caption.}
    \label{lst:codeblockA}            
    \end{minipage}%
    \hfill
\begin{minipage}[b]{.45\textwidth}
    \begin{lstlisting}
ldr x5, [x5]
    \end{lstlisting}
    \captionof{sublisting}{Some other caption.}
    \label{lst:codeblockB}            
\end{minipage}
\addtocounter{listing}{-1}
\caption{Some text.  The quick brown fox\ldots}
\label{lst:codeblocks}
\end{listing}

Figure~\ref{lst:codeblocks} includes block \ref{lst:codeblockA} and \ref{lst:codeblockB} 

\end{document}
  • This answer has not received enough attention/upvotes... – jub0bs Apr 3 '14 at 13:35
  • 1
    Nice, I can create an extra command that already includes the refstepcounter but otherwise that's exactly what I want. And it's actually surprisingly readable compared to what I expected! – Voo Apr 3 '14 at 22:48

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.