How-toNovember 2, 20265 min read

How to Show Page X of Y in LaTeX

Show Page X of Y in a LaTeX footer with the fancyhdr and lastpage packages, including the two-pass compile, with a compile-tested example.

latex page x of ylatex lastpagefancyhdr footer page number
On this page
  1. The two packages
  2. The footer command
  3. Why you compile twice
  4. A complete example you can compile
  5. FAQ
  6. Further reading

Load fancyhdr and lastpage, then set the footer with \cfoot{Page \thepage\ of \pageref{LastPage}}. \thepage is the current page and \pageref{LastPage} is the total. The total needs two compiler runs, because LaTeX learns the last page number only after the first pass.

The two packages

fancyhdr gives you control of headers and footers. lastpage defines the label LastPage on the final page of the document, which lets \pageref{LastPage} print the total number of pages.

Clear the default fields with \fancyhf{}, then put the text in the centre footer with \cfoot{Page \thepage\ of \pageref{LastPage}}. Use \lfoot or \rfoot for the left or right footer instead. The backslash-space after \thepage keeps a space before the word "of".

Why you compile twice

The total comes from the auxiliary file written by the previous run. On a first compile the total can be missing or show as question marks, and after a second run it is correct. Editors and online compilers normally rerun for you.

A complete example you can compile

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{lastpage}
\pagestyle{fancy}
\fancyhf{}
\cfoot{Page \thepage\ of \pageref{LastPage}}
\renewcommand{\headrulewidth}{0pt}
\begin{document}

This is the first page. The footer shows the current page and the total.

\newpage

This is the second page.

\end{document}

We compiled this file as-is with Tectonic 0.17.0 on November 2, 2026, and inspected both rendered pages. The footer reads "Page 1 of 2" on the first page and "Page 2 of 2" on the second. We did not compile it with other engines or package versions.

FAQ

How do I add page numbers like Page 1 of 5 in LaTeX?

Use fancyhdr for the footer and lastpage for the total: \cfoot{Page \thepage\ of \pageref{LastPage}}. The example above was compile-tested with Tectonic 0.17.0 and printed Page 1 of 2 and Page 2 of 2.

Why does my total show ?? or the wrong number?

\pageref{LastPage} reads the page count saved by the previous run. Compile again, and the total updates. Tectonic reruns automatically; with other tools, check that they run LaTeX enough times.

What does \thepage do?

It prints the current page number using the current numbering style, so it works with Arabic or Roman numbering.

How do I remove the header line that fancyhdr adds?

Set \renewcommand{\headrulewidth}{0pt}, as the example above does. Use \fancyhf{} first to clear the default header and footer fields.

Does this count unnumbered or front-matter pages the same way?

LastPage refers to the final physical page, so a title page or front matter counts toward Y. How that interacts with restarted page numbering was not tested in the example here.

Further reading

Companion guides in this series cover bold and italic text, commenting out blocks, two figures side by side, and enumerate list numbering. More how-tos are on the Bibby AI blog. Source question: How can I add "page # of ##" on my document? on TeX Stack Exchange.

Write your next paper in Bibby AI. LaTeX-native editing in the browser, free to start. Compare plans on the pricing page.

Last updated: November 2, 2026. By the Bibby AI editorial team.

Try a LaTeX Editor Built for Researchers

AI-powered writing, smart citations, no compile timeouts. Join 12,500+ researchers from academia and industry using Bibby AI.

Start Writing Free

Want to discuss this topic or need help with your paper?

Book a Free Call