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.

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.

# 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)

I do not know if this can be done with package tocloft, so here is a suggestion using tocbasic. Note, that you can use only one package for TOC and the lists: either tocloft or tocbasic or titletoc etc.

\documentclass{extarticle}
\usepackage{lipsum}
\usepackage[nottoc]{tocbibind}

\usepackage{tocbasic}
\DeclareTOCStyleEntries[
rightindent=12em,
pagenumberbox=\pagenumberbox
]{tocline}{section,subsection,subsubsection,paragraph,subparagraph,figure,table}
\newcommand*\pagenumberbox[1]{\mbox{\hspace{1em}#1}}

\begin{document}
\tableofcontents
\cleardoublepage
\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
\cleardoublepage
\setcounter{page}{123456789}
\section{Several pages later}
\subsection{Something}
\lipsum
\cleardoublepage
\pagenumbering{Roman}
\section{Now we have Roman numbering}
\subsection{Something}
\lipsum
\clearpage
\setcounter{page}{123}
\section{Several pages later}
\subsection{Something}
\lipsum
\end{document}


• Trying to compile and run this I am getting issues with \DeclareTOCStyleEntries and that it is an undefined control sequence. Is this solution fine to use with pdflatex, as that's what I've been using. (using pdflatex --version gives pdfTeX 3.14159265-2.6-1.40.19 (TeX Live 2018)). – oliversm Mar 3 '20 at 17:53
• Based on this online thread does it suggest the command requires a LaTeX update? I'd be a bit surprised as I updated my version moderately recently (within memory). – oliversm Mar 3 '20 at 18:00
• Works a treat when tested on overleaf. I'll update my LaTeX distribution and accept the answer if everything works locally. – oliversm Mar 3 '20 at 18:03
• Which KOMA-Script version do you use? – esdd Mar 4 '20 at 9:48
• After updating my TeX Live to 2019 the proposed solution works. (And upon updating my KOMA-Script version is 3.29). – oliversm Mar 4 '20 at 11:48

If you are willing to have the page numbers left aligned instead of right aligned then in your MWE put

...
\cftsetrmarg{12em}
\renewcommand{\cftpnumalign}{l} % left aligned page numbers
\begin{document}

• An interesting idea, but the aesthetics of having the numbers hang off into the right margin is not to my taste, and also runs the risk of the numbers being chopped off of the page, which can be seen by setting the Roman page number to e.g. \setcounter{page}{12340}. – oliversm Mar 4 '20 at 11:54