My question is very similar to How to extend the dots in a table of content up to the page numbers?, but the answer doesn't work in my context.
The dots don't reach the numbers in the table of contents
I would like the dots in the table of contents to go as close to the numbers as possible, but I have a few pages where the numbers can be quite big, and I don't want the dots to overlap with the numbers. Some of the back matter uses Roman numbering, and as I want this flush right, I am using the tocloft
package as this answer on latex.org recommends. This involves setting values for \cftsetpnumwidth{}
and \cftsetrmarg{}
, which in turn set values for \@pnumwidth
, in a similar fashion to this solution to Right justify roman page numbers in list of tables. Doing this so it is suitable for very large numbers (not unreasonable in real life if using Roman numbers, but exaggerated here) gives an ugly appearance.
With nothing
Slightly better but still falling short
Using
\cftsetpnumwidth{5em}
\cftsetrmarg{12em}
Ideally the dots would fill close to the numbers (perhaps stopping no further than about 1em
before seems reasonable), but never go into the numbers.
MWE
\documentclass{extarticle}
\usepackage{lipsum}
\usepackage[nottoc]{tocbibind} % My real document uses features of this, so I would prefer if this can be kept.
\usepackage{tocloft}
\cftsetpnumwidth{5em}
\cftsetrmarg{12em}
\begin{document}
\tableofcontents
\clearpage
\pagenumbering{arabic}
\section{Arabic numbering starts}
\subsection{Some possibly very long title section that might just decide to span several lines in the table of contents. Why would someone write this, Who knows?}
\lipsum
\clearpage
\setcounter{page}{123456789}
\section{Several pages later}
\subsection{Something}
\lipsum
\clearpage
\pagenumbering{Roman}
\section{Now we have Roman numbering}
\subsection{Something}
\lipsum
\clearpage
\setcounter{page}{123}
\section{Several pages later}
\subsection{Something}
\lipsum
\end{document}
Bonus requirements
Ideally, I would like any solution to also work with the other following tables (in order of importance) in order to maintain consistency in the front matter:
- List of figures
- List of tables
- List of listings (using
listings
) - List of algorithms (using
algorithm2e
)
And maybe also
- List of todos (using
todonotes
)