Creating headers and footers in Latex can be done by using the package “fancyhdr”. This is a short introduction, showing the most important features of the package. If you know “fancyhdr” and are looking for something particular, refer to the fancyhdr-documentation.
First of all, you need to tell Latex to use the package:
\usepackage{fancyhdr}
and change the style from “plain” to “fancy”:
\pagestyle{fancy}
You will now the get the default fancy pagestyle which adds a line at the top of every page, except for some exceptions (title-page, abstract, new chapter in report).
Default fancyhdr page style:
Above the line, Latex will print headings:
Book/report
Left-hand side: section
Right- hand side: chapter
Note: if you use the optional documentclass argument “twoside”, Latex will alter the position of the section and chapter. (e.g. \documentclass[twoside]{report}, also introducing non-symmetric margins).
Article
For acticles, Latex will print the section only (chapters cannot be used with articles).
The footer only includes the page number which is centered by default.
Custom fancyhdr page style:
Even though fancyhdr has a default page style, you are free to define headers/footers yourself , which is not too difficult after all.
First you need to clear the default layout:
\fancyhead{}
\fancyfoot{}
There are seven letters you need to know before you can define your own header/footer:
E: Even page
O: Odd page
L: Left field
C: Center field
R: Right field
H: Header
F: Footer
Now you can start to define your own layout. The definitions are added before the document starts, usually after the “usepackages”:
\fancyhead[CO,CE]{---Draft---}
\fancyfoot[CO,CE]{Confidential}
\fancyfoot[RO, LE] {\thepage}
The decorative lines can be changed by increasing/decreasing their thickness (0pt means no line):
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
Note: After the fancyhdr-documentation, the default layout is produced by the following commands:
\fancyhead[LE,RO]{\slshape \rightmark}
\fancyhead[LO,RE]{\slshape \leftmark}
\fancyfoot[C]{\thepage}
\headrulewidth 0.4pt
\footrulewidth 0 pt
Tags: article, book, fancy, fancyfoot, fancyhdr, fancyhead, LaTex, pagestyle, renewcommand, report, usepackage
December 1, 2007 at 6:51 pm |
Thanks, this was exactly what I was looking for. If you have time could you leave a comment explaining how to change the header font size and margins? I’d appreciate it.
December 4, 2007 at 7:09 pm |
Good job, very helpful. Thanks.
December 8, 2007 at 2:02 pm |
@ez
To answer your question:
First for the margins, you can access them through the following commands:
\fancyheadoffset[]{} and \fancyfootoffset[]{}
E.g. to change the width of a two-sided document’s header to the outer maximum (you need the calc-package for the addition):
\fancyheadoffset[LE,RO]{\marginparsep+\marginparwidth}
The font size can be changed directly where you define the header/footer.
E.g. to decrease the font size of the page numbering you would use:
\fancyfoot[RO, LE] {\footnotesize \thepage}
Check the fancyhdr documentation for more details on that topic:
http://www.ctan.org/tex-archive/macros/latex/contrib/fancyhdr/fancyhdr.pdf
Cu,
Tom.
March 5, 2008 at 4:40 pm |
Thanks! Fantastic tutorial!!!
April 23, 2008 at 2:53 pm |
Great! Thank you.
Tapani
June 23, 2008 at 8:34 pm |
i actually saved this to del.icio.us. thank you so much!
June 25, 2008 at 11:23 am |
I always wanted to know how to format headers/footers
Great tutorial!
September 27, 2008 at 7:14 pm |
I am writing my thesis template and would like to include image(logo) in the header. I tried several options, but none of them worked. Could anybody suggest me the way to insert image in the header section? I would be very grateful.
Another problem is the line spacing in the footer. I want to have different line spacing (less than the main text).
Thanks
September 29, 2008 at 7:40 pm |
Hi Bikash,
Including an image in your header is possible using the fancyhdr package.
E.g. if you’d like to have a logo in the top-right corner, you would use:
\fancyhead[RO,RE]{\includegraphics[width=3cm]{picture.jpg}}Concerning the text spacing in the footer, the only solution I can propose, even though not very nice, is the following:
\fancyfoot[LE,RO]{1st line\vspace{-2mm}\\ 2nd line}You can find a documentation of the fancyhdr package here.
Cheers,
Tom
October 10, 2008 at 10:39 pm |
[...] Header and Footer in LaTeX – Explained [...]
October 13, 2008 at 9:47 am |
I have been using fancyhdr package for a while but have knocked my head in to a problem which I can’t solve:
In a large document, using “BOOK” style, no header/footer is displayed on the chapter page it self. I have tried to redefine the “Plain” setup (which is called when chapter is included):
\fancypagestyle{plain}
\fancyhead…
BODY
\fancfoot …
\pagestyle{plain}
The problem is that I don’t know the “BODY” formatingen of a normal CHAPTER page. I simply want the CHapter to look as a chapter but adding a centered header and footer on these pages as well.
Any simple solutions ? Have read the fancyhd doc and I have not figured out any thing yet. I find it unnecessary to add the KOMA script package simply for this. Any help would be appreciated.
K
October 14, 2008 at 9:47 pm |
Hi Kent,
Actually, what you did is correct, using
\fancypagestyle{plain}Now if you want to modify the forced fancy-plain-pagestyle, you can add your own definitions as follows:
\fancypagestyle{plain}{%\fancyfoot[C]{\thepage}
%etc.
}
Cheers,
Tom
October 15, 2008 at 4:03 pm |
Tom,
I do agree with you here but then I have to dig out the formating of the Chapter page, to make the correct heading sizes and make the page look like a Chapter start page.
I did however fin something which might help others. It’s much easier, but not something I have found documenter.
It goes like this :
\usepackage{fancyhdr}
\pagestyle{fancyplain} %Note the \fancyplain command !!!
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}{}}
Then “normal formating of \lhead,\chead but with a twis:
\lhead[\fancyplain{E}{EE}] {\fancyplain{O}{OO}}
E:”Text”-forced into “plain” pages aka first page of Chapter etc- Even pages
EE: Text”-for all the other pages – Even pagenumber
Note the \lhead[ ] { } combination of different brackets !
O: Text”-pforced into “plain” pages aka first page of Chapter etc- Odd pages
OO: ext”-for all the other pages – Even pagenumber
\chead[\fancyhead{}{}]{\fancyplain{}{}}
\cfoot [ ] { }
.
.
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
If you read Piet van Oostrums document there is nothing mentioned about “\fancyplain”. Nor do you find it documented in The Latex Companinon where both refers to the re-definition of theĀ ”Plain” style.
I’m not sure if this is something which is elementary obvious for either the Authors of the “Companion” book or Mr Oostrum, but I think it would be worth a sentence or two in both documents.
Note that you need to document each of the \lhead, \chead….\rfoot to eventually “blank” each part of the header\footer positions. If not, LaTeX apply the normal fancy fields in each position, where for instance \rightmark would give you ONLY the text from the expected fields in the document (No Subsection number etc) but also all the others. So use \rfoot[\fancyplain{}{}]{\fancyplain{}{}} to blank for instance right corner.
Hope it helps for those who would like to add “something” on the first page of each Chapter,Tableofcontents,Listoftables etc
If it’s well known, OK, if not spread the word wherever you see people having these problems.
I found this out by reading the old documentation of the now not supported \fancyheader documentation – the older version of \fancyhdr.
Regards
Kent
October 16, 2008 at 7:18 pm |
Hi Kent,
Thanks for your explanations, I appreciate your feedback.
Good luck with your work,
Tom
October 18, 2008 at 10:19 pm |
Thanks for this very helpful information. I still have one technical problem and hope to share with you all:
I am trying to set up a page with left margin 1.5″, and all other margins 1.0″. I did this without problem using the geometry package like this:
\usepackage{geometry}
\geometry{hmargin={1.5in,1in},vmargin={1in,1in}}
However, for the page number, the specification requires me to put it on top right of each page, 0.75″ from the top and right margin. Using fancyhdr, I did manage to move it horizontally with \fancyheadoffset[HR]{0.25in}. However, I cannot find a way to snug it down to 0.75″ from the top while still preserving the 1″ top margin for the text.
Any insight would be appreciated.
Here is the whole format codes I used:
\usepackage{geometry}
\geometry{hmargin={1.5in,1in},vmargin={1in,1in}}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\fancyhead{}
\fancyfoot{}
\rhead{\thepage}
\fancyheadoffset[HR]{0.25in}
Thanks a lot.
October 21, 2008 at 7:37 pm |
Hi there,
You can control the vertical space for the header using the commands below.
Space between page border and header (1in + space). You can also use someting negative if you like the space to be below 1 inch.
\setlength\voffset{-0.25in}Height of the actual header.
\setlength\headheight{12pt}Separation between header and text.
\setlength\headsep{25pt}If you want to change other margins of the header or footer, please refer to the fancyhdr documentation on page 3.
Cheers,
Tom.
January 30, 2009 at 10:06 am |
Thanks for a great introduction. I have been looking for simple instructions how to modify the headers and footers.
March 10, 2009 at 1:26 pm |
That was exactly what I needed. Thanks
March 25, 2009 at 12:05 pm |
thanks guy.. very usefull tutorial…
April 16, 2009 at 4:44 pm |
thanks for your explanations! really helpful! cheers
May 1, 2009 at 4:17 am |
Ah, you can do dates with \today
August 30, 2009 at 3:24 pm |
Thank you very much for this very helpful guide.