[Tex/LaTex] How to put the page numbers where I need them

header-footerpage-numbering

I need to put the number for each page that is numbered at the top right hand corner of the document. Currently that condition is satisfied except for the first page of each chapter. That is, for the page that contains the title of the chapter the number is at the bottom (center) of the page. Can anyone help with this?

Best Answer

By default the first page of each \chapter is set using the plain page style under the basic book and report document classes. To see why this is the case, consider the \chapter macro:

\newcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
                    \thispagestyle{plain}%
                    \global\@topnum\z@
                    \@afterindentfalse
                    \secdef\@chapter\@schapter}

Note the setting of \thispagestyle{plain}. If you wish to change that, you can either create a new page style and update plain.

  • Creating a new style:

    Using fancyhdr you can either use the default fancy page style or create your own. Here's how to create your own:

    \fancypagestyle{main}{
      \fancyhf{}% Clear all headers/footers
      \fancyhead[R]{\thepage}% Page in Right header
      \renewcommand{\headrulewidth}{0.4pt}% Header rule
      \renewcommand{\footrulewidth}{0pt}% No footer rule
    }
    

    The usage with something like titleps is similar:

    \newpagestyle{main}{
      \sethead{}% Left header
        {}% Centre header
        {\thepage}% Right header
      \setfoot{}{}{}% Clear footer
      \setheadrule{.4pt}% Header rule
      \setfootrule{0pt}% Not footer rule
    }
    

    Now you either insert this style on the chapter pages manually using

    \chapter{My chapter}
    \thispagestyle{main}
    

    or you update this through a patch to be implemented globally. Here's a patch using etoolbox (place this in your document preamble):

    \usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
    \patchcmd{\chapter}% <cmd>
      {plain}% <search>
      {main}% <replace>
      {}{}% <success><failure>
    
  • Update plain:

    This is similar to the above solutions. For fancyhdr, use

    \fancypagestyle{plain}{%
      % your new plain settings
    }
    

    For titleps use

    \renewpagestyle{plain}{%
      % your new plain settings
    }
    

The above changes slightly if you're using a two-side document setup. However, it changes drastically if you're using another document class.