Menu

HowTo_TocRomanPageNumbers

How to align Roman page numbers neatly one below the other in the table of contents

When Roman numerals are added to a large number of pages, they soon become very long. However, since only a certain amount of space is reserved for the page numbers in the table of contents, the Roman page numbers then partially protrude into the margin.

The usual way to solve this problem is to reserve more space for page numbers in the table of contents. This could be done for example with

\makeatletter
\renewcommand*{\@pnumwidth}{4em}
\renewcommand*{\@tocrmarg}{5em}
\makeatother

in the preamble. (For a change depending on the entry level, see the pagenumberwidth and rightindent settings for the tocline entry style of the \DeclareTOCStyleEntry statement in the KOMA-Script manual).

However, this usual procedure has the disadvantage that this space is then also reserved for Arabic page numbers, which usually results in a very large space between the dotted line and the page numbers.

An alternative way is therefore to reserve an individually different space for the page numbers for each numbering style. To make this possible, one must write the desired changes of \@pnumwidth and \@tocrmarg into the table of contents file—file extension toc. If the switching of the numbering type is attached to the \frontmatter, \mainmatter and \backmatter statements, as is common when using a book class, it is obvious to attach the redefinition to them as well:

\documentclass{scrbook}
\usepackage[english]{babel}
\usepackage{blindtext}

\makeatletter
\g@addto@macro\frontmatter{%
  \addtocontents{\ext@toc}{%
    \protect\renewcommand*\protect\@pnumwidth{3em}
    \protect\renewcommand*\protect\@tocrmarg{4em}
  }%
}
\g@addto@macro\mainmatter{%
  \addtocontents{\ext@toc}{%
    \protect\renewcommand*\protect\@pnumwidth{1.55em}
    \protect\renewcommand*\protect\@tocrmarg{2.55em}
  }%
}
\g@addto@macro\backmatter{%
  \addtocontents{\ext@toc}{%
    \protect\renewcommand*\protect\@pnumwidth{4.25em}
    \protect\renewcommand*\protect\@tocrmarg{5.25em}
  }%
  \pagenumbering{Roman}%
  \setcounter{section}{0}%
}
\makeatother

\begin{document}
\frontmatter
\tableofcontents
\blinddocument\blinddocument\blinddocument
\blinddocument\blinddocument\blinddocument
\blinddocument\blinddocument

\mainmatter
\blinddocument\blinddocument\blinddocument
\blinddocument\blinddocument\blinddocument
\blinddocument\blinddocument

\backmatter
\blinddocument\blinddocument\blinddocument
\blinddocument\blinddocument\blinddocument
\blinddocument\blinddocument\blinddocument
\blinddocument
\end{document}

The example itself is of course absolute nonsense. Moreover, one should not try to insert numbered sections in the end part (\backmatter). But nonsensical requirements are often brought to my attention. Besides, I consider it rather useless anyway and a mothballed endeavor to switch the numbering style of page numbers several times in a document.


Related

Wiki (English): HowTo_ToCConfiguration

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.