I'm trying to generate a document that has two different possible headers, one for the first page of the document and the first page of sections, and one for all other pages. All pages should have footers that give the page number.
I'm using fancyhdr
and have defined page styles associated with each header, and the problem I'm running into is how to correct for the fact that the header for first pages is larger than that for the others. When I switch from one to the other, I have to force the geometry to be recalculated, most notably so that the footer doesn't get pushed off the page when I increase the headheight. I've put the recalculation of the geometry in the page style definition so that they're called together, and if I call \pagestyle{either style}
, I get the expected results.
The part where I'm lost is that I'd like to be able to use \thispagestyle
for the first pages of each section, and when I try to do so I get the appropriate heading but not the appropriate geometry.
Here's a MWE that shows the problem:
\documentclass[a5paper,12pt]{article}
\usepackage[showframe]{geometry}
\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{2pt}
\setlength{\parindent}{0cm}
\fancypagestyle{firstpages}{%
\newgeometry{includeheadfoot, left=1cm, right=1cm, top=.5cm, bottom = 1cm, headheight=57pt}
\rhead{Long header\\line2\\line3\\line4}
\cfoot{\thepage}
}
\fancypagestyle{others}{%
\newgeometry{includeheadfoot, left=1cm, right=1cm, top=.5cm, bottom=1cm, headheight=15pt}
\lhead{}
\chead{}
\rhead{Normal header}
\cfoot{\thepage}
}
\begin{document}
\pagestyle{firstpages}
Text under big header
\pagebreak
\pagestyle{others}
Text under little header
\pagebreak
\thispagestyle{firstpages}
Text under big header
\pagebreak
Text under little header
\pagebreak
\pagestyle{firstpages}
Text under big header
\end{document}
The pages where the style is defined by \pagestyle
(1, 2, 4, 5) are fine, but on page 3, where \thispagestyle
is used, the boxes associated with the geometry don't shift and (I think as a consequence) the footer is pushed off the page.
I haven't found a description of differences between \pagestyle
and \thispagestyle
that I'd expect to be causing this behaviour, but clearly something is. Is there a way of making this work? (Alternatively, I'd be happy to hear of a different way to make just the first page of an otherwise-undemarcated section differ from the rest of the section without a known page-break location.) I'm not a very experienced Latex user, so let me know if I'm overlooking anything obvious.
\newgeometry
as part of a pagestyle. Anyway,fancyhdr
expects the head height to be the same across the document.\thispagestyle
is only for the current page, afterwards the old page style before is used, i.e.\thispagestyle
'eclipses' the outer page style for a 'moment', applies its own settings, the page is shipped out, the old pagestyle is reactivated.