Graphics, Figures & TablesMultiple Listings in a single float

Information and discussion about graphics, figures & tables in LaTeX documents.
Klausiber
Posts: 16
Joined: Sun Apr 14, 2013 11:41 pm

Multiple Listings in a single float

Post by Klausiber »

Hi!

I'm using the listings package to include some source code excerpts in my document. It offers the float option to create a float with the code inside.

Now, I need two snippets to be closely together because they demonstrate a code transformation. Normally I would use a subfigure but this will put the listings in the "List of Figures", not the "List of Listings". Can I change this somehow or is there some other way to combine two listing in a float?

MWE:

Code: Select all

\documentclass{article}

\usepackage{listings}
\lstset{
 frame=single,
 captionpos=b,
 frameround=tttt
}
\usepackage{caption,subcaption}
\pagestyle{empty}

\begin{document}

\listoffigures
\lstlistoflistings

\clearpage

\begin{figure}
  \begin{subfigure}{\linewidth}
    \centering
    \begin{lstlisting}
      Listing 1
    \end{lstlisting}
    \caption{Listing 1}
  \end{subfigure}

  \vspace{2em}

  \begin{subfigure}{\linewidth}
    \centering
    \begin{lstlisting}
      Listing 2
    \end{lstlisting}
    \caption{Listing 2}
  \end{subfigure}
  \caption{Two listings}
\end{figure}

\begin{lstlisting}[float, caption={Listing 3}]
  Listing 3
\end{lstlisting}

\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Multiple Listings in a single float

Post by localghost »

There is a possible solution. But this would require a new float type and make the float capabilities of the lstlisting environment obsolete. I suggest that we just start.

The first step would be to create listing as a new float environment by the newfloat package (from the caption bundle).

Code: Select all

\usepackage{newfloat}
\DeclareFloatingEnvironment[
  fileext=lol,
  name=Listing
]{listing}
The next step is to create the corresponding sub-float type by the subcaption package.

Code: Select all

\usepackage{subcaption}
\DeclareCaptionSubType{listing}
The final step is to put it all together into a sample document.

Code: Select all

\documentclass{article}
\usepackage[T1]{fontenc}

\usepackage{listings}
\lstset{
 basicstyle=\ttfamily,
 frame=single,
 frameround=tttt
}

\usepackage{caption}

\usepackage{newfloat}
\DeclareFloatingEnvironment[
  fileext=lol,
  name=Listing
]{listing}

\usepackage{subcaption}
\DeclareCaptionSubType{listing}

\begin{document}
  \listoflistings
  \clearpage

  \begin{listing}
    \begin{sublisting}{\linewidth}
      \begin{lstlisting}[gobble=8]
        Listing 1
      \end{lstlisting}
      \caption{Listing 1}
    \end{sublisting}\\[2ex]
    \begin{sublisting}{\linewidth}
      \begin{lstlisting}[gobble=8]
        Listing 2
      \end{lstlisting}
      \caption{Listing 2}
    \end{sublisting}
    \caption{Two listings}
  \end{listing}

  \begin{listing}
    \begin{lstlisting}[gobble=6]
      Listing 3
    \end{lstlisting}
    \caption{Third Listing}
  \end{listing}
\end{document}
Some things may need further customization e.g. by \captionsetup. In case of upcoming problems feel free to ask further questions. The manuals of the involved packages should already answer some queries.


Remarks:
  • Important note: If a listing is to float, the lstlisting environment has to be put into the new listing environment. All options related to floating attributes for the lstlisting environment should not be use anymore in order to have all listings in the new List of Listings (LoL) generated by the new \listoflistings command.
  • What has not been tested here is to add optional placement parameters as known from {table} and {figure}, e.g. [!htb].
  • Non-floating listings might get a caption by the \captionof command from caption. This has also not been tested here. But the usual caption option for the lstlisting environment might work also.

Best regards and welcome to the board
Thorsten
Klausiber
Posts: 16
Joined: Sun Apr 14, 2013 11:41 pm

Multiple Listings in a single float

Post by Klausiber »

Thanks a lot! This is perfect - even better than the float option as the new environment lets me specify a short caption for the "List of Listings" as well as a long one to put directly below the source code.

YFTR: The distance between the listing and its caption is a little bit wider than it is for standard floats (like figure). This looks weird if both type appear close to each other. I adjusted this distance by using captionsetup:

Code: Select all

...
\usepackage{caption}

\usepackage{newfloat}
\DeclareFloatingEnvironment[
  fileext=lol,
  name=Listing
]{listing}

\captionsetup[listing]{skip=4pt}
...
Any idea where the extra space comes from in the first place?
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Multiple Listings in a single float

Post by localghost »

Klausiber wrote:[…] even better than the float option as the new environment lets me specify a short caption for the "List of Listings" as well as a long one to put directly below the source code.[…]
According to Section 4.9 of the listings manual the caption option allows very well to add a short form for the list entry.
Klausiber wrote:[…] The distance between the listing and its caption is a little bit wider than it is for standard floats (like figure). This looks weird if both type appear close to each other. […] Any idea where the extra space comes from in the first place?
I can't really comprehend this. Actually the captions of the new float are set with a standard skip (6pt) like those of {figure} and {table}.
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

Multiple Listings in a single float

Post by sommerfee »

localghost wrote:The next step is to create the corresponding sub-float type by the subcaption package.

Code: Select all

\usepackage{subcaption}
\DeclareCaptionSubType{listing}
Just a short note that \DeclareCaptionSubType{listing} is superfluous (but does not make any harm) since this will be done for all new floating environments defined with \DeclareFloatingEnvironment automatically.
Klausiber wrote:YFTR: The distance between the listing and its caption is a little bit wider than it is for standard floats (like figure). This looks weird if both type appear close to each other.
This should not happen. Do you have a small but complete example document for me which shows this behavior?
Klausiber
Posts: 16
Joined: Sun Apr 14, 2013 11:41 pm

Multiple Listings in a single float

Post by Klausiber »

localghost wrote:According to Section 4.9 of the listings manual the caption option allows very well to add a short form for the list entry.
You are right. I think I must have misread this part or maybe I accidentally tried to put the short version outside of the braces.
localghost wrote:
Klausiber wrote:Any idea where the extra space comes from in the first place?
I can't really comprehend this. Actually the captions of the new float are set with a standard skip (6pt) like those of {figure} and {table}.
Probably the lstlisting adds some extra space. On the other hand I experimented with its framexbottommargin without any effect. Well, never mind. It's not too important.

Anyway, thanks again for your help!
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Multiple Listings in a single float

Post by localghost »

sommerfee wrote:Just a short note that \DeclareCaptionSubType{listing} is superfluous (but does not make any harm) since this will be done for all new floating environments defined with \DeclareFloatingEnvironment automatically. […]
Thanks for this information. This feature seems not to be documented in the subcaption manual. Or did I miss something?
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

Multiple Listings in a single float

Post by sommerfee »

localghost wrote:This feature seems not to be documented in the subcaption manual. Or did I miss something?
Quoted from the subcaption package documentation:
The \DeclareCaptionSubType command:
...
For the environments figure & table (and all the ones defined with \DeclareFloatingEnvironment) this will be done automatically by this package, but for others (e.g. defined with \newfloat offered by the float package or \DeclareNewFloatType offered by the floatrow package) this has to be done manually.
...
I know that my docs are still incomplete but at least this one is already documented.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Multiple Listings in a single float

Post by localghost »

sommerfee wrote:Quoted from the subcaption package documentation: […]
I have already read this passage, but couldn't spot information about objects defined with newfloat.
sommerfee wrote:[…] I know that my docs are still incomplete but at least this one is already documented.
From my point of view your documentations rank among the best. And completeness is not the most important criterion for me. Other packages don't have a manual at all. Furthermore I appreciate that you hand in such information in forums like this.
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

Multiple Listings in a single float

Post by sommerfee »

localghost wrote:
sommerfee wrote:Quoted from the subcaption package documentation: […]
I have already read this passage, but couldn't spot information about objects defined with newfloat.
To make this more clear I will change "\DeclareFloatingEnvironment" to "\DeclareFloatingEnvironment offered by the newfloat package" within this paragraph.
Post Reply