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
\usepackage{xparse}% Defines the \NewDocumentEnvironment command (see https://www.ctan.org/pkg/xparse for more information)
\NewDocumentEnvironment{Numbering}{mm}%
{%
\cleardoublepage% Necessary before changing the page number (see http://golatex.de/abwechselnd-seitenausrichtung-rechts-links-mit-scrartcl-t12571.html for more information)
\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}