Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Get equal above/belowskip regardless of float for lstlisting

20 views
Skip to first unread message

Paul J. Lucas

unread,
Oct 27, 2024, 9:06:10 PM10/27/24
to latexus...@googlegroups.com
When using the listings package, for some listings I do not want to use float, i.e., I want the listing "inline." The minimal example below shows such a listing. The first lstlisting does not use float and the spacing above/below it is correct, i.e., a blank line.

For longer listings, however, I do want to use float. The second lstlisting does use float, but the spacing above/below it is incorrect — specifically, it's too big as if it were two blank lines.

(The length of the second listing is short since that's all that's necessary to illustrate the problem.)

I want the spacing above/below listings with float to be the same as those without. How can I get what I want?

- Paul

\documentclass[10pt]{book}
\usepackage{courier}
\usepackage[T1]{fontenc}
\usepackage[flushmargin, hang, perpage, symbol*]{footmisc}
\usepackage{geometry}
\geometry{letterpaper}
\usepackage{lipsum}

\usepackage{listings}
\lstset{
language=C,
abovecaptionskip=\baselineskip,
aboveskip=\baselineskip,
belowcaptionskip=0pt,
belowskip=\baselineskip,
basicstyle=\small\ttfamily,
breakatwhitespace=true,
breaklines=true,
captionpos=b,
floatplacement=hbt!,
keepspaces=true,
keywordstyle=\lst@ifdisplaystyle\bfseries\fi\normalsize,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2,
xleftmargin=.4in
}

\begin{document}
\mainmatter
\chapter{Chapter}

\lipsum[1]

\begin{lstlisting}
#include <stdio.h>

int main() {
printf( "This listing has the correct amount of space above/below it.\n" );
}
\end{lstlisting}

\lipsum[2]

\begin{lstlisting}[float]
#include <stdio.h>

int main() {
printf( "This listing with float has too much space above/below it.\n" );
}
\end{lstlisting}

\lipsum[3]
\end{document}

Paul Lucas

unread,
Nov 4, 2024, 8:57:39 PM11/4/24
to LaTeX Users Group
I posted my question in several places. Nobody answered. I eventually figured out _a_ solution... no idea whether it's the optimal solution:


— Paul

Peter Flynn

unread,
Nov 5, 2024, 1:48:45 AM11/5/24
to latexus...@googlegroups.com

I haven't had a chance to try but I'm baffled by the idea ATM.


--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to latexusersgro...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/latexusersgroup/f674125e-5aca-4b26-b395-d651d971736en%40googlegroups.com.

Peter Flynn

unread,
Nov 5, 2024, 4:16:07 AM11/5/24
to latexus...@googlegroups.com
I just tried this out so I now see the problem, sorry. I was confused by your use of the word "inline" which doesn't mean what I think you wanted ("inline" means "within the flow of the surrounding text", eg inside a paragraph). Both your examples are block-level; one just happens to be a float and the other one not.

I'm not sure that it's a good thing to have floats which don't have a caption or number (or at least some kind of handle or label); otherwise when they do float away from their point of reference, how are you going to point readers at them?

I'm also not sure that the authors of the listings package envisioned some block-level listings being used as floats and some as non-floats within the same document, so this may be the reason for the extra spacing. 

Your solution seems to be pretty much the best way. You might let the authors know the problem and your solution in case they'd like to incorporate it into a future version.

Peter

Paul J. Lucas

unread,
Nov 5, 2024, 8:28:17 AM11/5/24
to latexus...@googlegroups.com
On Nov 5, 2024, at 1:15 AM, Peter Flynn <angleb...@gmail.com> wrote:

> I just tried this out so I now see the problem, sorry. I was confused by your use of the word "inline" which doesn't mean what I think you wanted ("inline" means "within the flow of the surrounding text", eg inside a paragraph). Both your examples are block-level; one just happens to be a float and the other one not.

Yeah, sorry. I guess I meant “here."

> I'm not sure that it's a good thing to have floats which don't have a caption or number (or at least some kind of handle or label); otherwise when they do float away from their point of reference, how are you going to point readers at them?

What I gave was a minimum working example — and those things aren’t necessary for one. My real document has those things.

> I'm also not sure that the authors of the listings package envisioned some block-level listings being used as floats and some as non-floats within the same document, so this may be the reason for the extra spacing.

Well, that seems kind of odd — like they’ve never written any documents that explain programming. For actual full listings, I use float. For _very_ small excerpts to explain listings, like 1 or 2 lines, I don’t use float. For example, the following would use float (and a caption):

#include <stdio.h>

int main()
printf( “hello, world!\n” );
}

Listing 1.1: A first program

In subsequent explanatory text when I want to call out a specific line from a listing, I don’t use float:

In the previous program, the line:

#include <stdio.h>

includes the text from the standard library file stdio.h into your program.

For 1-liners like that, it seems both silly and overkill to make them float and give them a listing number. Also for 1-liners, it doesn’t matter if it gets pushed to the top of the next page.

In both the float and non-float cases, I want the space before/after listings (whether full listings or 1-linters) to be the same.

To me, such use seems obvious.

> Your solution seems to be pretty much the best way.

Thanks.

> You might let the authors know the problem and your solution in case they'd like to incorporate it into a future version.

OK.

- Paul


Reply all
Reply to author
Forward
0 new messages