Page number position

I am working on a long document in LaTeX with documentclass book. I need the page number to always be in the upper right corner of each page, even if that page is the first page of a chapter (right now on 1st pages of chapters, the page number is bottom-centered, on all other pages it's top-right).

I control the position of the page number with fancyhdr:

\usepackage{fancyhdr}
\pagestyle{fancy}
\lfoot{}
\cfoot{}
\rfoot{}


Also, I don't know if the problem is related but my chapters do not start from the top of the page. There is a white area, then comes chapter X, then a newline with the chapter line. What I also want is the chapter to start from the top of the page.

The main question here is how I can get the page number to always appear in the upper right corner, I mention the thing with the chapter title position only in case that might be related.

The first page of a chapter uses the plain pagestyle, even if some other style has been selected. You can fix this by adding \thispagestyle{fancy} after each \chapter{} command.

The blank space at the top of the first page of the chapter is part of the standard chapter style. You can use the fncychap package to create your own style (or use one of the packaged styles).

Update: For special chapters generated by command (e.g., \tableofcontents) which span more than one page, adding \thispagestyle{fancy} won't work. A more general solution is to add the following in your preamble:

\makeatletter
\renewcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
\thispagestyle{fancy}%
\global\@topnum\z@
\@afterindentfalse
\secdef\@chapter\@schapter}
\makeatother

• Great! Using "\thispagestyle{fancy}" (not "\thispagestyle{fancyhdr}") I could indeed fix this for all normal chapters and for the special chapter title "List of Tables". However, for the special chapter titles "Contents", "List of Figures", "References" and "Index" it does not work, these chapters stretch over multiple pages and on the first page the page number is still positioned bottom-centered. Do you also know how to deal with these cases? (Contents, List of Figures and so on)? – anton May 8 '10 at 5:29
• Sorry. I fixed the error, and added a comment about these special chapters. – Rob Hyndman May 8 '10 at 10:07
• Thank you Rob for looking into this. However when I added your code (from \makeatletter until \makeatother), there is absolutely no change at all. I am using "\usepackage{fancyhdr}" with that code, and the document compiles successfully (but no change in the layout). – anton May 9 '10 at 10:49

If I understand the question correect, the only problem is that the plain pagestyle was not defined in fancyhdr. so all you have to do is

From the manual

\fancypagestyle{plain}{%
\fancyhf{} % clear all header and footer fields
\renewcommand{\footrulewidth}{0pt}}


Note that there are three pagestyles.

empty: Is used for pages such as titlepages.

plain: Is used for chapter starts which includes the start of lists such as \tableofcontens and \listoffigures

fancy: For normal text pages

Also besides fancyhdr there are other pages such as scrpage2 that allow the manipulation of pagestyles.

• It is worth noting that you can create page style with any name using syntax in this answer and then use \thispagestyle{styleforchapters} at the start of each chapter. – Rafal Nov 6 '14 at 13:55
• The above code gives all the page numbers top right. Great. How if want the page numbers on bottom right? Appreciate any help. I am looking for this solution very badly now. – Vijay Sep 16 '15 at 4:03

It is easy to modify the pagestyle for regular and special chapters such as your table of contents. Chapters normally override the current pagestyle, setting it to plain. fancyhdr provides a command \fancypagestyle to redefine other pagestyles, and in this manner you can get the style you want on chapter pages by simply redefining the plain style as follows:

\fancypagestyle{plain}{\pagestyle{fancy}}


This is explained in section 7 of the fancyhdr documentation.

Chapter pages uses page style plain by default. Package fancyhdr does not redefine plain automatically. But if you replace page style fancy by fancyplain you can modify both styles at once:

\documentclass{report}
\usepackage{blindtext}% only for dummy text
\usepackage{fancyhdr}
\pagestyle{fancyplain}% <- use fancyplain instead fancy
\fancyhf{}
\begin{document}
\blinddocument
\end{document}


Result:

If there should be different content for plain and fancy use macro \fancyplain:

\documentclass{report}
\usepackage{blindtext}% only for dummy text
\usepackage{fancyhdr}
\pagestyle{fancyplain}% <- use fancyplain instead fancy
\fancyhf{}
\begin{document}
\blinddocument
\end{document}


Result:

If there should be a head rule on all pages, remove the redefinition of \headrulewidth and redefine \plainheadrulewidth:

\documentclass{report}
\usepackage{blindtext}% only for dummy text
\usepackage{fancyhdr}
\pagestyle{fancyplain}% <- use fancyplain instead fancy
\fancyhf{}