# How to left aligne the page numbers with the tocloft package

Using the tocloft package I would like to have the (Roman) page numbers in the table of contents to be aligned left (in the MWE chapter 1-3) instead of right (in the MWE chapter 4-5). (And later right aligned for the arabic page numbers.) Is there some easier way than redefining \cftchapfillnum?

The example is just about chapters, but in the not-minimal-document I also redefine it for part and section (and if somebody uses subsection,..., those redefinitions must be done, too.)

\documentclass{report}
\usepackage{letltxmacro}

\usepackage{tocloft}

\makeatletter
\renewcommand{\cftchapdotsep}{\cftdotsep}
\renewcommand{\@pnumwidth}{6em}
\renewcommand{\@tocrmarg}{8.55em}

\LetLtxMacro{\origcftchapfillnum}{\cftchapfillnum}
\renewcommand{\cftchapfillnum}[1]{%
\hb@xt@\@pnumwidth{\cftchappagefont #1\hfil}\cftchapafterpnum\par}

\makeatother

\begin{document}
\pagenumbering{Roman}

\tableofcontents
\newpage
1
\newpage
2
\newpage
3

\newpage
4

\newpage
5

\end{document}


The MWE really works (MWEs usually do not work - in spite of their name), but the question is whether there is some easier solution which I just did not find?

-

Your version looks OK to me, although an alternative that I'd probably use if I needed this myself is to drop the dependency on letltxmacro and to make the alignment automatic depending on the page number rather than needing to drop a redefinition into the toc file.

This looks at the first character of the expanded number and if it is (the same catcode as) a number then it is pushed flush right otherwise it is pushed flush left. I changed the MWE to use arabic numerals for the last two, to show the switch.

\documentclass{report}

\usepackage{tocloft}

\makeatletter
\renewcommand{\cftchapdotsep}{\cftdotsep}
\renewcommand{\@pnumwidth}{6em}
\renewcommand{\@tocrmarg}{8.55em}

\renewcommand{\cftchapfillnum}[1]{%
\hb@xt@\@pnumwidth{\cftchappagefont \leftorright#1\hfil}\cftchapafterpnum\par}

\def\leftorright#1{\ifcat 0#1\hfill\fi#1}

\makeatother

\begin{document}
\pagenumbering{Roman}

\tableofcontents
\newpage
1
\newpage
2
\newpage
3

\newpage
\pagenumbering{arabic}


OK, thus no option I missed to see. Thanks for improving the code! Actually, I choose \@pnumwidth not 6em but as wide as the widest "number", and together with \pagenumbering{arabic} further decreased that. (I may have higher page numbers, but nothing to fill 6em with an arabic number!) +1 and accepted. – Stephen Jun 9 '12 at 18:00