texblog

Multiple page styles with fancyhdr

The fancyhdr package provides an easy and flexible way to customize the page style. Furthermore, the package makes it easy to define new page styles. In this post, I will briefly show how to define an alternative page style in general and illustrate two possible applications including complete code examples:

  1. Different page style in the appendix
  2. Different page style for chapter title pages

I assumes that you are familiar with fancyhdr. Otherwise, take a look at this post or the documentation.

 

Defining an alternative page style

Fancyhdr allows the definition of alternative page styles through the \fancypagestyle command.

\fancypagestyle{mypagestyle}{%
	%pagestyle definition
}

The alternative page style can then be used for a specific page or a range of pages, until it is changed back (e.g. to fancy).

\thispagestyle{mypagestyle}

\pagestyle{mypagestyle}
...
\pagestyle{fancy}

I will illustrate both with an example, starting with the latter.

 

1. Alternative appendix page style

To keep things simple, I defined a new page style appendix that has an empty header. \clearpage assures a smooth transition between the two page styles. Otherwise, the last page of the main body might already have the new page style.

\appendix changes chapter names and numbering, but it not required for the example to work.

\documentclass[12pt, twoside, openany]{report}
\usepackage{fancyhdr, blindtext}

\fancypagestyle{appendix}{%
	\fancyhead{}
	\renewcommand{\headrulewidth}{0pt}
}

\setlength{\headheight}{15pt} % fixes \headheight warning

\begin{document}

\pagestyle{fancy}

\tableofcontents

\chapter{Main body chapter}
\Blindtext\Blindtext

\clearpage

\appendix
\pagestyle{appendix}

\chapter{Appendix chapter}
\Blindtext\Blindtext

\end{document}

 
Main body page style:

 
Appendix page style with empty header:

 

2. Alternative chapter title page style

The page style for chapter title pages is set by redefining the chapter command, adding \thispagestyle{pagestyle}. Defining a new page style is not different from the previous example and therefore omitted here. I just use the standard fancy page style. The header for non-numbered (starred) chapters has to be set manually using \markboth.

I copied the code to redefine chapter from tex.SX.

\documentclass[12pt, twoside, openrany]{report}

\usepackage{fancyhdr, blindtext}

%Redefine chapter by adding fancy as the chapter title page page-style
\makeatletter
    \let\stdchapter\chapter
    \renewcommand*\chapter{%
	\@ifstar{\starchapter}{\@dblarg\nostarchapter}}
	\newcommand*\starchapter[1]{%
		\stdchapter*{#1}
		\thispagestyle{fancy}
		\markboth{\MakeUppercase{#1}}{}
	}
	\def\nostarchapter[#1]#2{%
		\stdchapter[{#1}]{#2}
		\thispagestyle{fancy}
	}
\makeatother

\setlength{\headheight}{15pt} % fixes \headheight warning

\begin{document}
\pagestyle{fancy}

\tableofcontents

\chapter{Some chapter}
\Blindtext\Blindtext
\clearpage

\chapter{Some other chapter}
\Blindtext
\clearpage

\chapter*{Some non-numbered chapter}
\Blindtext\blindtext\blindtext
\clearpage

\chapter*{Some other non-numbered chapter}
\Blindtext

\end{document}

 
Numbered chapter title page with header:

 
Non-numbered chapter title page with header:

Exit mobile version