## Define a new environment for different page numbering styles in LaTeX

For historical reasons1, the page numbering in the front matter (e.g. table of contents) uses a different numbering format than the rest of the document. Roman numerals are commonly used in the front matter (I, II, III, ...) and Arabic numerals (1, 2, 3, ...) for the rest of the document.

In $$\LaTeX$$ this is easily achieved by using the \pagenumbering command. An introduction to this command can be found at sharelatex.com. The use is quite straightforward and should work out of the box.

What I wanted was to define an environment which allows me to set the part of the document which has a different page numbering format than the rest of the document. I wanted to use it as follows:


\begin{Numbering}{Roman}{arabic}
\tableofcontents
\end{Numbering}



The first mandatory argument defines the page numbering style for the contents of this environment and the second mandatory argument defines the page numbering style for the rest of the document. Surprisingly, this turned out to be a little bit awkward. But for a start I am presenting you my solution:


\documentclass{scrbook}
\usepackage{blindtext}% Just to get some default text
\usepackage{hyperref}% To get the page numbering also in the pdf correct

\NewDocumentEnvironment{Numbering}{mm}%
{%
\pagenumbering{#1}
}%
{%
\cleardoublepage
\pagenumbering{#2}
}%

\begin{document}
\begin{Numbering}{Roman}{arabic}
\tableofcontents
\end{Numbering}

\Blinddocument

\end{document}



I used the xparse package which offers an advanced interface to create new commands and define environments (similar to e.g. the standard \newcommand macros). In the lines 6–14, I defined a new environment with the help of the \NewDocumentEnvironment{NAME}{ARGUMENTS}{START_CODE}{END_CODE} command. It differs from the normal \newenvironment in several ways (incomplete list):

• It specifies the arguments in a special syntax which essentially is a list describing each argument. In my case, the first and second arguments are mandatory (If you need 3 mandatory arguments you should write mmm).
• You can use your arguments in both the begin and the end block. As you can see in the code this is a necessary requirement here.
• It works.

Explanation to the last two points: If you want to use your arguments in the end block with \newenvironment, you have to apply a workaround, because normally arguments are only visible in the begin block2. So, the solution can be to introduce a new variable, store your arguments in the begin block in that variable and last but not least use your variable (with the stored argument) in the end block. Unfortunately, this did not work in my case. I received lots of errors when trying to do so.

So, hopefully, this helps some people facing the same problem.

EDIT: As the user Taiki pointed out, if you are only using the scrbook class, you can also use the following approach (to get the different page numbering styles):


\documentclass{scrbook}
\usepackage{blindtext}% Just to get some default text
\usepackage{hyperref}% To get the page numbering also in the pdf correct

\begin{document}
\frontmatter
\pagenumbering{Roman}
\tableofcontents
\mainmatter
\Blinddocument

\end{document}



1. Before the advent of the digital era, parts of the front matter such as the table of contents could only be written after the book was completed, because only then the page numbers of the main text were final. But, the exact number of pages for the front matter was not known before it was written and the author could not just guess that the main text starts at e.g. page 20. Any mistake would have meant to touch every single page again or have empty pages. Therefore, the main matter started at page 1 (Arabic) and the front matter at page I (Roman). This allowed the author to add as many pages to the front matter as he needed without having to touch the rest of the book or have superfluous empty pages.
2. See the question Why can't the end code of an environment contain an argument? for more information.