How can I add "page # of ##" on my document?

I have not tried, but I found this at some old .tex files:

\usepackage{lastpage}
\usepackage{fancyhdr}
\pagestyle{fancy} 
...
\cfoot{\thepage\ of \pageref{LastPage}}

enter image description here


If the document just contains arabic page numbers, probably the lastpage package is your friend:

\usepackage{lastpage}
...
\cfoot{\thepage\ of \pageref{LastPage}}

If the document consists of e.g. X (ten) pages with Roman numbers in the front matter, 100 pages with arabic numbers in the main matter, and K (eleven) pages with alphabetical "numbered" pages in the back matter, \pageref{LastPage} will give "K", the name of the last page. If instead "121" (=10+100+11) shall be shown, the pageslts package can be used:

\usepackage{pageslts}
...
\cfoot{Page \thepage\ (\theCurrentPage) of \lastpageref{LastPages}}

which e.g. for page "B" in the backmatter would print "Page B (112) of 121". pageslts offers a lot of additional possibilities (probably "overkill" for a document with only arabic numbers).

When there is output via \AtEndDocument, \pageref{LastPage} of the lastpage package will not include that output, if that was placed \AtEndDocument after loading the package. If you want to reference the very last page, \lastpageref{VeryLastPage} of pageslts package can be used.

If \addtocounter{page}{...} or \setcounter{page}{...} is used, "LastPage does not give the total number of pages (even if arabic is the page numbering scheme of that page). (The pageslts package puts \lastpageref{LastPages} (with s at the end) at your disposal for remediation: LastPages ignores page number manipulation.)" (from the lastpage documentation).

If the last page uses the fnsymbol page numbering scheme, lastpage could get problems. (pageslts should work.)

Generally for generating "page ... of ..." two compilation runs are required. Depending on other packages, even more migt be necessary. See for any rerun messages at the end of the log-file.

If the hyperref package is used, but \pageref{LastPage} or \lastpageref{LastPages} shall not create a hyperlink, the starred forms are available: \pageref*{LastPage} and \lastpageref*{LastPages}.

The lastpage and pageslts manuals also list some further alternatives:

  • lastpage209.sty (also from the lastpage package), for LaTeX 2.09
  • pageslts (as mentioned above)
  • totpages
  • totcount used with the page counter
  • nofm.sty (if anyone would be able to find a working version thereof) ftp://tug.ctan.org/pub/tex-archive/obsolete/macros/latex209/contrib/misc/nofm.sty,
  • count1to
  • zref, additionally requiring e-TeX (included in all modern TeX distributions), "but if you already have e-TeX, you may have a look at the extensive zref package, whether it suits your needs better (or additionally or whatsoever)" (lastpage documentation again).

And Brent.Longborough additionally mentioned here the memoir package providing \thelastpage and \thelastsheet.

(If the question was "How do you add “page # of ##” on your document?" this would be a CW question...)


There is an alternative to the lastpage provided by the zref package. It has a lastpage module which provides the same functionality using the modern zref way to make references. Simply use \zpageref{LastPage} instead of \pageref{LastPage}.

For the minimal use-case this doesn't give any benefits, but with zref the number is not hyperlinked by default (if hyperref is loaded of course) and it also provides a \ziflastpage{<refname>}{<true>}{<false>} conditional to test if a certain label got placed on the last page. There are also further macros for package writers. See the zref manual for them.

\documentclass{article}

\usepackage[lastpage,user]{zref}

\usepackage{fancyhdr}
\pagestyle{fancy}
\cfoot{\thepage\ of \zpageref{LastPage}}

\begin{document}

The last page is \zpageref{LastPage}.

\newpage

...

\newpage

...

\newpage

on the last page

\end{document}