33

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}
\lhead{}
\chead{}
\rhead{\thepage}
\lfoot{}
\cfoot{}
\rfoot{}
\renewcommand{\headrulewidth}{0pt}

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.

4 Answers 4

24

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
3
  • 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, 2010 at 5:29
  • Sorry. I fixed the error, and added a comment about these special chapters. May 8, 2010 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, 2010 at 10:49
14

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
\fancyhead[RO,RE]{\thepage} %RO=right odd, RE=right even
\renewcommand{\headrulewidth}{0pt}
\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.

2
  • 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, 2014 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, 2015 at 4:03
11

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.

6

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{}
\fancyhead[R]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\begin{document}
\blinddocument
\end{document}

Result:

enter image description here

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{}
\fancyhead[L]{content for \fancyplain{plain}{fancy} pages}% <- added
\fancyhead[R]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\begin{document}
\blinddocument
\end{document}

Result:

enter image description here

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{}
\fancyhead[L]{content for \fancyplain{plain}{fancy} pages}% <- added
\fancyhead[R]{\thepage}
%\renewcommand\headrulewidth{0pt}% default ist .4pt
\renewcommand{\plainheadrulewidth}{.4pt}% default is 0pt
\begin{document}
\blinddocument
\end{document}

Result:

enter image description here

You must log in to answer this question.