4

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}
5
  • 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}%.....
    – touhami
    Sep 21 '15 at 14:14
2

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

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

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[includeheadfoot,top=0cm,headheight=16pt,headsep=3cm]{geometry}
\usepackage{xcolor}

\usepackage[automark,headsepline]{scrlayer-scrpage}
\clearpairofpagestyles
\addtokomafont{pageheadfoot}{\upshape\color{black}}

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

\ohead{\colorbox{yellow}{\leftmark}}

% Declare new layers and a new page style for the first page of a chapter
\DeclareNewPageStyleByLayers{chapterfirstpage}{%
  chapternumber,chapternumberline,%
  plain.scrheadings.foot.odd,plain.scrheadings.foot.oneside%
}
\DeclareNewLayer[
   background,
   head,
   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[
  clone=plain.scrheadings.head.below.line,
  voffset=107pt,
  addvoffset=-\ht\strutbox,
  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}

enter image description here

Or if there are also unnumbered chapters:

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

\usepackage[automark,headsepline]{scrlayer-scrpage}
\clearpairofpagestyles
\addtokomafont{pageheadfoot}{\upshape\color{black}}

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

\ohead{\colorbox{yellow}{\leftmark}}

% Declare new layers and a new page style for the first page of a chapter
\DeclareNewPageStyleByLayers{chapterfirstpage}{%
  chapternumber,chapternumberline,%
  plain.scrheadings.foot.odd,plain.scrheadings.foot.oneside%
}
\DeclareNewLayer[
   background,
   head,
   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[
  clone=plain.scrheadings.head.below.line,
  voffset=107pt,
  addvoffset=-\ht\strutbox,
  contents=\rule{\textwidth}{.4pt}
]{chapternumberline}

\newcommand\chapternumberon{%
  \IfLayerAtPageStyle{chapterfirstpage}{chapternumber}{}{%
    \AddLayersToPageStyle{chapterfirstpage}{chapternumber,chapternumberline}}%
}
\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}

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.