I am trying to automize setting the correct \headheight
following Heiko Oberdiek's approach. It is based on the fact that the fancyhdr
package usually increases the \headheight
as required and saves this value at the very end to a file. The problem is that when using the calc
package that value does not seem to change (although the log file still claims "We now make it that large for the rest of the document.").
I know that I could avoid the calc
package by using \dimexpr
and friends but I need the mathtools
package which requires the calc
package.
Does anyone have an idea how to get the correct value for \headheight
even if the calc
package is used?
\documentclass{article}
\usepackage{blindtext}
\usepackage{calc} % this suppresses fancyhdr's feature of increasing \headheight as required
% ===== save header and footer height =====
% https://tex.stackexchange.com/a/117810/120953
\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
\showthe\headheight
\fi
}
\makeatother
%\InputIfFileExists{\jobname.heights}{}{} % I have commented this out for testing
\usepackage[
includehead,
includefoot,
showframe,
]{geometry}
% ===== header & footer =====
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{graphicx}
\pagestyle{fancy}
\fancyhead{}
\fancyhead[L]{a rather long header\\ which needs to be broken into several lines}
%\fancyhead[R]{\includegraphics[height=\dimexpr\the\headheight-4.3501pt\relax]{icon}}
\fancyfoot{}
\fancyfoot[R]{page \thepage \ of \pageref{LastPage}}
\fancyfoot[L]{some footer}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
% ===== test document =====
\begin{document}
\blinddocument
\end{document}
fancyhdr
doesn't usually increase headheight, it just warns, and increases it if the settings are inconsistent with the specified heading. Rather than use this file to save this error-correction it woul dbe more natural just to set up the correct settings using the geometry package in the first place.