# How to check if a section title (or any heading) is the first line of a new page?

I would like to be able to use an \if command to check if a \section appears on the first line of a new page. If a section heading is the first line of a new page, I would like to then adjust some of the spacing between the section heading and the header by modifying \headsep.

Is there a simple way to do this?

More info (edit): The issue arises whenever you don't forcibly add new pages to separate sections. If you were to write continuously, then, whenever TeX decides to insert a page break, the spacing arguments native to \section are suppressed iff the page break occurs right before a new section.

Hence, if, for instance, you have long, ruled headers, the lack of spacing looks odd:

As far as I know, TeX doesn't have a built-in counter for line numbering that can be used to check if a heading appears on the first line of a new page. You don't know when a page break is likely to occur, either. Thus, although I have a specific purpose for finding a way to check if something is on the first (or nth line) of a page, it might still help others with different purposes.

-
It seems I solved this myself... My solution requires the lineno, fancyhdr, and color packages, so it is not an elegant solution by any means. First initiate lineno to insert line numbers to your document; lineno adds a counter called linenumber which you can reset for every new page by adding \@addtoreset{linenumber}{page}. –  John Jan 16 '12 at 20:16
... Then.... Define a new fancypagestyle{y} that adds the x shift \setlength{\headsep}{x} Then, when you define your \def\@makesectionhead#1 macro, add: \ifnum\value{linenumber}=1\thispagestyle{y}\else\fi. Lastly, \renewcommand\linenumberfont{\color{white}} to hide line#s. :) –  John Jan 16 '12 at 20:27
But changing \headsep will shift the whole text block down. –  egreg Jan 16 '12 at 21:15
@egreg. That's true. You could counteract it by reducing \textheight by an equal amount within the new pagestyle. Keeping it in a fancypagestyle and using \thispagestyle should ensure that both changes are only applied to the single page where the condition of the first line being a section heading is met. –  John Jan 17 '12 at 7:46

This is a rather horrible hack:

\makeatletter
\let\john@@section\section
\def\section{\goodbreak\@nobreaktrue
\vspace*{3.5ex \@plus 1ex \@minus .2ex}\john@@section}
\makeatother


The argument of \vspace has been copied from the class definition of \section.

(It's quite hard to understand the need for this.)

-

This solution requires the lineno, fancyhdr, and color packages, so it is not elegant.

First, Initiate lineno to insert line numbers to your document (use \linenumbers after begin{document}); lineno adds a counter called linenumber which you can force-reset for each new page by adding \@addtoreset{linenumber}{page} to your .cls (class/style) file.

Define a new fancypagestyle{y} that adds the required additional space (x) between header and the first line using \setlength{\headsep}{x}.

Then, when you define your \def\@makesectionhead#1{} macro that creates or prints the section number and name, add \ifnum\value{linenumber}=1\thispagestyle{y}\else\fi as the last line before closing with }.

Lastly, use \renewcommand\linenumberfont{\color{white}} to hide line numbers by changing them to white.

-
As egreg noted: changing \headsep will shift the whole textblock. To counteract this, simple add a second \setlength command in the newly defined pagestyle that reverses the change in \textheight. So, if you increase \headsep by 1em, reduce \textheight by 1em. –  John Jan 17 '12 at 7:48