# Creating variable pagebreaks

Is it possible to procedurally generate page breaks by measuring text/page space? I am having a problem which you can see here:

There are two problems in this picture. First, the heading should overflow to the next page because it doesn't fit...but more importantly, and more annoyingly, the heading should be forced to the next page because its content does not fit on the same page.

I am procedurally generating my latex from javascript, so it is sort of hard to post code snippets, but I can show you the function/environment I am using...it is the second code block in the accepted answer here

Any ideas or tips for measuring the text?

edit

Here is the code to generate my custom lists, from the link above:

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{enumitem}
\usepackage{indentfirst}
\pagestyle{empty}
%% I use a box for your "title" and save its width in "\mytitlewidth"
\newlength{\mytitlewidth}
\newlength{\mytopsep}
\newsavebox{\mytitlebox}
%% and environment to handle your list
\newenvironment{enumwithinenum}[1]
{\setlength{\mytitlewidth}{\dimexpr0.30\textwidth\relax}%%'
\setlength{\mytopsep}{2ex}%%'
%% save the title in a box so we can access its height later to
%% use to adjust where the next enumerate begins.
\begin{lrbox}{\mytitlebox}%%'
\begin{minipage}[t]{\dimexpr\mytitlewidth\relax}
\raggedright\strut
\textbf{#1}
\par\xdef\tpd{\the\prevdepth}%%'
\end{minipage}%%'
\end{lrbox}%%"
\usebox{\mytitlebox}%%'
\vspace{-\dimexpr\dp\mytitlebox+\parskip+0.7\baselineskip+\mytopsep\relax}%%'
\begin{enumerate}[align=left,
label={.\arabic*},
leftmargin=\dimexpr\mytitlewidth+2em\relax,
topsep=\mytopsep,
]
}
{\end{enumerate}}
%-@-(1)---------------------------------------------------------------------
\begin{document}

\noindent
\textbf{Part One - General}

zzz

\vspace{12.85cm}

\begin{enumerate}[leftmargin=*,topsep=1ex,itemsep=4ex,label={1.\arabic*}]
\item  \begin{enumwithinenum}{SECTION INCLUDES}
\item \strut Work covered by Contract documents
\item Contract Method
\item Contract Method
\item Contract Method
\item Contract Method
\item Contract Method
\item Contract Method
\item Contract Method
\item Contract Meth
\end{enumwithinenum}
\item  \begin{enumwithinenum}{THIS IS A LONGER TITLE}
\item \strut Work covered by Contract documents
\item Contract Method
\end{enumwithinenum}
\end{enumerate}

\end{document}

• Here I would suggest using the needspace package which would conditionally insert a page break if the space left on a page doesn't meet some threshold. You could set the contents in the "right column" and use it as a test. But it's not easy to provide more detail without some tangible code.
– Werner
Aug 23, 2013 at 18:05
• Yeah...it is sort of hard to paste code :( Like I said, the code to actually layout the heading/content is in the link - I will look into the package, thanks! Aug 23, 2013 at 18:12
• Your heading probably ought to be part of the first item not the preding one, in general latex thinks between items of a list is a rather good place to break a page. By using that (and using \textbf rather than a sectioning command for \part you are not using any of latex's code to avoid bad braks after section heads. Aug 24, 2013 at 0:18

I would bind the heading more tightly to the first item:

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{enumitem}
\usepackage{indentfirst}
\pagestyle{empty}
%% I use a box for your "title" and save its width in "\mytitlewidth"
\newlength{\mytitlewidth}
\newlength{\mytopsep}
\newsavebox{\mytitlebox}
%% and environment to handle your list
\newenvironment{enumwithinenum}[1]
{\setlength{\mytitlewidth}{\dimexpr0.30\textwidth\relax}%%'
\setlength{\mytopsep}{2ex}%%'
%% save the title in a box so we can access its height later to
%% use to adjust where the next enumerate begins.
\begin{lrbox}{\mytitlebox}%%'
\begin{minipage}[t]{\dimexpr\mytitlewidth\relax}
\raggedright\strut
\textbf{#1}
\par\xdef\tpd{\the\prevdepth}%%'
\end{minipage}%%'
\end{lrbox}%%"
\begin{enumerate}[align=left,
label={\llap{\smash{\box\mytitlebox}}.\arabic*},
leftmargin=\dimexpr\mytitlewidth+2em\relax,
topsep=\mytopsep,
]
}
{\end{enumerate}}
%-@-(1)---------------------------------------------------------------------
\begin{document}

\noindent
\textbf{Part One - General}

zzz

\vspace{12.85cm}

\begin{enumerate}[leftmargin=*,topsep=1ex,itemsep=4ex,label={1.\arabic*}]
\item  \begin{enumwithinenum}{SECTION INCLUDES}
\item \strut Work covered by Contract documents
\item Contract Method
\item Contract Method
\item Contract Method
\item Contract Method
\item Contract Method
\item Contract Method
\item Contract Method
\item Contract Meth
\end{enumwithinenum}
\item  \begin{enumwithinenum}{THIS IS A LONGER TITLE}
\item \strut Work covered by Contract documents
\item Contract Method
\end{enumwithinenum}
\end{enumerate}

\end{document}

• This seems to fix the problem I posted. I will try to generate huge amounts of data, to see if it breaks anywhere :) I may still have to use needspace - but this is a great step in the correct direction. Thanks!!!! Aug 24, 2013 at 17:36