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"?

\documentclass[a4paper]{book}
\usepackage{color}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}

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

\textcolor{black}{\rlap{%
\colorbox{yellow}{\leftmark}}}}

\fancypagestyle{plain}{%
\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}

• You are aware that fancyhdr complains about the head height? You gave 16pt, but it requests 122pt
– user31729
Sep 21 '15 at 12:27
• Normally, no header is included on plain pages. You would create the 'header' for the chapter as part of the \chapter formatting rather than as part of a header. I think this is the most straightforward solution.
– cfr
Sep 21 '15 at 12:27
• @ChristianHupfer I thought that was the point of the question ;).
– cfr
Sep 21 '15 at 12:28
• Using \raisebox fixes everything except the line drawn below the header, which stays in the same place regardless. Sep 21 '15 at 13:00
• a trick \fancyfoot[L]{% \global\setlength{\headheight}{16pt}%..... Sep 21 '15 at 14:14

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{fancyhdr}
\usepackage{color}
\usepackage{tikz}
\usetikzlibrary{calc}

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

\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}


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{fancyhdr}
\usepackage{color}
\usepackage{everypage}

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

\textcolor{black}{\rlap{%
\colorbox{yellow}{\leftmark}}}}

\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}

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


• +1 for offering two methods. Tikz worked for me, however, only without the calculations in path and draw (in path, +(16cm,0pt) will do). Sep 21 '15 at 19:18

Here is an additional suggestion using package scrlayer-scrpage instead fancyhdr. Then you can define a new page style for the chapter pages.

\documentclass[a4paper]{book}
\usepackage{xcolor}

\clearpairofpagestyles

\lefoot*{\smash{\llap{\colorbox{yellow}{\makebox[\textwidth][r]{\pagemark}}}}}
\rofoot*{\smash{\rlap{\colorbox{yellow}{\makebox[\textwidth][l]{\pagemark}}}}}

% Declare new layers and a new page style for the first page of a chapter
\DeclareNewPageStyleByLayers{chapterfirstpage}{%
chapternumber,chapternumberline,%
}
\DeclareNewLayer[
background,
align=t,
voffset=0pt,
height=93pt,
contents={%
\parbox[b][\layerheight]{\layerwidth}{%
\setlength{\fboxsep}{14pt}%
\hfill\rlap{\colorbox{yellow}{%
\makebox(23,93)[cb]{\Huge\textbf{\thechapter.}}}}%
\hspace*{9\marginparsep}}}
]{chapternumber}
\DeclareNewLayer[
voffset=107pt,
contents=\rule{\textwidth}{.4pt}
]{chapternumberline}

% use page style chapterfirstpage instead plain on the first page of a chapter
\usepackage{etoolbox}
\patchcmd{\chapter}{plain}{chapterfirstpage}{}{}

\usepackage{blindtext}% dummy text

\begin{document}
\chapter{foo}
\Blindtext
\section{bar}
\blindtext
\chapter{Foo}
\Blindtext[3]
\blinddocument
\end{document}


Or if there are also unnumbered chapters:

\documentclass[a4paper]{book}
\usepackage{xcolor}

\clearpairofpagestyles

\lefoot*{\smash{\llap{\colorbox{yellow}{\makebox[\textwidth][r]{\pagemark}}}}}
\rofoot*{\smash{\rlap{\colorbox{yellow}{\makebox[\textwidth][l]{\pagemark}}}}}

% Declare new layers and a new page style for the first page of a chapter
\DeclareNewPageStyleByLayers{chapterfirstpage}{%
chapternumber,chapternumberline,%
}
\DeclareNewLayer[
background,
align=t,
voffset=0pt,
height=93pt,
contents={%
\parbox[b][\layerheight]{\layerwidth}{%
\setlength{\fboxsep}{14pt}%
\hfill\rlap{\colorbox{yellow}{%
\makebox(23,93)[cb]{\Huge\textbf{\thechapter.}}}}%
\hspace*{9\marginparsep}}}
]{chapternumber}
\DeclareNewLayer[
voffset=107pt,
contents=\rule{\textwidth}{.4pt}
]{chapternumberline}

\newcommand\chapternumberon{%
\IfLayerAtPageStyle{chapterfirstpage}{chapternumber}{}{%
}
\newcommand\chapternumberoff{%
\RemoveLayersFromPageStyle{chapterfirstpage}{chapternumber,chapternumberline}}

% use page style chapterfirstpage instead plain on the first page of a chapter
\usepackage{etoolbox}
\patchcmd{\chapter}{plain}{chapterfirstpage}{}{}

\usepackage{blindtext}% dummy text

\begin{document}
\chapternumberoff
\tableofcontents
\cleardoublepage
\chapternumberon
\chapter{foo}
\Blindtext
\section{bar}
\blindtext
\chapter{Foo}
\Blindtext[3]
\blinddocument
\end{document}