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 want to add a footer to my document displaying the current number out of total number of pages. How do I do that?

share|improve this question

7 Answers 7

up vote 69 down vote accepted

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

\usepackage{lastpage}
...
\cfoot{\thepage\ of \pageref{LastPage}}
share|improve this answer
6  
+1 This is the method recommended within The LaTeX Compantion. –  David Z Jul 27 '10 at 0:40
1  
When using the Ruby rtex gem with Rails, this wasn't working for me at first. Then I added :preprocess => true to my rails controller render block and all is good! render :filename => "schedule.pdf", :preprocess => true, :file => 'document.pdf.rtex', :content_type => 'application/pdf', :processor => "pdflatex -shell-escape" I had read somewhere that using \pageref requires running latex twice. –  Docunext May 9 '11 at 18:56
8  
@Docunext: Using lastpage does require two passes --- latex has no way to know how many pages a document will contain until it has had the chance to compile the document. –  Sharpie May 9 '11 at 19:09
8  
Note that this requires loading fancyhdr –  egreg Apr 9 '12 at 14:28
    
Minor mod, to get the right spacing, \thepage\~of~\pageref{LastPage}} –  Nicholas Hamilton Dec 20 '12 at 4:05

Using the lastpage package, you can use the \pageref{LastPage} command to get the final page number of the document.

So the following code:

page \thepage\ of \pageref{LastPage}

would print "page # of ##" (where # is the current page number and ## is the final page number).

share|improve this answer

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}
share|improve this answer
4  
To suppres hyperlink to last page in lastpage one can use \pageref*{LastPage}. –  Adobe Feb 23 '13 at 11:04

The memoir package provides \thelastpage and \thelastsheet from their respective counters, depending on your precise needs.

share|improve this answer
    
So with memoir one normally doesn't need lastpage or pageslts, correct? –  Lover of Structure Feb 24 '13 at 12:18
1  
@LoverofStructure : That's correct. –  Brent.Longborough Feb 24 '13 at 12:21

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:

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...)

share|improve this answer
4  
This is an excellent answer and summarizes all the issues very well. –  Yiannis Lazarides Oct 24 '11 at 8:42

Sorry to add my late answer but this can be done without the use of any package, just by adding at the end a label \label{LastPage}. The total number of page being a \pageref{LastPage}.

Hey, but this doesn't work with complex page numbering ??!! Well, that's not true :

  • One has to put the label at the end of the part where total page number is required.
  • To prevent the label from escaping to the following part (ie jumping from main matter to back matter), I add some whitespaces after the label : {LastPage}~.

Here is a working example. I redefine frontmatter, mainmatter and backmatter because they are not present in the article class. This homemade solution should fit any needs

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy} 
\newcommand{\frontmatter}{\clearpage \cfoot{\thepage\ }
\setcounter{page}{1}
\pagenumbering{Roman}}
\newcommand{\mainmatter}{\clearpage \cfoot{\thepage\ of \pageref{LastPage}}
\setcounter{page}{1}
\pagenumbering{arabic}}
\newcommand{\backmatter}{\clearpage \cfoot{\thepage\ }
\setcounter{page}{1}
\pagenumbering{alph}}

\begin{document}
\frontmatter
    The last page is \pageref{LastPageOfFrontMatter}.
    \newpage ... \newpage  ... \newpage
    on the last page of frontmatter
    \label{LastPageOfFrontMatter}~
\mainmatter
    The last page is \pageref{LastPage}.
    \newpage ... \newpage  ... \newpage
    on the last page of mainmatter
    \label{LastPage}~
\backmatter 
    The last page is \pageref{LastPageOfBackMatter}.
    \newpage ... \newpage  ... \newpage
    on the last page of backmatter
 \label{LastPageOfBackMatter}~
 \end{document}
share|improve this answer

You could also use the totalcount package as follows:

% arara: pdflatex
\documentclass{article}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage[page]{totalcount}

\pagestyle{fancy}
\fancyhf{}
\cfoot{Page \thepage~of \totalpages}
\begin{document}

\lipsum
\lipsum
\lipsum
\lipsum
\lipsum
\lipsum
\lipsum
\lipsum
\lipsum
\lipsum
\lipsum
\end{document}
share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.