## Headers and Footers in LaTeX using fancyhdr

Posted: 7th August 2010 by Tim in LaTeX
Tags: , , , , , , , , , ,

There are a number of ways to change the document header and footer styles in LaTeX. One of the easiest and most flexible options is to use the fancyhdr package.

fancyhdr is a massive package with a crazy number of tweaks and modifications available for your document’s header and footer styles. This tutorial will only brush over the basic capabilities of the package, describing the most common styling choices.

We tell LaTeX to use fancyhdr by adding the following to the preamble (the top of the document before the \begin{document} call):

\usepackage{fancyhdr} \pagestyle{fancy}

fancyhdr allows the content of the left, middle and right parts of both the header and footer to be changed. By default, the header contains the chapter name on the left, the section name and number on the right and has a black line separating the header from the document. The footer contains the page number in the middle.

To clear the header and footer contents, add \fancyhf{} to the preamble below the pagestyle line. You can now set the contents of the header and footer using the following calls in the preamble:

\lhead{left header content} \chead{middle header content} \rhead{right header content} \lfoot{left footer content} \cfoot{middle footer content} \rfoot{right footer content}


There are a few commands you can use to put dynamic content in the header and footer:

\today – the current day. ie: August 6, 2010
\thepage – the current page number
\thesection – the current section number
\leftmark – current chapter name in uppercase. ie: CHAPTER 1. TITLE OF THE FIRST CHAPTER
\rightmark – current section name in uppercase. ie: 1.1. TITLE OF THE FIRST SECTION

We can also use information from other packages, like the lastpage package which gives the number of the last page.

That’s a lot of detail, so let’s look at an example:

\documentclass{article}

\usepackage{lastpage} % for the number of the last page in the document \usepackage{fancyhdr} \pagestyle{fancy} \fancyhf{} \lhead{--- DRAFT ---} \rhead{Section \thesection} \lfoot{\today} \rfoot{Page \thepage\ of \pageref{LastPage}}

\begin{document} \section{\LaTeX headers and footers} Headers and footers can be set with the fancyhdr package. \end{document}

Will produce this document

Note: remember to compile this document twice so that all the reference work properly.

1. Raj says:

Thanks Tim for this short tips on Latex headers. I am not sure if I can ask you a related question here. Just ignore if you don’t don’t want to. I am writing CV in latex found some templates. I liked the one using fancy header. It however looks like I cannot write two header lines about the horizontal line. I would like to put my name as lheader and my address and email in the second line rather than the default style. I will appreciate any help on this. Thanks.

2. Tim says:

Hi Raj,
Not 100% sure what you mean here but I think you want to put multiple lines in the header. You can do that by separating the lines with \\ within the \lhead call. For example, \lhead{Name\\Address\\Email} will make the left header three lines long.

3. George says:

Hello, I was wondering is it possible to take just the header away for a page with a table float on it? The table is landscape and goes over the header. Thanks for your help.

4. Tim says:

HI George. You can add \thispagestyle{empty} to not show any header or footer on that page. -T

5. Stephen says:

I like including lines in both the header and footer, and to have lhead and rhead appear at the left and right most sides of the page. Yet my pages have quite wide margins (I have many long equations and wish to squeeze much onto a single line). It seems that the default fancyhdr widths are smaller than my specified page layout. Any hints for how to have fancyhdr “feel” my margin widths, and so to adjust the placements of lhead and rhead, as well as the length of the lines?

6. Ken Beesley says:

Dear Mr. Murphy,
I stumbled onto this discussion, and wondered if you could help me with a related problem. I’m preparing a book for publication, using XeTeX, with fancyhdr. One of the editors requested (or demanted) that the headers be suppressed always and only when a (floating) graphic appeared at the top of a page. Is is possible, using fancyhdr, to have such suppression done automatically?

Thanks,

Ken

7. Haritha says:

Thanks for sharing.

8. swapna says:

Tim thank u for sharing

9. swapna says:

Thank u every one for sharing.an special thanks to tim because that was the one I wanted

10. swapna says:

Tim sir I’m just starting to type on latex can I ask u for help and can v insert pic in latex