Spring til indhold

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.

Setting the style of page numbers

The style of page numbers can be changed using the command

\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 for more detail and examples.

\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

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

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


Example of page numbers typeset using Uppercase letters

Book class: using two styles of page number

The first few pages of a book, 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. 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.

\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

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

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 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:

\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

\setcounter{page}{3}

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

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

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

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

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

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

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

Overleaf guides

LaTeX Basics

Mathematics

Figures and tables

References and Citations

Languages

Document structure

Formatting

Fonts

Presentations

Commands

Field specific

Class files

Advanced TeX/LaTeX