I want to add a footer to my document displaying the current number out of total number of pages. How do I do that?
-
Good answer here: https://tex.stackexchange.com/a/646292/4736 – Keks Dose Jun 01 '22 at 11:25
13 Answers
I have not tried, but I found this at some old .tex files:
\usepackage{lastpage}
\usepackage{fancyhdr}
\pagestyle{fancy}
...
\cfoot{\thepage\ of \pageref{LastPage}}
- 1,611
- 5,919
- 7
- 36
- 40
-
14
-
1When using the Ruby rtex gem with Rails, this wasn't working for me at first. Then I added
:preprocess => trueto 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\pagerefrequires runninglatextwice. – Docunext May 09 '11 at 18:56 -
12@Docunext: Using
lastpagedoes require two passes ---latexhas no way to know how many pages a document will contain until it has had the chance to compile the document. – Sharpie May 09 '11 at 19:09 -
3Minor mod, to get the right spacing, \thepage~of~\pageref{LastPage}} – Nicholas Hamilton Dec 20 '12 at 04:05
-
17
-
-
-
@DavidZ When I use \usepackage{lastpage} \fancyfoot[C]{name {\thepage}~of~\pageref{LastPage}} I get this error:LaTeX Warning: Reference `LastPage' on page 1 undefined on input line 99.
[1{C:/ProgramData/MiKTeX/2.9/pdftex/config/pdftex.map}]
LaTeX Warning: Reference `LastPage' on page 2 undefined on input line 147.
[2]
LaTeX Warning: Reference `LastPage' on page 3 undefined on input line 189.
[3]
Package lastpage Warning: Rerun to get the references right on input line 207.
! Package lastpage Error: No auxiliary file allowed.
– 5823574 Jul 31 '15 at 15:53 -
5In addition to
\usepackage{fancyhdr},\pagestyle{fancy}or\thispagestyle{fancy}are needed. – Skippy le Grand Gourou Oct 13 '15 at 14:07 -
@SkippyleGrandGourou
\pagestyle{fancy}works for me, but not\thispagestyle{fancy}. I'm usingscrartcl. – Faheem Mitha Dec 12 '15 at 22:32 -
3
-
9One more useful note: to make the number black, use \pageref*{LastPage} – Ben Usman Feb 10 '17 at 20:00
-
7@jontejj Could you elaborate? Why do you think this is incompatible with the
hyperrefpackage? The other answers suggest otherwise and recommend that you use\pageref*{LastPage}instead if you want to remove the link. – caw Nov 15 '17 at 21:31 -
2@BenUsman What you are referring to is that the starred version (with the asterisk) tells the
hyperrefpackage not to make a link from the page number and thus the number is displayed as normal text, which often is black. – caw Nov 15 '17 at 21:33 -
Using the 'fancy' option on
fancyhdrseems to add a horizontal line in the header. If I change it to 'plain', the line disappears, but so does the Page X of Y. Any ideas? – Dan Lewer May 11 '19 at 18:15 -
how to add the last page to the first/title page? I need 1 of Y in the first page, but it's not applying there – Diego S Sep 19 '22 at 18:58
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...)
-
7
-
I use the
totpagespackage you reference. The total # of pages is invoked with\ref{TotPages}. – Steven B. Segletes Oct 19 '15 at 10:05
The memoir class provides \thelastpage and \thelastsheet from their respective counters, depending on your precise needs.
- 14,890
- 27,991
-
So with
memoirone normally doesn't needlastpageorpageslts, correct? – Lover of Structure Feb 24 '13 at 12:18 -
1
-
This is very useful as I am using memoir class for my books, so its better to have less external dependencies. Thanks for this method. – Ibn Saeed Dec 22 '15 at 16:28
-
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}
- 262,582
-
12To suppres hyperlink to last page in
lastpageone can use\pageref*{LastPage}. – Adobe Feb 23 '13 at 11:04
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}
- 301
-
3+1 for avoiding loading yet another package. Nice if you want to keep it simple. – Vid Merljak Jul 31 '17 at 12:00
-
1
-
This is good additional information, but it does not provide the total number of pages for the whole document. – Stephen Sep 03 '21 at 10:44
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).
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}
- 100,947
Here is a MWE using the lastpage package.
\documentclass[12pt]{article}
\usepackage{lastpage}
\usepackage{fancyhdr}
\usepackage{lipsum} % just for mock text
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\fancyhf{}% clear all fields
\fancyfoot[C]{\thepage\ of \pageref{LastPage}}
\begin{document}
\lipsum
\end{document}
If you are using \maketitle, it issues \thispagestyle{plain}. The effect of this is that the first page numbering will remain 1, rather than 1 of 2. So you will need to redefine the plain pagestyle. Here is a MWE in that case.
\documentclass[12pt]{article}
\usepackage{lastpage}
\usepackage{fancyhdr}
\usepackage{lipsum} % just for mock text
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot[C]{\thepage\ of \pageref{LastPage}}
\fancypagestyle{plain}{%
\renewcommand{\headrulewidth}{0pt}%
\fancyhf{}%
\fancyfoot[C]{\thepage\ of \pageref{LastPage}}}
\begin{document}
\title{MWE}
\maketitle
\lipsum
\end{document}
Thanks to egreg for the explanation and code for this example.
- 1,121,712
- 7,778
-
-
+1 thank you very much indeed, can you also show how to do this with the KOMA articles (they are clashing with fancyhdr)? – Dieter.Wilhelm Feb 26 '18 at 06:44
-
1@Dieter.Wilhelm Probably not - I'm no expert. I suggest you ask another question on the site. If you want, you could ask on chat first, but they'll probably just tell you to ask a question. – Faheem Mitha Feb 26 '18 at 06:48
-
-
1@DiegoS At the end of the line, it kills the carriage return character, or whatever the correct term is. Otherwise this is converted into
\par, I believe. – Faheem Mitha Sep 20 '22 at 13:11
You can do it without the fancyhdr package, as documented in the lastpage package:
\usepackage{lastpage}
\makeatletter
\renewcommand{\@evenfoot}{\hfil page \thepage{} of \pageref*{LastPage}\hfil}
\renewcommand{\@oddfoot}{\@evenfoot}
\makeatother
- 575
-
1
-
@rasul I didn't have to remove it, and it works for me, Also
$evenheadand$oddheadwork analogously too! – Steve Powell Feb 08 '23 at 17:09
In ConTeXt
\setuppagenumbering[location={footer,center}]
\setupfootertexts[Page \currentpage\ of \lastpage]
- 4,252
Here is a combination of the total counter and associated counter features from xassoccnt to achieve the total page count, even if the page numbers are reset or changed manually in between.
Finally, the output is done with fancyhdr, for example.
\documentclass{article}
\usepackage{blindtext}
\usepackage{pgffor}
\usepackage{fancyhdr}
\usepackage{xassoccnt}
\NewTotalDocumentCounter{totalpages}
\DeclareAssociatedCounters{page}{totalpages}
\fancypagestyle{plain}{%
\fancyfoot[C]{This is page \thepage\ of \TotalValue{totalpages} pages}
}
\begin{document}
\pagestyle{plain}
There are \TotalValue{totalpages} pages in this document! % Should report 51 here!
\clearpage
% Generating 50 pages
\foreach \x in {1,...,50} {%
\blindtext
\clearpage
}
\end{document}
Since version 1.2 of xassoccnt the package supports the \DeclareTotalAssociatedCounters macro:
\documentclass{article}
\usepackage{blindtext}
\usepackage{pgffor}
\usepackage{fancyhdr}
\usepackage{xassoccnt}
\DeclareTotalAssociatedCounters{page}{totalpages}
\fancypagestyle{plain}{%
\fancyfoot[C]{This is page \thepage\ of \TotalValue{totalpages} pages}
}
\begin{document}
\pagestyle{plain}
There are \TotalValue{totalpages} pages in this document! % Should report 51 here!
\clearpage
% Generating 50 pages
\foreach \x in {1,...,50} {%
\blindtext
\clearpage
}
\end{document}
-
You should really rewrite this to answer the question. That is, provide a solution of the form Page ## of ###. – Werner Mar 03 '17 at 06:21
-
In plain TeX, we can use the \lastpage register when opmac is loaded.
\input opmac
\openref
\footline={\hss \rm \thefontsize[10]\the\pageno/\the\lastpage \hss}
- 74,238
-
On Debian
opmacis hidden in the packagetexlive-lang-czechslovak. And it requires more than one TeX run. – ceving Feb 19 '20 at 15:08
Here is a similar answer as above. But simpler
\usepackage{lastpage}
...
You are on page \thepage\ of \pageref{LastPage}
- 101
-
2
-
-
I think my example would be easier for others to follow. Hence it is simpler. – Md Amiruzzaman Mar 07 '21 at 19:59

