# listing in minipage sucks content of previous page to the bottom

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.

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

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

(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}
%#############################################################################


• 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 – David Carlisle Feb 19 '15 at 12:31
• the image shows the difference between \flushbottom and \raggedbottom but your example uses \raggedbottom in all cases. – David Carlisle Feb 19 '15 at 12:56
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.
• 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 '15 at 13:32