By default, in LaTeX pages are numbered using Arabic numbers, which are the usual numbers we mostly use. However, we can customize the page numbering of our document. For example, it is conventional to number the pages of a book before the first page of the main text (the preamble, table of contents, etc.) using lowercase roman numbers. Sometimes appendices are also numbered with their own system.
In this tutorial, we are going to explore some of the different options that we have inside LaTeX to change the page numbering system.
1. Customize page numbering style
The easiest way to do so is using the \pagenumbering command in the preamble of our document. This command lets us set a page numbering style, and the arguments that can be passed to it are the following:
arabic
to use arabic numerals (default option),roman
to use lowercase roman numerals,Roman
to use uppercase roman numerals,alph
to use lowercase letters andAlph
to use uppercase letters.
Check the following illustrative example that changes the page numbering style to Roman:
% Change page numbering to Roman style \documentclass{article} % Change numbering page style \pagenumbering{Roman} \begin{document} % Table of contents \tableofcontents % Pages \newpage \section{A section} \newpage \subsection{A subsection} \newpage \subsection{Another subsection} \newpage \section{Another section} \end{document}
produces a document whose table of contents is the one shown below; as you can see, the numbering of the pages, in this case, is done with roman numerals.
2. Different page numbering styles in the same document (Roman and Arabic)
The numbering system used in the previous example is rather unusual. But as was mentioned in the introduction, sometimes lowercase roman numerals are actually used in books, to number the pages before the first chapter (that is, before the main contents of the book). Since this kind of structure is very common, LaTeX has commands already implemented that let you easily produce it: inside the book class, when you use the \frontmatter
command, the roman numbering will start, until you use the \mainmatter
command when it will be substituted with the usual Arabic numbering.
Here is an example of how you could use these commands when producing a book:
% Frontmatter and mainmatter numbering styles \documentclass{book} % Generate dummy text \usepackage {lipsum} \begin{document} % Start Roman numbering style \frontmatter \addcontentsline{toc}{chapter}{ Copyright} \lipsum \addcontentsline{toc}{chapter}{ Abstract} \lipsum \addcontentsline{toc}{chapter}{ Acknowledgements} \lipsum \tableofcontents \newpage \addcontentsline{toc}{chapter}{Preamble} \lipsum % Start Arabic numbering style \mainmatter \chapter{First chapter} \section{A section} \newpage \subsection {A subsection} \newpage \subsection {Another subsection} \newpage \section{An other section} \end{document}
The table of contents of this book is shown in the following image. In the example, \lipsum simply generates dummy text, to fill the different sections.
Observe that the numbering in the table of contents is the one that is found in most edited and printed books.
3. More control over page numbering
One may want more fine-grained control over the page numbering, instead of just this predefined system. This can be accomplished simply using the \pagenumbering command inside the document. Once it is inserted, the following page will start with the new numbering system. However, the counter that determines the page number will not be reset, and therefore if you were at page 5 and the next one starts with the lower case roman numbering system, its number will be vi.
To prevent this behavior, we have the \setcounter command, which accepts two mandatory arguments:
- the first determines the counter whose value we want to change (in this case, page, which is the counter
that determines the page number) and - the second, the value to which we want to set it.
So for example, we can accomplish fancy changes of page numbering as in the following example:
% Customize page numbering \documentclass{article} % Generate dummy text \usepackage{lipsum} \begin{document} % Table of contents \tableofcontents \newpage % Uppercase page numbering \pagenumbering{Alph} \section{A section} \lipsum \subsection{A subsection} \lipsum \subsection{Another subsection} \lipsum % Roman page numbering \pagenumbering{Roman} % Set counter page number to 10 \setcounter{page}{10} \section{Another section} \end{document}
The table of contents produced in this case is shown below. Observe how the page numbering is affected when we use the \pagenumbering and \setcounter commands.
4. Add text to page numbering
We have seen how to change the numbering system used to counter the pages, and now we are going to explain how to customize the appearance of the number inside the page. To do so, we will be using the fancyhdr package, a package that provides several options to customize the heading and foot of our page.
For example, here is a very simple use of this package to print at the foot of our page the page number preceded by the word “Page”:
% Change footer page numbering style \documentclass{article} % Generate dummy text \usepackage{lipsum} % Change page numbering style \usepackage{fancyhdr} \pagestyle{fancy} \fancyhf{} \cfoot{Page \thepage} \begin{document} \lipsum \end{document}
Compiling this code yields the following output:
Now let’s dissect what have we done in this example.
- The package fancyhdr provides various default page layouts, and we can set them with
\pagestyle
. - Here we select the fancy style, and empty both the default foot and head passing no argument to \fancyhf.
- Then, we change how the centre of the foot looks with
\cfoot
.
Note the use of the macro \thepage
, which returns the page we are in.
- As you may guess, the analogous commands \rfoot and \lfoot are provided to customize the right and left footers, and also the commands \rhead, \chead, and \lhead let you customize the head of the document.
You can make the page number appear wherever you want, and also other elements of the page such as the chapter or section name, the name of the author, and more; you can explore the fancyhdr documentation for further details.
Finally, you can also opt to use no page number at all. For this purpose, you can use \pagestyle{empty}
in the preamble, and no elements will appear in your page layout.
We reached the end of this tutorial, If you have any remarks or suggestions, please feel free to reach us via email at admin@latex-tutorial.com