3

I'm working on a document that contains many listings (namely code samples) but not a lot of text. Most of these listings are only a couple of lines long, and a few span multiple pages. At the end of a page, a break is inserted in the middle of a listing.

screenshot #1
(source: warmonkey.org)

I don't want short listings to be broken across pages. Rather, short listings should just be moved to the next page, leaving some white space at the end of the previous page. The ideal solution would be if I could define a threshold for listing size beyond which listings are broken across pages, but below which the listings are automatically moved to the beginning of the next page.

screenshot #3
(source: warmonkey.org)

I haven't found anything like this on the web. At least I've found out how to prevent single listings from being broken across pages, namely by putting them into a minipage. By putting small listings into minipages but leaving long listings as they are (by hand), I almost have what I want.

The problem I have, however, is that when a listing is moved to the next page, the listings (and the text) on the previous page is "sucked" to the bottom of the page! This results in whitespace between the title and the beginning of the content, instead of between the end of the content and the end of the page, which doesn't make a whole lot of sense to me. I'm sure that there's some way to prevent this, I just have no clue how...

screenshot #2
(source: warmonkey.org)

Below is a sample code (note that it's only that long because I didn't find any way to fill the listings with random text, so I had to insert lots of placeholder text manually). The minipage commands are commented, the code corresponds to screenshot #1. If the minipage commands are uncommented, the result looks like screenshot #2. What I want it to look like is shown in screenshot #3 (achieved by uncommenting the pagebreak).

EDIT: Added twoside to documentclass (the initial sample code could not reproduce the comment because I removed twoside right before posting, now knowing that that actually caused the problem).

%#############################################################################
\documentclass[11pt,twoside,a4paper]{article}
%#############################################################################

\usepackage{lipsum} % dummy text

\usepackage{fixltx2e} % textsubscript and textsuperscript
\usepackage{listings}

\lstset{frame=single}

%#############################################################################
\begin{document}
%#############################################################################

%""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
\section{A few listings}
%------------------------------------------------------------------------------

\lipsum[2]

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

%###\noindent\begin{minipage}{\hsize}
\begin{lstlisting}[caption=foo]
not
such
a
huge
listing
\end{lstlisting}
%###\end{minipage}

%###\noindent\begin{minipage}{\hsize}
\begin{lstlisting}[caption=foo]
yet
another
short
one
,
though
a
bit
longer
\end{lstlisting}
%###\end{minipage}

%###\newpage

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

%###\noindent\begin{minipage}{\hsize}
\begin{lstlisting}[caption=foo]
this
one
is
a
bit
larger
,
so
it
doesn
not
fit
onto
the
page
anymore
!
but
it
still
fits
on
one
page
!
\end{lstlisting}
%###\end{minipage}

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

\begin{lstlisting}[firstnumber=494,label=code:assoc494,caption=foo]
this
particular
listing
on
the
other
hand
is
a
lot
longer
than
the
one
before
!





















...



























finally
it
is
coming
to
an
end
!!
\end{lstlisting}

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

%#############################################################################
\end{document}
%#############################################################################

Thanks in advance!

9
  • 1
    Please always post the code in a form that demonstrates the problem. If I uncomment the begin and end minipage lines, the first page is left short with all the space at the bottom as I would expect. the stretched version is what I would expect from \flushbottom but that is not the default with article Feb 19, 2015 at 12:31
  • 1
    You could use the needspace package for your "threshold". Feb 19, 2015 at 12:47
  • @DavidCarlisle Indeed, I'm suddenly not able anymore to reproduce the problem myself, neither in my sample code nor in the original document I'm working on! Now this is just bizarre, after all I wouldn't have gone through the trouble of preparing this question (let alone produce the screenshots) if the problem wouldn't have been there (and reproducible) before...
    – flotzilla
    Feb 19, 2015 at 12:51
  • the image shows the difference between \flushbottom and \raggedbottom but your example uses \raggedbottom in all cases. Feb 19, 2015 at 12:56
  • @UlrikeFischer Thanks for the hint, this might really do the trick! But even apart from my specific problem (although it seems to have disappeared in thin air...) the package is great, I wanted to be able to do something like this for a long time (but I've apparently never searched for it thoroughly enough).
    – flotzilla
    Feb 19, 2015 at 13:04

1 Answer 1

3

The images show the difference between \flushbottom and \raggedbottom, adding \raggedbottom to your preamble (which is the default for single side `article) will cause LaTeX to pad any short pages with stretchable space at the bottom of the page.

2
  • Do you have any idea why onesided defaults to \raggedbottom but twosided to \flushbottom? As mentioned in my comment above, I'd have though the only difference between onesided and twosided would be the lateral page margins!
    – flotzilla
    Feb 19, 2015 at 13:32
  • @flotzilla mostly because it's always been that way. Arguably if printing two sided its more likley that you see two sides at once, so different length pages are more distracting. Perhaps. But Leslie Lamport's original code made this distinction without additional comment some time last century and that's just how it is:-) Feb 19, 2015 at 13:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .