[Tex/LaTex] fancyhdr resets geometry to adjust header heights

fancyhdrgeometry

I use two pagestyles fancy for the default header and plain for headers on chapter pages. These headers should have completely different heights, however, it looks like latex adjusts both header heights to the maximum and tries to keep header height constant throughout the whole document. Why is that, and how can I disable this "feature"?

enter image description here

\documentclass[a4paper]{book}
\usepackage[includeheadfoot,top=0cm,headheight=16pt,headsep=3cm]{geometry}
\usepackage{color}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}

\fancyfoot[L]{%
  \textcolor{black}{\llap{%
      \colorbox{yellow}{\makebox[\textwidth][r]{\thepage}}}}}

\fancyhead[L]{%
    \textcolor{black}{\rlap{%
        \colorbox{yellow}{\leftmark}}}}

\fancypagestyle{plain}{%
\fancyhead{}%
\fancyhead[R]{%
    \textcolor{black}{\rlap{%
        \hspace{-9\marginparsep}%
        \setlength{\fboxsep}{14pt}
        \colorbox{yellow}{%
            \makebox(23,93)[cb]{\Huge\textbf{\thechapter.}}}%       
      }}}}

\begin{document}
\chapter{foo}
text
\newpage
\section{bar}
text
\end{document}

Best Answer

Since pagestyle plain is used for more than just the first page in a chapter, you are better off overlaying a header directly. Tikz is one option. Everypage is another (\AddThispageHook). You could create a \mychapter macro to simplify the code.

\documentclass[a4paper]{book}
\usepackage[includeheadfoot,top=0cm,headheight=16pt,headsep=3cm]{geometry}
\usepackage{fancyhdr}
\usepackage{color}
\usepackage{tikz}
\usetikzlibrary{calc}

\fancyfoot[L]{%
  \textcolor{black}{\llap{{%
      \colorbox{yellow}{\makebox[\textwidth][r]{\thepage}}}}}}

\fancyhead[L]{%
    \textcolor{black}{\rlap{%
        \colorbox{yellow}{\leftmark}}}}

\begin{document}
\pagestyle{fancy}
\chapter{foo}
\begin{tikzpicture}[remember picture,overlay]
\path (current page.north west) +(1in+\oddsidemargin+\textwidth-9\marginparsep,0pt)
  node[below right,inner sep=14pt,fill=yellow]
    {\makebox(23,93)[cb]{\Huge\textbf{\thechapter.}}};
\draw (current page.north west) ++(1in+\oddsidemargin,-121pt) -- +(\textwidth,0pt);
\end{tikzpicture}

text
\newpage
\section{bar}
text
\end{document}

chapter header

Here is a solution using everypage. The "cursor" is located at (1in,-1in) relative to the upper left corner and must not be moved.

\documentclass[a4paper]{book}
\usepackage[includeheadfoot,top=0cm,headheight=16pt,headsep=3cm]{geometry}
\usepackage{fancyhdr}
\usepackage{color}
\usepackage{everypage}

\fancyfoot[L]{%
  \textcolor{black}{\llap{{%
      \colorbox{yellow}{\makebox[\textwidth][r]{\thepage}}}}}}

\fancyhead[L]{%
    \textcolor{black}{\rlap{%
        \colorbox{yellow}{\leftmark}}}}

\newcommand{\chapterhead}{\fboxsep=14pt\relax% local to header
  \raisebox{\dimexpr 1in-109pt}[0pt][0pt]{%
    \rlap{\hspace{\dimexpr \oddsidemargin+\textwidth-9\marginparsep}%
       \colorbox{yellow}{%
         \makebox(23,93)[cb]{\Huge\textbf{\thechapter.}}}}%       
    \rlap{\hspace{\oddsidemargin}\rule[-16pt]{\textwidth}{0.5pt}}%
}}

\begin{document}
\pagestyle{fancy}
\chapter{foo}
\AddThispageHook{\chapterhead}

text
\newpage
\section{bar}
text
\end{document}

another chapter header