Graphics, Figures & TablesMultiple Listings in a single float

Information and discussion about graphics, figures & tables in LaTeX documents.
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:[…] To make this more clear I will change "\DeclareFloatingEnvironment" to "\DeclareFloatingEnvironment offered by the newfloat package" within this paragraph.
Sigh! I simply missed the forest for the trees.

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

Klausiber
Posts: 16
Joined: Sun Apr 14, 2013 11:41 pm

Multiple Listings in a single float

Post by Klausiber »

sommerfee wrote:
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?
Sure:

Code: Select all

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

\usepackage{listings}
\lstset{frame=single}

\usepackage{caption}

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

%\captionsetup[listing]{skip=4pt}

\begin{document}
  \begin{listing}
    \begin{lstlisting}[gobble=6]
      Listing
    \end{lstlisting}
    \caption{A Listing}
  \end{listing}

  \begin{figure}
    \centering
    \rule{\textwidth}{7mm}
    \caption{A Figure}
  \end{figure}

  This just makes both floats stick to the top of the page.

\end{document}
Note that I commented out the captionsetup line that fixes the problem for me.
Klausiber
Posts: 16
Joined: Sun Apr 14, 2013 11:41 pm

Multiple Listings in a single float

Post by Klausiber »

As forum won't let me edit my last post (maybe its too old) I'll add this in a new one:

I finally found the problem after it also occurred (but differently) with sublistings. The subcaptions were too far from the actual listing but too close to the main caption. The source of these problems is that before and after each lstlisting there is a fixed amount (\medskipamount to be precise) of vertical space. This can be prevented with

Code: Select all

\lstset{aboveskip=0pt,belowskip=0pt}
and everything looks fine again :-)
Post Reply