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:
- Different page style in the appendix
- 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:
Jason
This is excellent. I am encountering a situation where I need to add a statement to the bottom of a few select pages of a document as a footer. My problem is that I need to add the footer to a page that only has two, large, floating figures on it (no inline text). The command \thispagestyle is inline with the text and does not float with the figures. How do I change the page style of the specific page where those two figures end up? Thanks.
tom
Hi Jason,
Thanks for your question! The fancyhdr package implements the
\iffloatpage
macro, which might do exactly what you are looking for. See here for an example.Tom
Vipin Kumar
Dear Sir,
I am going to write my thesis. My question is following.
I am using following code
This gives top page header on each page with the chapter name on left and page no. on right.
How can I get page no., chapter no. and chapter name on left (odd) page while section no./ name on right (even) page. Please help me.
tom
Heya!
I created a minimal working example.
Thesis
is not a standard document class. I usedreport
instead. But it shouldn’t make a difference.Cheers, Tom
Panha
Work like charm, !
aju
Hey
Im trying to make footer @left side with page numbering @right n it works but not for first page of every chapter plz help
tom
Hi Aju,
The first page of a chapter will always have page style
plain
. You can change this for example by patching the chapter command in your preamble.See here for more details.
HTH, Tom
Gian Paolo Renello
Hi Tom,
is there a way to use \pagestyle command on different files included or imported as separated tex files in in a main.tex document
Tx a lot
Gian Paolo
tom
Hi Gian Paolo,
Yes of course, just define the page style in the preamble and switch to it at the beginning of the imported files.
Please provide a minimal working example if you require more detailed help.
Best, Tom