[Tex/LaTex] Page number position

fancyhdrheader-footer

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.

Best Answer

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