Page LayoutFancyhdr fails to clear headrule on chapter pages

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
petersfreeman
Posts: 19
Joined: Wed Apr 28, 2021 9:40 am

Fancyhdr fails to clear headrule on chapter pages

Post by petersfreeman »

Using fancyhdr, it clears the header text correctly on chapter pages but does not clear the headrule.

Infominimal working example

Code: Select all

\documentclass[11pt,letterpaper]{book}

\usepackage{fancyhdr}
\usepackage{tocloft}
\usepackage{blindtext}

\pagestyle{fancy}

\newcommand{\bookheader}{\sffamily\fontseries{b}\fontsize{10}{12}\selectfont}

\fancypagestyle{plain}{
	\fancyhf{}
	\renewcommand{\headrulewidth}{0pt}%
}
\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{#1}}{}}
\renewcommand{\sectionmark}[1]{\markright{\MakeUppercase{#1}}}

\fancyhead[LO]{\bookheader{THE TITLE}}
\fancyhead[CO]{\bookheader\thepage}
\fancyhead[RO]{\bookheader\leftmark}
\fancyhead[LE]{\bookheader\leftmark}
\fancyhead[CE]{\bookheader\thepage}
\fancyhead[RE]{\bookheader{THE TITLE}}

\renewcommand{\headrulewidth}{0.4pt}

\fancypagestyle{plain}{
	\fancyhf{}
	\fancyfoot[C]{\bookfooter \empty}
	\renewcommand{\footrulewidth}{0.0pt}
}

\renewcommand{\footrulewidth}{0.0pt}
\fancyfoot[C]{\bookfooter \empty}

\makeatletter
\def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else
	\hbox{}
	\vspace*{\fill}
	\begin{center}
		%This page intentionally contains only this sentence.
	\end{center}
	\vspace{\fill}
	\thispagestyle{empty}
	\newpage
	\if@twocolumn\hbox{}\newpage\fi\fi\fi}
\makeatother

\begin{document}
	\blinddocument
\end{document}
[/MWE]
Last edited by petersfreeman on Wed Apr 28, 2021 8:18 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

Fancyhdr fails to clear headrule on chapter pages

Post by Bartman »

Please make sure that your Infominimal working example is complete. The source code is marked with the help of the Code button, while the MWE button inserts a link to the instructions for a minimal working example.

You change the plain style twice. The first time, the thickness of the line below the page header is set to zero. The second change then overwrites the first without setting the thickness of the same line to zero again.

As far as I can see, in both cases there is no reason to set \footrulewidth to zero.
petersfreeman
Posts: 19
Joined: Wed Apr 28, 2021 9:40 am

Fancyhdr fails to clear headrule on chapter pages

Post by petersfreeman »

My apologies. I am unfamiliar with the code/MWE system as this is the first time I have posted on such a system. Can I correct it by editing my original post? If not, I'll try again. I also realized I should have commented the steps to explain my rational. This time, I pressed the code button, inserted my code between code and /code and then bracketed the entire section with the MWE and /MWE enclosure. Have I done that right?

Somehow I can't help but think that my approach is all wrong. I'm seeing the problem as one when the XeLaTeX compiler encounters one of three different page types:
  1. A blank filler page necessary to place the chapter page on an odd page
  2. A chapter title page (first page in a chapter)
  3. A regular (non chapter title) page

it runs the code that matches those pages.

I have reworked the code and solved the problem of the line appearing on the chapter title pages, however I have introduced two new problems:
  1. Now the head rule line is appearing on pages that should be blank except for the phrase 'This page intentionally contains only this sentence.'
  2. The header does not contain my fields
Infominimal working example

Code: Select all

\documentclass[11pt,letterpaper]{book}

% Load packages
\usepackage{fancyhdr}
\usepackage{tocloft}
\usepackage{lipsum}

% Initialize fancyhdr with this mandatory statement
\pagestyle{fancy}

% Create a font shortcut
\newcommand{\bookheader}{\sffamily\fontseries{b}\fontsize{10}{12}\selectfont}

% Remove the Chapter label, i.e. 'Chapter 1 - '
\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{#1}}{}}
\renewcommand{\sectionmark}[1]{\markright{\MakeUppercase{#1}}{}}

% Set up code to show how to display 'empty' pages
\fancypagestyle{empty}{
	\makeatletter
	\def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else
		\hbox{}
		\vspace*{\fill}
		\begin{center}
			This page intentionally contains only this sentence.
		\end{center}
		\vspace{\fill}
		\thispagestyle{empty}
		\newpage
		\if@twocolumn\hbox{}\newpage\fi\fi\fi}
	\makeatother
	\fancyhf{} % Clear the header fields
	\renewcommand{\headrulewidth}{0pt} % Create an invisible header ruler line
}

% Setup code to be followed when LaTeX encounters a chapter title page
\fancypagestyle{plain}{
	\fancyhf{} % Clear the header fields
	\renewcommand{\headrulewidth}{0pt} % Create an invisible header ruler line
}

% Setup code to be followed when LaTeX is processing non-chapter title pages
\fancypagestyle{myheadings}{
	\fancyhead[LO]{\bookheader{BOOK TITLE}} % Put the book    title on the left  (inside)  of odd  pages
	\fancyhead[CO]{\bookheader\thepage}      % Put the page   number in the center          of odd  pages
	\fancyhead[RO]{\bookheader\leftmark}    % Put the chapter title on the right (outside) of odd  pages
	\fancyhead[LE]{\bookheader\leftmark}    % Put the chapter title on the left  (outside) of even pages
	\fancyhead[CE]{\bookheader\thepage}      % Put the page   number in the center          of even pages
	\fancyhead[RE]{\bookheader{BOOK TITLE}} % Put the book    title on the right (inside)  of even pages
	\renewcommand{\headrulewidth}{0.4pt}    % Create a thin header ruler line
}

\begin{document}
	\chapter{Introduction}
		\lipsum[1-10]
	\chapter{Process}
		\lipsum[1-10]
\end{document}
[/MWE]
Last edited by petersfreeman on Wed Apr 28, 2021 7:02 pm, edited 2 times in total.
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Fancyhdr fails to clear headrule on chapter pages

Post by Ijon Tichy »

Your example is not working, because \markboth expects two arguments, but you are using only one. So the second argument breaks the internal usage of \chaptermark. You should replace:

Code: Select all

\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{#1}}}
e.g. by

Code: Select all

\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{#1}}{}}
Maybe this also solves your other issues, which I do not understand.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
petersfreeman
Posts: 19
Joined: Wed Apr 28, 2021 9:40 am

Fancyhdr fails to clear headrule on chapter pages

Post by petersfreeman »

Thanks, Ijon Tichy, that solves the 'lof' at the beginning of the chapter. I'll edit the code to add that correction and remove the error message and that item from my list.

I still have the problem of the mandatory blank page not appearing correctly to fix,

Peter.
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Fancyhdr fails to clear headrule on chapter pages

Post by Ijon Tichy »

Do you want the empty page without head and foot? Then you can try:

Code: Select all

\usepackage[cleardoublepage=empty]{scrextend}
The package provides some features of the KOMA-Script classes for, i.e., the standard classes.

BTW: You are defining a new page style without activating it. So if myheadings should be used, you have to add \pagestyle{myheadings} (after defining it). But I would recommend another name for the pagestyle, because book already has a myheadings. And I do not understand, why you are not simply using fancyhdr's pagestyle fancy. Sorry, but I do not understand, what you expect and why you do the things your are doing.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
petersfreeman
Posts: 19
Joined: Wed Apr 28, 2021 9:40 am

Fancyhdr fails to clear headrule on chapter pages

Post by petersfreeman »

Thanks, Ijon Tichy. I've made the changes off-line on my own compiler and that works. I will repost the new code. I still have the final problem of not getting the book title and page number in the header. Also, the page number is appearing in the footer. I want the footer to be always blank.

Sorry, I did not see your last paragraph. Due to my ignorance, I have been trying to set up what I want by copying examples I see on the internet and trying to understand how they work. I recognize that I lack the overall structural idea of how fancyhdr works in spite of reading the documentation on it as carefully and as thoroughly as I can.

I'll go back and read the fancyhdr documentation again to see how to get it to do what I want which is:

Even-side filler pages before chapter pages: Completely blank
Chapter pages (always odd-side): No header or footer
Non-chapter pages: No footer with header as described below (I don't know how this is going to render so I'll put it in code tags):

Code: Select all

                                  (Header)
            | Left side            Centre              Right side  |
            +------------------------------------------------------+
Odd Pages:  | BOOK TITLE              45              INTRODUCTION |    or
Even Pages: | INTRODUCTION            46                BOOK TITLE |
Head Rule:   _______________________________________________________
.
.                                   (Content)
.                 
            |                                                                       |
            +------------------------------------------------------+
                               (Footer - Always blank)
Infominimal working example

Code: Select all

\documentclass[11pt,letterpaper]{book}

% Load packages
\usepackage{fancyhdr}
\usepackage{tocloft}
\usepackage{lipsum}
\usepackage[cleardoublepage=empty]{scrextend}

% Initialize fancyhdr with this mandatory statement
\pagestyle{fancy}

% Create a font shortcut
\newcommand{\bookheader}{\sffamily\fontseries{b}\fontsize{10}{12}\selectfont}

% Remove the Chapter label, i.e. 'Chapter 1 - '
\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{#1}}{}}
\renewcommand{\sectionmark}[1]{\markright{\MakeUppercase{#1}}{}}

% Setup code to be followed when LaTeX encounters a chapter title page
\fancypagestyle{plain}{
	\fancyhf{} % Clear the header fields
	\renewcommand{\headrulewidth}{0pt} % Create an invisible header ruler line
}

% Setup code to be followed when LaTeX is processing non-chapter title pages
\fancypagestyle{headings}{
	\fancyhead[LO]{\bookheader{BOOK TITLE}} % Put the book    title on the left  (inside)  of odd  pages
	\fancyhead[CO]{\bookheader\thepage}     % Put the page   number in the center          of odd  pages
	\fancyhead[RO]{\bookheader\leftmark}    % Put the chapter title on the right (outside) of odd  pages
	\fancyhead[LE]{\bookheader\leftmark}    % Put the chapter title on the left  (outside) of even pages
	\fancyhead[CO]{\bookheader\thepage}     % Put the page   number in the center          of even  pages
	\fancyhead[RE]{\bookheader{BOOK TITLE}} % Put the book    title on the right (inside)  of even pages
	\renewcommand{\headrulewidth}{0.4pt}    % Create a thin header ruler line
}

\begin{document}
	\chapter{Introduction}
		\lipsum[1-10]
	\chapter{Process}
		\lipsum[1-10]
\end{document}
[/MWE]
Last edited by petersfreeman on Wed Apr 28, 2021 7:43 pm, edited 1 time in total.
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Fancyhdr fails to clear headrule on chapter pages

Post by Ijon Tichy »

As already told, either use:

Code: Select all

\documentclass[11pt,letterpaper]{book}

% Load packages
\usepackage{fancyhdr}
\usepackage{tocloft}
\usepackage{lipsum}
\usepackage[cleardoublepage=empty]{scrextend}

% Initialize fancyhdr with this mandatory statement
\pagestyle{fancy}

% Create a font shortcut
\newcommand{\bookheader}{\sffamily\fontseries{b}\fontsize{10}{12}\selectfont}

% Remove the Chapter label, i.e. 'Chapter 1 - '
\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{#1}}{}}
\renewcommand{\sectionmark}[1]{\markright{\MakeUppercase{#1}}{}}

% Setup code to be followed when LaTeX encounters a chapter title page
\fancypagestyle{plain}{
	\fancyhf{} % Clear the header fields
	\renewcommand{\headrulewidth}{0pt} % Create an invisible header ruler line
}

% Setup code to be followed when LaTeX is processing non-chapter title pages
%\fancypagestyle{headings}{
        \fancyhf{}% Clear the header and footer fields (otherwise you would get two page numbers)
        \fancyhead[LO]{\bookheader{BOOK TITLE}} % Put the book    title on the left  (inside)  of odd  pages
	\fancyhead[CO]{\bookheader\thepage}     % Put the page   number in the center          of odd  pages
	\fancyhead[RO]{\bookheader\leftmark}    % Put the chapter title on the right (outside) of odd  pages
	\fancyhead[LE]{\bookheader\leftmark}    % Put the chapter title on the left  (outside) of even pages
	\fancyhead[CE]{\bookheader\thepage}     % Put the page   number in the center          of EVEN  pages
	\fancyhead[RE]{\bookheader{BOOK TITLE}} % Put the book    title on the right (inside)  of even pages
	\renewcommand{\headrulewidth}{0.4pt}    % Create a thin header ruler line
%}

\begin{document}
	\chapter{Introduction}
		\lipsum[1-10]
	\chapter{Process}
		\lipsum[1-10]
\end{document}
To not define a new pagestyle, or

Code: Select all

\documentclass[11pt,letterpaper]{book}

% Load packages
\usepackage{fancyhdr}
\usepackage{tocloft}
\usepackage{lipsum}
\usepackage[cleardoublepage=empty]{scrextend}

% Initialize fancyhdr with this mandatory statement
\pagestyle{fancy}

% Create a font shortcut
\newcommand{\bookheader}{\sffamily\fontseries{b}\fontsize{10}{12}\selectfont}

% Remove the Chapter label, i.e. 'Chapter 1 - '
\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{#1}}{}}
\renewcommand{\sectionmark}[1]{\markright{\MakeUppercase{#1}}{}}

% Setup code to be followed when LaTeX encounters a chapter title page
\fancypagestyle{plain}{
	\fancyhf{} % Clear the header fields
	\renewcommand{\headrulewidth}{0pt} % Create an invisible header ruler line
}

% Setup code to be followed when LaTeX is processing non-chapter title pages
\fancypagestyle{PFheadings}{
        \fancyhf{}% Clear the header and footer fields (otherwise you would get two page numbers)
        \fancyhead[LO]{\bookheader{BOOK TITLE}} % Put the book    title on the left  (inside)  of odd  pages
	\fancyhead[CO]{\bookheader\thepage}     % Put the page   number in the center          of odd  pages
	\fancyhead[RO]{\bookheader\leftmark}    % Put the chapter title on the right (outside) of odd  pages
	\fancyhead[LE]{\bookheader\leftmark}    % Put the chapter title on the left  (outside) of even pages
	\fancyhead[CE]{\bookheader\thepage}     % Put the page   number in the center          of EVEN  pages
	\fancyhead[RE]{\bookheader{BOOK TITLE}} % Put the book    title on the right (inside)  of even pages
	\renewcommand{\headrulewidth}{0.4pt}    % Create a thin header ruler line
}
\pagestyle{PFheadings}

\begin{document}
	\chapter{Introduction}
		\lipsum[1-10]
	\chapter{Process}
		\lipsum[1-10]
\end{document}
with explicite activation of the new pagestyle.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
petersfreeman
Posts: 19
Joined: Wed Apr 28, 2021 9:40 am

Fancyhdr fails to clear headrule on chapter pages

Post by petersfreeman »

Thanks, Ijon Tichy! That is exactly what I want! I'll study the code to understand why it works properly...

studying the code...

...done! I think I would go with the first example and NOT define a new page style. It seems more straightforward. I see how it works by not wrapping the default page format in a page style and clearing the LaTeX imposed header/footer system.

Thank you so much for your help!

Peter
Post Reply