longtable sometimes starts new page when we are near the top of the page

Most of the time longtable does exactly what is required, but sometimes it starts a new page when it is near the top of the page and there is plenty of space for the entire table on the current page.

I have isolated the problem in the following complete document:

\documentclass[a4paper] {report}

\usepackage{longtable}
\setlongtables

\parindent0mm
\parskip2ex plus1ex minus1ex

\author{Author}
\date{10 November 2014}

\title{Document}
\begin{document}
\maketitle

\newpage
\chapter{Functions}

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

to show the problem the function needs to start on a new page

\section{matrix\_multiply} \label{function_matrix_multiply}

This function multiplies two matrices together.

\textbf{Inputs:}

\small
\begin{longtable}{|p{150pt}|p{230pt}|l}

\cline{1-2} \textbf{Type and Name} & \textbf{Description} &

\cline{1-2} \textbf{Type and Name} & \textbf{Description} &

\cline{1-2} \multicolumn{2}{r}{{Continued on next page}}
\endfoot

\cline{1-2}
\endlastfoot
\cline{1-2}
\raggedright Matrix A &
\raggedright Matrix A
& \\
\cline{1-2}
\raggedright Matrix B &
\raggedright Matrix B - the number of rows of B must be the same as the number of columns of A
& \\
\cline{1-2}

\end{longtable}
\normalsize

\textbf{ReturnType:} Matrix

\end{document}


What I am seeing is that the section goes onto a new page (of its own accord - there is no \newpage command before the section), then the longtable appears on a new page even though it has about 80% of the page free and is only a few lines.

If you comment out the last of the dummy text before the section command, then the problem goes away. However my TeX files are generated from source code and editing the TeX file manually is not a good option for me.

Any help and insights would be greatly appreciated.

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. I would like to say that you did really well for your first question! :) Just a small thing: you can markup inline code with backticks  – yo' Nov 10 '14 at 15:39
• Using a nonzero parskip creates several white strips on the page; if it is stretchable shrinkable, those white strips may become arbitrarily wide. Avoid setting \parskip or, if you insist, avoid letting it stretch (and shrink). – egreg Nov 10 '14 at 19:11

The problem goes if you lose this line

  %\parskip2ex plus1ex minus1ex


or even just change it to

\parskip2ex plus1ex %minus1ex


Putting lots (or any) of shrink glue into the vertical list rather complicates the assessment of whether there is room...

longtable could try harder to estimate the amount of glue that will really stretch/shrink but currently, it doesn't.

If you wish to keep the shrink in the \parskip value, you can force the page break decision to occur before it confuses longtable by putting a \clearpage at the section title.

\clearpage
\section{matrix\_multiply} \label{function_matrix_multiply}
`