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:

\subcaptionbox{Subcaption a.}%
\subcaptionbox{Subcaption b.}
\caption{Main caption.}

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.


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:


The MWE code:







add x1, x1, 5
stp fp, lr, [sp, #-16]!
add x5, x6, x7
    \captionof{sublisting}{Some caption.}
ldr x5, [x5]
    \captionof{sublisting}{Some other caption.}
\caption{Some text.  The quick brown fox\ldots}

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

  • 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.