Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

migrated from superuser.com Feb 1 '11 at 18:04

This question came from our site for computer enthusiasts and power users.

3 Answers

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
share|improve this answer
    
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
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.