# Change header if title is first on page

(Not a dupe of Is something the first item on a page? and another similar question, because these involve preventing rules at the top of a page by putting them in leaders; but leaders require boxes, and I don't want a box, but simply to issue a header command.)

So I often roll my own sectioning commands, and I'm currently typesetting a volume which has frequent, shorter chapters. As a result, I don't want to start a new page for every chapter, so when I wrote my sectioning command, it simply skips, prints the title, sends the title to the TOC, then skips again to be ready for the text. Normally, these chapters do not begin at the start of a page; so in that case, having the full, normal header is the correct behavior.

Occasionally, however, the chapters do begin at the top of a page, and in those circumstances I'd like to have the equivalent of \thispagestyle{plain} happen. I can't simply put that in the chapter definition, because most of the time the chapters don't start at the top of the page. I only want a plain pagestyle when the chapter falls at the top of the page. MWE:

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\def\newchap#1{%
\vskip 3em plus1em minus0.5em%
\hbox to\linewidth{\LARGE\hfil#1\hfil}%
\vskip 3em plus1em minus0.5em%
}%
\begin{document}
\newchap{New Chapter Here}
\end{document}


Notice that, although the chapter is the first thing on the page, we end up with the full header. (As the code directs.) I'd like to know something I can put in \newchap which, when it's at the top of the page, will issue \thispagestyle{plain}, or something with equivalent functionality.

The following is not thoroughly tested, but should suffice with some tweaking.

The idea is to update \newchap to include some code from needspace. This allows you to force a new page based on the amount of space available on the page, and condition accordingly between issuing \thispagestyle{plain} and \thispagestyle{fancy}:

\documentclass{article}
\usepackage{lipsum}% Just for this example
\usepackage{fancyhdr,afterpage}

\newif\iftopchap

% http://tex.stackexchange.com/a/32622/5764
\makeatletter
% \needspace{<len>}{<NOT enough space>}{<enough space>}
\newcommand{\needspace}[3]{\par \penalty-100\begingroup
\setlength{\dimen@}{#1}%
\ifdim \dimen@>\dimen@ii% execute the following if there IS NOT enough space on page
\ifdim \dimen@ii>\z@
\vfil
\fi
\break
#2% <NOT enough space>
\else% execute the following if there IS enough space on page
#3% <enough space>
\fi\endgroup}
\makeatother

\pagestyle{fancy}

\newcommand{\newchap}[1]{%
\par
\needspace{6em}{% <not enough space>
\global\topchaptrue
\thispagestyle{plain}%
\afterpage{\global\topchapfalse}%
}{% <enough space>
\iftopchap\else\thispagestyle{fancy}\fi
}%
\vspace{3em plus 1em minus 0.5em}
\noindent\makebox[\linewidth]{\LARGE #1}%
\par\nobreak\vspace{3em plus 1em minus 0.5em}
}%

\begin{document}
\sloppy% Just for this example

\thispagestyle{plain}
\newchap{New chapter}
\lipsum[1]

\newchap{Another chapter}
\lipsum[2-3]

\newchap{Yet another chapter}
\lipsum[3-7]

\newchap{Penultimate chapter}
\lipsum[4-5]

\newchap{Final chapter}
\lipsum[5-7]

\end{document}


The choice for a required space of 6em for the heading is based on an estimate of the header size (vertical space included). This may require some adjustment from your end to ensure proper page-breaking.