1

Possible Duplicate:
How can I add “page # of ##” on my document?

While submitting Test paper in LaTeX, I was asked to display page numbers as current page number out of total page numbers.

I am using article class.

For example it should look like: 1 out of 4, 2 out of 4, 3 out of 4 etc. or 1/4,2/4 etc.

How to obtain it?

manjusha
  • 445

1 Answers1

10

You could simply use the lastpage package and use the following MWE to get the 1/3, 2/3, etc "look". If you want "out of" instead of "/", just replace \thepage/\pageref{LastPage} with \thepage\ out of \pageref{LastPage}. Be sure to compile the document twice to give LaTeX a chance to get the cross-referencing right.

Note that the code in the MWE doesn't require you to use any additional packages (such as fancyhdr) to manipulate the page headers and footers.

\documentclass{article}
\usepackage{lipsum,lastpage}  % 'lipsum' provides filler text
\makeatletter
\renewcommand{\@oddfoot}{\hfil \thepage/\pageref{LastPage} \hfil}
\makeatother
\begin{document}
\lipsum[1-15]
\end{document}

Addendum: Should you be using the hyperref package as well, you may want to replace the instruction \pageref with \pageref*. The former command would instruct hyperref to make its argument into a hyperlink -- probably not an intended effect. The hyperref package provides the variant command \pageref* (as well as \ref* ...) to avoid creating hyperlinks out of the cross-reference.

Mico
  • 506,678
  • It worked perfectly! – manjusha Apr 15 '12 at 13:28
  • This does not work with the first page ... – Hesam Apr 08 '13 at 06:52
  • @Hesam - May I ask you to be a bit more specific as to what doesn't work on the first page? Do you get an incorrect page number, or no "1 of nn" page marker, or is there another problem? – Mico Apr 08 '13 at 11:22
  • I only get "1", no "1 of X". It appears once you add \maketitle ... – Hesam Apr 09 '13 at 07:30
  • @Hesam - Are you working with example code given in my answer, or are you working with your own document class and document setup? The reason I ask is that it looks like your document class is setting the page style of the title page separately from that of the other pages -- in which case my answer won't apply. – Mico Apr 09 '13 at 08:54
  • I already explained, in your example code above add \title{...} and \maketitle and the first page number becomes "1" not "1 of X". – Hesam Apr 10 '13 at 19:18
  • @Hesam - Thanks for providing this explanation. The \maketitle macro of the article document class includes the instruction \thispagestyle{plain}, which undoes the redefinition of \@oddfoot of the MWE. If you use \maketitle, I suggest you load the fancyhdr package and follow the instructions in http://tex.stackexchange.com/a/235/5001. – Mico Apr 10 '13 at 20:50