> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/formatting/03-page-numbering.md).

# Page numbering

Document page numbers can be typeset using a particular *style*, such as using Arabic or Roman numerals, and typeset at a particular page *location*—usually within headers or footers. This article shows how the style and location of page numbers can be changed:

* the *style* of page numbers can be changed by the `\pagenumbering` command;
* the *location* of page numbers can be changed using the [`fancyhdr` package](https://ctan.org/pkg/fancyhdr?lang=en).

## Setting the style of page numbers

The style of page numbers can be changed using the command

```latex
\pagenumbering{⟨style⟩}
```

where `⟨style⟩` is one of

* `arabic`: use Arabic numerals (1, 2, 3, ...)
* `alph`: use lowercase letters (a, b, c, ...)
* `Alph`: use uppercase letters (A, B, C, ...)
* `roman`: use lowercase roman numerals (i, ii, iii, ...)
* `Roman`: use uppercase roman numerals (I, II, III, ...)

### Example to demonstrate page number styles

The following example typesets a table of contents followed by 5 pages, each of which demonstrates one of the `⟨style⟩` options for page numbers. Internally, LaTeX uses a so-called *counter* to record the current page number. A counter is the name of a LaTeX variable used to store an integer value—see the [Overleaf Counters article](/latex/formatting/10-counters.md) for more detail and examples.

```latex
\begin{document}
% Insert a table of contents
\tableofcontents
\newpage
\section{Uppercase Roman}
\pagenumbering{Roman}% Capital 'R': uppercase Roman numerals
\blindtext[1]
\newpage
\section{Lowercase Roman} % lowercase Roman numerals
\pagenumbering{roman}
\blindtext[1]
\newpage
\section{Arabic numbers}
\pagenumbering{arabic} % Arabic/Indic page numbers
\blindtext[1]
\newpage
\section{Lowercase alphabetic}
\pagenumbering{alph} % Lowercase alphabetic page "numbers"
\blindtext[1]
\newpage
\section{Uppercase alphabetic}
\pagenumbering{Alph} % Uppercase alphabetic page "numbers"
\blindtext[1]
\end{document}
```

[Open this example in Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Demonstrating+page+number+styles\&snip=%5Cdocumentclass%7Barticle%7D%0A%25+Use+a+small+page+size+to+avoid+%0A%25+needing+lots+of+text+to+create+%0A%25+several+pages%0A%5Cusepackage%5Bincludefoot%2C%0Apaperheight%3D10cm%2C%0Apaperwidth%3D10cm%2C%0Atextwidth%3D9cm%2C%0Atextheight%3D8cm%5D%7Bgeometry%7D%0A%5Cusepackage%7Bblindtext%7D%0A%5Cbegin%7Bdocument%7D%0A%25+Insert+a+table+of+contents%0A%5Ctableofcontents%0A%5Cnewpage%0A%5Csection%7BUppercase+Roman%7D%0A%5Cpagenumbering%7BRoman%7D%25+Capital+%27R%27%3A+uppercase+Roman+numerals%0A%5Cblindtext%5B1%5D%0A%5Cnewpage%0A%5Csection%7BLowercase+Roman%7D+%25+lowercase+Roman+numerals%0A%5Cpagenumbering%7Broman%7D%0A%5Cblindtext%5B1%5D%0A%5Cnewpage%0A%5Csection%7BArabic+numbers%7D%0A%5Cpagenumbering%7Barabic%7D+%25+Arabic%2FIndic+page+numbers%0A%5Cblindtext%5B1%5D%0A%5Cnewpage%0A%5Csection%7BLowercase+alphabetic%7D%0A%5Cpagenumbering%7Balph%7D+%25+Lowercase+alphabetic+page+%22numbers%22%0A%5Cblindtext%5B1%5D%0A%5Cnewpage%0A%5Csection%7BUppercase+alphabetic%7D%0A%5Cpagenumbering%7BAlph%7D+%25+Uppercase+alphabetic+page+%22numbers%22%0A%5Cblindtext%5B1%5D%0A%5Cend%7Bdocument%7D)

The next graphic shows the table of contents produced by this document. Note how the `\pagenumbering` command has reset the starting page number to the initial value for each style: `i` for the `roman` style, `A` for the `Alph` style, `I` for the `Roman` style and so forth:

![Example table of contents typeset in LaTeX](/files/yKRFTmHbIqKzA3MiuURa)

Here are two sample pages produced by this example; the first image shows page numbers as uppercase Roman numbers, the second demonstrates page numbers as uppercase letters:

![Example of page numbers typeset using Roman numerals](/files/KDRsu3v7VUtX2AzNAkUv)

![Example of page numbers typeset using Uppercase letters](/files/l1aKpFiIfpR0dFaQhCzY)

## Book class: using two styles of page number

The [first few pages of a book](https://en.wikipedia.org/wiki/Book_design#Structure), which include the copyright page, title or half-title page, any Foreword or Preface and table of contents, are collectively called the *preliminary* pages, or prelims for short—another term for those pages is [*front matter*](https://en.wikipedia.org/wiki/Book_design#Front_matter). Traditionally, prelim pages are numbered using lowercase Roman numerals with the main (body) pages being numbered using Arabic numerals. The `book` document class contains commands to assist with this, as the following example demonstrates.

```latex
\documentclass{book}
% The emptypage package prevents page numbers and
% headings from appearing on empty pages.
\usepackage{emptypage}
\begin{document}
\frontmatter %Use lowercase Roman numerals for page numbers
\chapter*{Foreword}
\addcontentsline{toc}{chapter}{Foreword}
The Foreword is written by someone who is not the book's author.

\chapter*{Preface}
\addcontentsline{toc}{chapter}{Preface}
The Preface is written by the book's author.

\tableofcontents

\mainmatter % Now Use Arabic numerals for page numbers

\chapter{First Chapter}
This will be an empty chapter...
\section{First section}
Some text would be good.
\chapter{The second chapter}
\end{document}
```

[Open this example in Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+page+numbers+in+book+class\&snip=%5Cdocumentclass%7Bbook%7D%0A%25+The+emptypage+package+prevents+page+numbers+and%0A%25+headings+from+appearing+on+empty+pages.%0A%5Cusepackage%7Bemptypage%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cfrontmatter+%25Use+lowercase+Roman+numerals+for+page+numbers%0A%5Cchapter%2A%7BForeword%7D%0A%5Caddcontentsline%7Btoc%7D%7Bchapter%7D%7BForeword%7D%0AThe+Foreword+is+written+by+someone+who+is+not+the+book%27s+author.%0A%0A%5Cchapter%2A%7BPreface%7D%0A%5Caddcontentsline%7Btoc%7D%7Bchapter%7D%7BPreface%7D%0AThe+Preface+is+written+by+the+book%27s+author.%0A%0A%5Ctableofcontents%0A%0A%5Cmainmatter+%25+Now+Use+Arabic+numerals+for+page+numbers%0A%0A%5Cchapter%7BFirst+Chapter%7D%0AThis+will+be+an+empty+chapter...%0A%5Csection%7BFirst+section%7D%0ASome+text+would+be+good.%0A%5Cchapter%7BThe+second+chapter%7D%0A%5Cend%7Bdocument%7D)

The table of contents produced by this example demonstrates the use of Roman numerals for the front matter and Arabic numerals for the main (body) matter:

![Showing Roman and Arabic page numbers in a book](/files/RfIeuUQb30IbnMYcQ09t)

The commands that control the page numbering are:

* `\frontmatter`: Pages after this command and before the command `\mainmatter`, will be numbered with lowercase Roman numerals.
* `\mainmatter`: This will restart the page counter and change the style to Arabic numbers.

## Article class: using two styles of page number

As noted above, the current page number is stored in a [LaTeX counter variable](/latex/formatting/10-counters.md#introduction-to-latex-counters) which is called `page`. The value stored in `page`, or any other counter variable, can be set to a specific value using the `\setcounter` command:

```latex
\setcounter{⟨countvar⟩}{⟨intval⟩}
```

where the counter variable `⟨countvar⟩` is set to the value `⟨intval⟩`. For example, to set the `page` counter to `3` you would write

```latex
\setcounter{page}{3}
```

Other commands to change counter variables include `\addtocounter` and `\stepcounter`:

```latex
\addtocounter{⟨countvar⟩}{⟨increment⟩}
\stepcounter{⟨countvar⟩}
```

* `\addtocounter{⟨countvar⟩}{⟨increment⟩}` adds an amount `⟨increment⟩` to the counter variable `⟨countvar⟩`. **Note**: `⟨increment⟩` can be positive, to increase the counter value, or negative to decrease it.
* `\steptocounter{⟨countvar⟩}` adds 1 to the counter variable `⟨countvar⟩`

This hypothetical example modifies the page counter to plan a table of contents, including sections that haven't been written and whose page count is estimated. It also uses `\pagenumbering{roman}` and `\pagenumbering{Arabic}` to set the stye of page numbers.

```latex
\documentclass{article}
\pagenumbering{roman}
\begin{document}

\section*{Guest Foreword (TBD)}
\addcontentsline{toc}{section}{Foreword: 4 pages (TBD)}
To be written: Allowing 4 pages.
\newpage
\setcounter{page}{5}

\section*{Introduction (2 pages)}
\addcontentsline{toc}{section}{Introduction: 2 pages (TBD)}
To be written: 2 pages allowed.
\newpage
\addtocounter{page}{1}

\section*{Strategy summary (2 pages)}
\addcontentsline{toc}{section}{Strategy summary: 2 pages (TBD)}
To be written: 2 pages.
\newpage
\stepcounter{page}

\tableofcontents

\newpage
\pagenumbering{arabic}

\section{Level 1 heading}
Some text on this page.
\newpage
\section{Another Level 1 heading}
\end{document}
```

[Open this example in Overleaf.](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Mixing+page+number+formats+in+the+article+class\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cpagenumbering%7Broman%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Csection%2A%7BGuest+Foreword+%28TBD%29%7D%0A%5Caddcontentsline%7Btoc%7D%7Bsection%7D%7BForeword%3A+4+pages+%28TBD%29%7D%0ATo+be+written%3A+Allowing+4+pages.%0A%5Cnewpage+%0A%5Csetcounter%7Bpage%7D%7B5%7D%0A%0A%5Csection%2A%7BIntroduction+%282+pages%29%7D%0A%5Caddcontentsline%7Btoc%7D%7Bsection%7D%7BIntroduction%3A+2+pages+%28TBD%29%7D%0ATo+be+written%3A+2+pages+allowed.%0A%5Cnewpage%0A%5Caddtocounter%7Bpage%7D%7B1%7D%0A%0A%5Csection%2A%7BStrategy+summary+%282+pages%29%7D%0A%5Caddcontentsline%7Btoc%7D%7Bsection%7D%7BStrategy+summary%3A+2+pages+%28TBD%29%7D%0ATo+be+written%3A+2+pages.%0A%5Cnewpage%0A%5Cstepcounter%7Bpage%7D%0A%0A%5Ctableofcontents%0A%0A%5Cnewpage%0A%5Cpagenumbering%7Barabic%7D%0A%0A%5Csection%7BLevel+1+heading%7D%0ASome+text+on+this+page.%0A%5Cnewpage%0A%5Csection%7BAnother+Level+1+heading%7D%0A%5Cend%7Bdocument%7D)

This example produces the following table of contents, showing the results of altering the value of the `page` counter:

![Changing the page counter in LaTeX](/files/dbYTzzIqzrzfmC2EXPNR)

## Customizing page numbers with the fancyhdr package

The `fancyhdr` package can be used to customize the location and format of page numbers; for example, placing them at specific positions within the header or footer. The Overleaf article [Headers and footers](/latex/formatting/02-headers-and-footers.md) explores this in detail, with many examples we won't reproduce here.

Here we'll give one example of writing the current page number in the context of the total page count, such as `Page X of Y` where Y is the total number of document pages obtained using the [`lastpage` package](https://ctan.org/pkg/lastpage?lang=en).

```latex
\documentclass{article}
% Use a small page size to avoid
% needing lots of text to create
% several pages
\usepackage[includefoot,
paperheight=10cm,
paperwidth=10cm,
textwidth=9cm,
textheight=8cm]{geometry}
\usepackage{blindtext}
\usepackage{lastpage}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} % clear existing header/footer entries
% Place Page X of Y on the right-hand
% side of the footer
\fancyfoot[R]{Page \thepage \hspace{1pt} of \pageref{LastPage}}
\begin{document}
% Insert a table of contents
\tableofcontents
\newpage
\section{One}
\blindtext[1]
\newpage
\section{Two}
\blindtext[1]
\newpage
\section{Three}
\blindtext[1]
\newpage
\section{Four}
\blindtext[1]
\newpage
\section{Five}
\blindtext[1]
\end{document}
```

[Open this example in Overleaf.](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Modifying+page+numbers+with+fancyhdr\&snip=%5Cdocumentclass%7Barticle%7D%0A%25+Use+a+small+page+size+to+avoid+%0A%25+needing+lots+of+text+to+create+%0A%25+several+pages%0A%5Cusepackage%5Bincludefoot%2C%0Apaperheight%3D10cm%2C%0Apaperwidth%3D10cm%2C%0Atextwidth%3D9cm%2C%0Atextheight%3D8cm%5D%7Bgeometry%7D%0A%5Cusepackage%7Bblindtext%7D%0A%5Cusepackage%7Blastpage%7D%0A%5Cusepackage%7Bfancyhdr%7D%0A%5Cpagestyle%7Bfancy%7D%0A%5Cfancyhf%7B%7D+%25+clear+existing+header%2Ffooter+entries%0A%25+Place+Page+X+of+Y+on+the+right-hand%0A%25+side+of+the+footer%0A%5Cfancyfoot%5BR%5D%7BPage+%5Cthepage+%5Chspace%7B1pt%7D+of+%5Cpageref%7BLastPage%7D%7D%0A%5Cbegin%7Bdocument%7D%0A%25+Insert+a+table+of+contents%0A%5Ctableofcontents%0A%5Cnewpage%0A%5Csection%7BOne%7D%0A%5Cblindtext%5B1%5D%0A%5Cnewpage%0A%5Csection%7BTwo%7D%0A%5Cblindtext%5B1%5D%0A%5Cnewpage%0A%5Csection%7BThree%7D%0A%5Cblindtext%5B1%5D%0A%5Cnewpage%0A%5Csection%7BFour%7D%0A%5Cblindtext%5B1%5D%0A%5Cnewpage%0A%5Csection%7BFive%7D%0A%5Cblindtext%5B1%5D%0A%5Cend%7Bdocument%7D)

The following image shows one of the pages produced by this example—note `Page 3 of 6` on the right-hand side of the footer:

![Changing page numbers with fancyhdr](/files/2R3m9AOFAzjV6BVVThTo)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/formatting/03-page-numbering.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
