# Issue an error when section takes more than 2 pages

I have a need to make the build crash when a \section takes more than a single page (in a given environment). How can I achieve that?

-
Is "a section" now some range in an environment or a \section? –  Martin Scharrer Apr 18 '11 at 7:57
The 'sections' tag you added is the plural version of 'section' which is already an synonym of 'sectioning'. According with the tagging rules applied to this site I made it as well a synonym of 'sectioning'. That tag is for all sectioning macros like \chapter, \section, \subsection, etc. Feel free to remove it if its doesn't fit for your question. Sorry for the hassle. –  Martin Scharrer Apr 18 '11 at 8:06
Thanks for the comments. I'm talking about \section indeed. –  ℝaphink Apr 18 '11 at 8:08

You could add a label after the \section and after the end of it and then compare the pagenumbers e.g. using the macro \vrefpagenum of the varioref package. IMHO this is saver then using \pageref directly because it could include other material like hyperlinks, but I might be wrong.

\documentclass{article}

\usepackage{varioref}

\makeatletter

\newcommand*{\checksection}[2]{%
\begingroup
\vrefpagenum\@tempa{#1}%
\vrefpagenum\@tempb{#2}%
\ifx\@tempa\@tempb\else
\GenericError{}{Section longer than one page!}{}{}%
\fi
\endgroup
}
\makeatother

\begin{document}
....

\section{Some section}\label{sec:some}
...

\label{sec:some-end}
\checksection{sec:some}{sec:some-end}

\section{Other section}\label{sec:other}
% Alternative:
\checksection{sec:some}{sec:other}

\end{document}


You could also make an environment which places this labels at the \begin and \end and runs the check automatically. I might need to use \refstepcounter{dummy} (where dummy is some dummy counter) to make the \label produce the correct page number.

A problem with this solutions is that there might be error caused because of an outdated .aux file.

-
Interesting solution. Ideally though, I'd like to make it so that I don't have to add a tab at the end of each section. Also, wouldn't it be easier to just set a counter to \thepage at the beginning of the section and compare with \thepage at the end of the section? –  ℝaphink Apr 18 '11 at 8:36
@Raphink: No, the \thepage macro is not reliable (except in footers and headers), because when it is processed the page is not yet fully build and TeX doesn't know if the material it is used in is part of the current page or not. –  Martin Scharrer Apr 18 '11 at 8:49
@Martin: even if I build several times (I'm used to building 3 times to make sure references are fine after everything is positioned)? I'm using \thepage in a \titleformat{\section} command to display tabs on the side of the page, and I've had no problems with it. Is that a mistake? –  ℝaphink Apr 18 '11 at 8:53
@Raphink: Yes, \thepage simply adds the number of the page counter which is unaffected by reruns. Using \thepage might get you the correct number but it is not guaranteed. –  Martin Scharrer Apr 18 '11 at 8:58
@Martin: I plan on adding this check system to \titleformat because I need to check all sections. Using labels, I will end up with labels titled the same in all sections... –  ℝaphink Apr 18 '11 at 9:06