3

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]{%
{\cftchapleader}\nobreak
\hb@xt@\@pnumwidth{\cftchappagefont #1\hfil}\cftchapafterpnum\par}

\makeatother

\begin{document}
\pagenumbering{Roman}

\tableofcontents
\newpage
\addcontentsline{toc}{chapter}{one}
1
\newpage
\addtocounter{page}{25}
\addcontentsline{toc}{chapter}{two}
2
\newpage
\addtocounter{page}{9}
\addcontentsline{toc}{chapter}{three}
3

\addtocontents{toc}{\string\LetLtxMacro{\string\cftchapfillnum}{\string\origcftchapfillnum}}

\newpage
\addtocounter{page}{9}
\addcontentsline{toc}{chapter}{four}
4

\newpage
\addtocounter{page}{1}
\addcontentsline{toc}{chapter}{five}
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?

1 Answer 1

6

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.

enter image description here

\documentclass{report}

\usepackage{tocloft}

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


\renewcommand{\cftchapfillnum}[1]{%
{\cftchapleader}\nobreak
\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
\addcontentsline{toc}{chapter}{one}
1
\newpage
\addtocounter{page}{25}
\addcontentsline{toc}{chapter}{two}
2
\newpage
\addtocounter{page}{9}
\addcontentsline{toc}{chapter}{three}
3

\newpage
\pagenumbering{arabic}


\addtocounter{page}{9}
\addcontentsline{toc}{chapter}{four}
4

\newpage
\addtocounter{page}{1}
\addcontentsline{toc}{chapter}{five}
5


\end{document}
1
  • 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
    Commented Jun 9, 2012 at 18:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .