[Tex/LaTex] page numbers should be of the form 4/7

page-numbering

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?

Best Answer

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.