# multicol: Forcing column breaks depending on remaining column space

I'm sure I'm not the first one to run into this problem, but I couldn't find any other question that quite answered this in a way that worked for me.

I would like to insert a section heading in the same column if there's sufficient space and kick it to the next column if there is not; I've succeeded in this by setting penalties in the appropriate places. However, when a column break is necessary, I'd like to fill the previous column, so that \parskip doesn't stretch crazily.

Turning of \parskip for the whole document is not an option, or the columns do not balance.

MWE:

\documentclass[letterpaper]{article}
\usepackage{multicol}

\makeatletter
\def\articulus{%
\vskip 3em plus 0.75em minus 0.25em%
\centerline{\Large\textsc{Articulus }}%
\penalty10000\vskip 3em plus 0.75em minus 0.25em%
}%
\makeatother

\def\text{Now is the time for all good men to come to the
aid of their party.}

\begin{document}
\begin{multicols}{2}
\text\text\text\text\text\text\text\text\text\text

\text\text\text\text\text\text\text\text\text
\text\text\text
\articulus{}
\text\text\text\text\text\text\text\text\text
\text\text\text\text\text\text\text\text\text
\text\text\text\text\text\text\text\text\text
\text\text\text\text\text\text\text\text\text
\text\text\text\text\text\text\text\text\text
\end{multicols}
\end{document}


I would like to vertically fill the remainder of the column if a pagebreak has to occur here. Is this possible? If not, is there a better way to arrange this?

You are probably looking for something like this, right?

If so, here is the code:

\documentclass[letterpaper]{article}
\usepackage{multicol}

\makeatletter
\def\articulus{%
\vfil \vskip-\prevdepth \nointerlineskip\null
\penalty 200  \vfilneg
\vskip 3em plus 0.75em minus 0.25em%
\centerline{\Large\textsc{Articulus }}%
\penalty10000\vskip 3em plus 0.75em minus 0.25em%
}%
\makeatother

\def\text{Now is the time for all good men to come to the
aid of their party. }

\begin{document}
\begin{multicols}{2}
\text\text\text\text\text\text\text\text\text\text

\articulus{}

\text\text\text\text\text\text\text\text\text
\text\text

\articulus{}
\text\text\text\text\text\text\text\text\text
\text\text\text\text\text\text\text\text\text

\text\text\text\text\text\text\text\text\text

\text\text\text\text\text\text\text\text\text
\articulus{}
\text\text\text\text\text

\text\text\text\text
\end{multicols}
\end{document}


Explanation: this code here is what makes it happen:

   \vfil
\vskip-\prevdepth \nointerlineskip\null
\penalty 200  \vfilneg


The combination of \vfil \penalty <number> \vfilneg is a trick already explained in the TeXbook. It means if there is a break at the penalty fill up the space above, otherwise cancel the space out. In our case use a positive penalty because, otherwise this may get taken as the best place to end multicols collection phase for all columns because the \vfil can stretch arbitrarily (that got me puzzled a bit initially). In fact it might be better for this reason to use a finite stretch and shrink here instead.

The interesting extra is the line in the middle. The recent releases of multicol correctly drop spaces at the bottom of a column (for example space after a list etc) so if the break is taken at the penalty, the \vfil would get removed by the column generation. To avoid that I added \null which is short for \hbox{} but that normally adds a baselineskip which is not what we want, thus \nointerlineskip. But that means we lost our \prefdepth so additionally we first back up so that we are actually at the baseline of the previous line.

In fact that is really where we want to be because if we are on the final page where balancing is happening and a break is taken at the penalty, the line before is most likely the last line of the column without a need to stretch and then (because of a \vfil that amounts to zero the columns would not align be off by that \prevdepth. You can see that nicely on the second page of the example which now comes out like this:

i.e., very well-behaved.

• Should have known it had something to do with \prevdepth. My real articulus definition is much more complex, of course, but this does the trick. I've already accepted your answer. Thanks. – dgoodmaniii Nov 4 '14 at 20:57