# Large Page Numbers Using Roman Page Numbering Don't Align as Expected in TOC

Description of the Problem

When using Roman page numbering, large page numbers don't align as expected in the table of content (TOC). I would expect them to be right-aligned just like Arabic numbers.

Preview of the Problem

Quick Fix of the Problem Proposed by @alephzero

@alephzero proposed to add:

\renewcommand{\@pnumwidth}{<Required-Length-to-Fix-the-Problem>}


before the document begins - which indeed works. However, this fix requires to manually find the required length that fixes one's problem - which depends on the size of the longest Roman page number encountered in the document. On top of that, it shortens the dotted-line even for Arabic numbers when it is clearly not required.

Preview of the Quick Fix Proposed by @alephzero

Question

How can one fix this problem so Roman page numbers are automatically right-aligned for all possible range of numbers and that the dotted-line is automatically generated to fill the empty gap between the title and the page number?

Minimal Working Example (Without the Fix)

\documentclass{article}

\begin{document}

\tableofcontents

\pagenumbering{arabic}
\setcounter{page}{999}
\section{Page numbering using Arabic numbers}
\subsection{We can see that 999 is right-aligned as expected}

\newpage

\pagenumbering{Roman}
\setcounter{page}{999}
\section{Page numbering using Roman numbers}
\subsection{We can see that CMXCIX is not properly aligned}

\end{document}


Minimal Working Example (With the Fix)

\documentclass{article}

\makeatletter
\renewcommand{\@pnumwidth}{5em}
\makeatother

\begin{document}

\tableofcontents

\pagenumbering{arabic}
\setcounter{page}{999}
\section{Page numbering using Arabic numbers}
\subsection{We can see that 999 is right-aligned as expected}

\newpage

\pagenumbering{Roman}
\setcounter{page}{999}
\section{Page numbering using Roman numbers}
\subsection{We can see that CMXCIX is not properly aligned}

\end{document}


Accepted Solution of the Problem Proposed by @Werner (See Answer Below)

\documentclass{article}

\usepackage{tocloft,etoolbox}

\makeatletter

\patchcmd{\cftsecfillnum}
{\makebox[\@pnumwidth][\cftpnumalign]}{}{}{}

\patchcmd{\cftsubsecfillnum}
{\makebox[\@pnumwidth][\cftpnumalign]}{}{}{}

\patchcmd{\cftsubsubsecfillnum}
{\makebox[\@pnumwidth][\cftpnumalign]}{}{}{}

\patchcmd{\cftparafillnum}
{\makebox[\@pnumwidth][\cftpnumalign]}{}{}{}

\patchcmd{\cftsubparafillnum}
{\makebox[\@pnumwidth][\cftpnumalign]}{}{}{}

\makeatother

\begin{document}

\setcounter{secnumdepth}{5}
\setcounter{tocdepth}{5}
\tableofcontents

\newpage
\pagenumbering{arabic}
\setcounter{page}{555}
\section{Page numbering using Arabic numbers}
\newpage
\setcounter{page}{666}
\subsection{Sub-Section}
\newpage
\setcounter{page}{777}
\subsubsection{Sub-Sub-Section}
\newpage
\setcounter{page}{888}
\paragraph{Paragraph}
\newpage
\setcounter{page}{999}
\subparagraph{Sub-Paragraph}

\newpage
\pagenumbering{Roman}
\setcounter{page}{555}
\section{Page numbering using Roman numbers}
\newpage
\setcounter{page}{666}
\subsection{Sub-Section}
\newpage
\setcounter{page}{777}
\subsubsection{Sub-Sub-Section}
\newpage
\setcounter{page}{888}
\paragraph{Paragraph}
\newpage
\setcounter{page}{999}
\subparagraph{Sub-Paragraph}

\end{document}

• Welcome to TeX.SE. Well, this a reason why Roman numbers are so popular nowadays -- their pretty alignment ;-) – user31729 Oct 20 '17 at 21:25
• @ChristianHupfer - they ARE better than binary page numbers, to be sure. – Michael Palmer Oct 20 '17 at 21:35
• @MichaelPalmer: I prefer Babylonian Page numbers, however ;-) – user31729 Oct 20 '17 at 21:35
• @ChristianHupfer and MichaelPalmer: What about the Maya numeral system?! ;-) I understand and share your point of view. However, Roman numbers are still the norm in many cases such as for Appendices... which motivated me to ask this question to see if some people could come up with a "more automatic fix". – Olivier Gougeon Oct 20 '17 at 22:11
• I'd suggest using Greek numbering – and ending proofs with Ε.Ε.Δ. instead of Q.E.D. ;o) – Bernard Oct 20 '17 at 22:44

## 1 Answer

You would only need to change those ToC entries with leaders. In your case, we can patch the \subsection ToC entry's way in which the page number is set by removing the box it is placed in. This also removes any alignment that might be visible, setting it as-is in line with the leader:

\documentclass{article}

\usepackage{tocloft,etoolbox}

\makeatletter
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\cftsubsecfillnum}% <cmd>
{\makebox[\@pnumwidth][\cftpnumalign]}% <search>
{}% <replace>
{}{}% <success><failure>
\makeatother

\begin{document}

\tableofcontents

\pagenumbering{arabic}
\setcounter{page}{999}
\section{Page numbering using Arabic numbers}
\subsection{We can see that 999 is right-aligned as expected}

\newpage

\pagenumbering{Roman}
\setcounter{page}{999}
\section{Page numbering using Roman numbers}
\subsection{We can see that CMXCIX is properly aligned}

\end{document}


We use tocloft since it provides an identifiable structure for the ToC entries, and with something that is fairly easy (and understandable) to patch.

The patch (thanks to etoolbox takes the original structure that resembles

\newcommand{\cftsubsecfillnum}[1]{%
{\cftsubsecleader}\nobreak
\makebox[\@pnumwidth][\cftpnumalign]{\cftsubsecpagefont #1}\cftsubsecafterpnum\par
}


and changes it to

\newcommand{\cftsubsecfillnum}[1]{%
{\cftsubsecleader}\nobreak
{\cftsubsecpagefont #1}\cftsubsecafterpnum\par
}


so there's no boxing or alignment issue.

One can do the same for other entries in the ToC with leaders, if they exist (like \subsubsection or the like).

• This is exactly the kind of automatic fix that I was looking for! Thank you so much! For completeness sake, I would also add that the other patch commands are \cftsecfillnum, \cftsubsecfillnum, \cftsubsubsecfillnum, \cftparafillnum and \cftsubparafillnum for someone using respectively \section, \subsection, \subsubsection, \paragraph and \subparagraph with the article document class. – Olivier Gougeon Oct 21 '17 at 18:12