275

I want to add a footer to my document displaying the current number out of total number of pages. How do I do that?

1

15 Answers 15

231

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

16
  • 14
    +1 This is the method recommended within The LaTeX Compantion.
    – David Z
    Commented Jul 27, 2010 at 0:40
  • 12
    @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
    Commented May 9, 2011 at 19:09
  • 17
    Note that \usepackage{hyperref} is incompatible with this.
    – jontejj
    Commented Jul 27, 2013 at 13:41
  • 10
    One more useful note: to make the number black, use \pageref*{LastPage}
    – Ben Usman
    Commented Feb 10, 2017 at 20:00
  • 7
    @jontejj Could you elaborate? Why do you think this is incompatible with the hyperref package? The other answers suggest otherwise and recommend that you use \pageref*{LastPage} instead if you want to remove the link.
    – caw
    Commented Nov 15, 2017 at 21:31
96

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

3
  • 7
    This is an excellent answer and summarizes all the issues very well.
    – yannisl
    Commented Oct 24, 2011 at 8:42
  • I use the totpages package you reference. The total # of pages is invoked with \ref{TotPages}. Commented Oct 19, 2015 at 10:05
  • Note that pageslts doesn't work with current LaTeX.
    – cfr
    Commented May 8 at 14:44
30

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

4
  • So with memoir one normally doesn't need lastpage or pageslts, correct? Commented Feb 24, 2013 at 12:18
  • 1
    @LoverofStructure : That's correct. Commented Feb 24, 2013 at 12:21
  • 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
    Commented Dec 22, 2015 at 16:28
  • @Brent Longborough: MWE would be nice.
    – gizeh
    Commented Aug 7, 2020 at 11:39
26

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}
1
  • 12
    To suppres hyperlink to last page in lastpage one can use \pageref*{LastPage}.
    – Adobe
    Commented Feb 23, 2013 at 11:04
22

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}
3
  • 3
    +1 for avoiding loading yet another package. Nice if you want to keep it simple. Commented Jul 31, 2017 at 12:00
  • 1
    Very nice, and simple, and NO EXTRA PACKAGE
    – plasmo
    Commented Aug 4, 2021 at 8:20
  • This is good additional information, but it does not provide the total number of pages for the whole document.
    – Stephen
    Commented Sep 3, 2021 at 10:44
18

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

0
13

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}
13

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.

5
  • +1 for solving first page 1 of x inestead of 1.
    – Emadpres
    Commented Jan 10, 2017 at 10:31
  • +1 thank you very much indeed, can you also show how to do this with the KOMA articles (they are clashing with fancyhdr)? Commented Feb 26, 2018 at 6: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. Commented Feb 26, 2018 at 6:48
  • what is % used for?
    – Diego S
    Commented Sep 19, 2022 at 19:06
  • 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. Commented Sep 20, 2022 at 13:11
8

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
2
  • 1
    I had to remove the * in \pageref*{LastPage} to make it work.
    – rasul
    Commented Sep 8, 2018 at 1:33
  • @rasul I didn't have to remove it, and it works for me, Also $evenhead and $oddhead work analogously too! Commented Feb 8, 2023 at 17:09
6

In ConTeXt

\setuppagenumbering[location={footer,center}]
\setupfootertexts[Page \currentpage\ of \lastpage]
5

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}

enter image description here

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}
2
  • You should really rewrite this to answer the question. That is, provide a solution of the form Page ## of ###.
    – Werner
    Commented Mar 3, 2017 at 6:21
  • @Werner: Thanks, I 'ignored' the Page ## of ...### for unknown reasons
    – user31729
    Commented Mar 3, 2017 at 6:27
3

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}
1
  • On Debian opmac is hidden in the package texlive-lang-czechslovak. And it requires more than one TeX run.
    – ceving
    Commented Feb 19, 2020 at 15:08
1

This is an attempt to sketch a method for replacing the functionality of pageslts, which is not compatible with current LaTeX. It does not require any packages. The only packages used are for demonstration purposes only.

The setup assumes the use of book or a similar class, with the use of \frontmatter, \mainmatter and \backmatter. For purposes of illustration, we change the page numbering scheme for \backmatter, which is not default. (I'm not recommending this - I just wanted two switches of scheme for the example.)

For purposes of demonstration, fancyhdr is loaded so we can say

\fancyhf[rf]{Page ~ number ~ \thepage{} ~ of ~ \locallastpage }
\fancyhf[lf]{Page ~ \abspage {} ~  of ~ \abspageref {nopages:LastAbsPage} }

\locallastpage produces the last number of pages using the current numbering scheme. In the front matter, this is the last page before \mainmatter, in the main matter, the last before \backmatter and in the back matter, the final page. The number returned is the number in the local scheme.

\abspage returns the absolute number of the current page. \abspageref{<label>} returns the absolute page number of the page labelled <label>. nopages:LastAbsPage provides information for the final page of the document. These are labels in the new properties framework. To use a regular \ref or \pageref, we also create a regular nopages:LastPage on the final page of the document.

I'm going to omit the image because I don't currently have an idea of how to produce one for multiple pages. (Okular has a bug on X. I can sort-of workaround it for simple cases, but 'simple' means one page. I could maybe produce several images ... well, if anybody's interested, maybe I will.)

Code:

\documentclass{book}
% ateb: https://tex.stackexchange.com/a/717530/
\ExplSyntaxOn
\makeatletter
\hook_new:n { nopages / page ~ series }
\int_new:N \g__nopages_pageseries_int
\int_new:N \l__nopages_tmpa_int
\property_new:nnnn { nopages / pageseries } { shipout } { 0 } { \int_use:N \g__nopages_pageseries_int }
\tl_new:N \g__nopages_phantomanchor_tl
\tl_new:N \l__nopages_tmpa_tl
\cs_generate_variant:Nn \property_record:nn { en }
\tl_gset:Nn \g__nopages_phantomanchor_tl 
{
  \cs_if_free:NF \@currentHpage
  {% ateb Ulrike Fischer: https://tex.stackexchange.com/a/715126/
    \gdef\@currentHref{\@currentHpage}
  }
}
\hook_gput_code:nnn { shipout / lastpage } { . }
{
  \g__nopages_phantomanchor_tl
  \label{nopages:LastPage}
  \hook_use:n { nopages / page ~ series }
  \property_record:nn {nopages:LastAbsPage} { abspage, page, pagenum, nopages / pageseries }
}
\hook_gput_code:nnn { shipout / before } { . }
{
  \property_record:en { nopages: AbsPage \int_to_arabic:n { \g_shipout_readonly_int } } { abspage, page, pagenum, nopages / pageseries }
  \tl_set:Nf \l__nopages_tmpa_tl
  {
    \exp_args:Nce \gdef { nopages@LastLocalPage \int_to_roman:n { \g__nopages_pageseries_int } } { \thepage }
  }
  \iow_now:NV \@mainaux \l__nopages_tmpa_tl
}
\clist_map_inline:nn { frontmatter, mainmatter, backmatter }
{
  \hook_gput_code:nnn { cmd/#1/after } { . }
  {
    \int_gincr:N \g__nopages_pageseries_int
  }
}
\NewDocumentCommand \abspage { m }
{
  \int_set_eq:NN \l__nopages_tmpa_int \g_shipout_readonly_int
  \int_incr:N \l__nopages_tmpa_int
  \int_use:N \l__nopages_tmpa_int
}
\NewDocumentCommand \abspageref { m }
{
  \property_ref:nn { #1 } { abspage }
}
\cs_new_protected_nopar:Npn \locallastpage 
{
  \cs_if_free:cTF {  nopages@LastLocalPage \int_to_roman:n { \g__nopages_pageseries_int } }
  {
    \property_ref_undefined_warn:
  } {
    \use:c {  nopages@LastLocalPage \int_to_roman:n { \g__nopages_pageseries_int } }
  }
}
\makeatother
\ExplSyntaxOff
\usepackage{fancyhdr}
\usepackage{kantlipsum}
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\footrulewidth}{0pt}
\renewcommand{\headrulewidth}{0pt}
\ExplSyntaxOn
\fancyhf[rf]{Page ~ number ~ \thepage{} ~ of ~ \locallastpage }
\fancyhf[lf]{Page ~ \abspage {} ~  of ~ \abspageref {nopages:LastAbsPage} }
\ExplSyntaxOff
\begin{document}
\frontmatter
\textbf{FRONT}
\kant[1]
\mainmatter
\textbf{MAIN}
\kant[2-7]
\backmatter
\pagenumbering{Roman}
\textbf{BACK}
\kant[8-10]

\end{document}

excessive aux:

\relax
\gdef \nopages@LastLocalPagei {i}
\gdef \nopages@LastLocalPagei {ii}
\new@label@record{nopages:AbsPage0}{{abspage}{2}{page}{ii}{pagenum}{2}{nopages/pageseries}{1}}
\gdef \nopages@LastLocalPageii {1}
\new@label@record{nopages:AbsPage1}{{abspage}{3}{page}{1}{pagenum}{1}{nopages/pageseries}{2}}
\gdef \nopages@LastLocalPageii {2}
\new@label@record{nopages:AbsPage2}{{abspage}{4}{page}{2}{pagenum}{2}{nopages/pageseries}{2}}
\gdef \nopages@LastLocalPageiii {I}
\new@label@record{nopages:AbsPage3}{{abspage}{5}{page}{I}{pagenum}{1}{nopages/pageseries}{3}}
\newlabel{nopages:LastPage}{{}{I}{}{}{}}
\new@label@record{nopages:LastAbsPage}{{abspage}{5}{page}{I}{pagenum}{1}{nopages/pageseries}{3}}
\gdef \@abspage@last{5}

Note that this is almost certainly a very bad way to do this.

0

Another option:

\@abspage@last (no package needed)

Internal macro of LaTeX. (But maintainers will never change it for backwards compatibility reason)

source2e

The other options are all documented in the answer above.)

-1

Here is a similar answer as above. But simpler

\usepackage{lastpage} ...
You are on page \thepage\ of \pageref{LastPage}

3
  • 3
    How is this simpler than the identical answer above?
    – campa
    Commented Mar 7, 2021 at 19:42
  • In this example, only the package and how to use it are shown. Commented Mar 7, 2021 at 19:58
  • I think my example would be easier for others to follow. Hence it is simpler. Commented Mar 7, 2021 at 19:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .