# allow minipage to column break but not pagebreak

I have a twocolumn document with many sections. I want to allow sections to break across columns of the same page, but not allow sections to break across pages.

The most closely related previously asked question I can find is this. But it won't solve the problem I have.

In the MWE I have a long section that needs to break across a column in order to fit onto a page. The minipage environment doesn't allow this, and starts the long section in a new column but overruns the page. See here for piccy.

I also want to prevent the section title being separated from the section text.

\documentclass[12pt,oneside,twocolumn]{article}
\columnsep=2.5cm
\raggedbottom
\newcommand{\textbox}[1]{
\framebox{\begin{minipage}{\columnwidth}\mbox{}\vspace{#1}\end{minipage}}}

\begin{document}

\section{small-length section}
\textbox{1in}

\section{epic-length section}
\textbox{12in}

\section{mid-length section}
\textbox{3in}

\section{big-length section}
\textbox{6in}

\end{document}


If the end of the SamePage environment was on a different page, a \newpage will be performed before the start.

There are two problems with this solution. First, it may take more than 2 runs for the document to stabilize. Second, if a subsequent edit frees up enough space for the environment to fit on the previous page, the only way to test it is to delete the aux file (clean project). Alternatively, using draft mode will disable the page test.

\documentclass[twocolumn]{article}
\usepackage{environ}
\usepackage{lipsum}

\newcounter{SamePage}

\newcommand{\newSamePage}[2]% #1 = index, #2 = page
{\expandafter\gdef\csname SamePage#1\endcsname{#2}}

\makeatletter
\newenvironment{SamePage}%
{\stepcounter{SamePage}%
\ifdim\overfullrule=0pt\relax% check for final mode
\@ifundefined{SamePage\theSamePage}{}%
{\ifnum\csname SamePage\theSamePage\endcsname>\c@page\newpage\fi}%
\fi\ignorespaces}%
{\protected@write\@auxout{}{\string\newSamePage{\theSamePage}{\arabic{page}}}%
\par}
\makeatother

\begin{document}
\begin{SamePage}
\section{First}
\lipsum[1-4]
\end{SamePage}
\begin{SamePage}
\section{Second}
\lipsum[5-8]
\end{SamePage}
\begin{SamePage}
\section{Third}
\lipsum[9]
\end{SamePage}

\end{document}

• If I put the "Third" section before the "Second", the "Third" section should be on the first page, in the second column, because there is room enough. Is this possible? Currently your solution begins a new page unnecessarily. – Tim Dec 30 '15 at 11:21
• That would fall under "subsequent edit" category. Delete the aux file or use \documentclass[draft]{,,,} to re-test if it will fit on an earlier page. – John Kormylo Dec 30 '15 at 14:37
• of course. my mistake. Yep, it works great. – Tim Dec 31 '15 at 9:22