How to determine head height automatically
I do not think, it makes much sense to use an overlarge height of the titlepage's header for the whole document. Nonetheless the question is interesting in general.
Package fancyhdr
already updates \headheight
or \footskip
, if the header or footer is too small. The following example stores the latest values of these dimension registers at the end of the document in an auxiliary file \jobname.heights
. If the file exists, it is read in the preamble, before package geometry
is loaded that might use the updated values for its calculations. Therefore the .aux
file cannot be used for this purpose, because it is read a little too late (in \begin{document}
).
\documentclass[12pt]{report}
\usepackage{atveryend}
\makeatletter
\AtVeryEndDocument{%
\if@filesw % respect \nofiles
\begingroup
% same write register as environment `filecontents` uses
\chardef\reserved@c=15 %
\immediate\openout\reserved@c=\jobname.heights\relax
\immediate\write\reserved@c{%
\string\setlength{\string\headheight}{\the\headheight}%
}%
\immediate\write\reserved@c{%
\string\setlength{\string\footskip}{\the\footskip}%
}%
\immediate\closeout\reserved@c
\endgroup
\fi
}
\makeatother
\InputIfFileExists{\jobname.heights}{}{}
\usepackage[
includehead,
includefoot,
a6paper,
landscape,
showframe,
]{geometry}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\chead{\Huge Header}
\cfoot{\Huge \begin{tabular}{c}Footer\\\thepage\end{tabular}}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\begin{document}
Hello World
\end{document}
First run
Package fancyhdr
warns:
Package Fancyhdr Warning: \headheight is too small (12.0pt):
Make it at least 30.0pt.
We now make it that large for the rest of the document.
This may cause the page layout to be inconsistent, however.
Package Fancyhdr Warning: \footskip is too small (30.0pt):
Make it at least 50.99991pt.
We now make it that large for the rest of the document.
This may cause the page layout to be inconsistent, however.
And the contents of the page is displaced:
Second run
The previously written \jobname.heights
is used:
\setlength{\headheight}{30.0pt}
\setlength{\footskip}{50.99991pt}
There are no warnings and the page is:
Workaround for package calc
Package fancyhdr
has bugs, e.g. \global\setlength
that does not work, if package calc
is loaded. The following code patches the macro \@fancyvbox
.
\makeatletter
\def\reserved@a#1#2{\setbox0\vbox{#2}\ifdim\ht0>#1\@fancywarning
{\string#1 is too small (\the#1): ^^J Make it at least \the\ht0.^^J
We now make it that large for the rest of the document.^^J
This may cause the page layout to be inconsistent, however\@gobble}%
\dimen0=#1\global\setlength{#1}{\ht0}\ht0=\dimen0\fi
\box0}
\ifx\reserved@a\@fancyvbox
\typeout{Patching \noexpand\@fancyvbox from package fancyhdr.}
\def\@fancyvbox#1#2{%
\setbox0\vbox{#2}%
\ifdim\ht0>#1\relax
\@fancywarning{%
\string#1 is too small (\the#1): ^^J%
Make it at least \the\ht0.^^J%
We now make it that large for the rest of the document.^^J%
This may cause the page layout to be inconsistent, however\@gobble%
}%
\dimen0=#1\relax
\global#1=\ht0\relax
\ht0=\dimen0 %
\fi
\box0 %
}
\fi
\makeatother
The code should be inserted in the preamble after package fancyhdr
is loaded.