1

Based on a previous question Extending table of contents dots up to large page numbers I have been using tocbasic and have some Roman pages. In all the list of somethings the Roman numbers are right aligned, except for the:

  • List of listings.
  • List of algorithms.
  • List of todos (not so worried about this one).

How can I get these right aligned?

enter image description here

enter image description here

\documentclass{book}
\usepackage[algosection]{algorithm2e} % For writing nice algorithms. 
\usepackage{etoolbox}
\usepackage{float}  %% The ordering matters here. 
\usepackage{listings} %% The ordering matters here. 
\usepackage{lipsum}
\usepackage{tocbasic} % For better TOC alignment
\usepackage[nottoc]{tocbibind} 
\usepackage{todonotes}
\usepackage[margin=10pt,font=small,labelfont=bf,labelsep=endash,figurewithin=section,tablewithin=section]{caption} % Caption figures and tables nicely. %% After cleveref.

% List of Algorithms (NB, requires french spelling of 'algorithmes')
\renewcommand*{\listalgorithmcfname}{List of algorithms}
\newcommand{\listofalgorithmes}{\tocfile{\listalgorithmcfname}{loa}}

% We want the list of listings to look like all the others. 
\let\Chapter\chapter
\def\chapter{\addtocontents{lol}{\protect\addvspace{10pt}}\Chapter}
\makeatletter
\patchcmd{\@chapter}{\chaptermark{#1}}{%
    \chaptermark{#1}%
    \addtocontents{lol}{\protect\addvspace{10\p@}}%
}{\typeout{Chapters patched for list-of-listings.}}{\typeout{Could not patch chapters for list-of-listings.}}
\makeatother

% So Roman page numbers are right aligned. 
\DeclareTOCStyleEntries[
  rightindent=10em,
  pagenumberbox=\pagenumberbox
]{tocline}{section,subsection,subsubsection,paragraph,subparagraph,figure,table}
\newcommand*\pagenumberbox[1]{\mbox{\hspace{1em}#1}}


\DeclareTOCStyleEntry[numwidth=3.5em]{tocline}{figure}% for figure entries
\DeclareTOCStyleEntry[numwidth=3.5em]{tocline}{table}
%\DeclareTOCStyleEntry[numwidth=3.5em]{tocline}{listing}% <- Doesn't work
%\DeclareTOCStyleEntry[numwidth=3.5em]{tocline}{algorithm}% <- Doesn't work

\AtBeginDocument{\counterwithin{lstlisting}{section}} % Ensures these are numbered enough

\setcounter{secnumdepth}{3}

\begin{document}
\tableofcontents
\listoffigures
\listoftables
\lstlistoflistings
\listofalgorithmes
\listoftodos   
\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
\chapter{title}
\section{Some figures}
\subsection{title}
\subsubsection{title}
\begin{figure}[htb]\caption{something}\end{figure}
\begin{figure}[htb]\caption{something}\end{figure}
\begin{figure}[htb]\caption{something}\end{figure}
\begin{figure}[htb]\caption{something}\end{figure}
\begin{figure}[htb]\caption{something}\end{figure}
\chapter{Chapter}
\section{Section}\section{Section}\section{Section}
\begin{table}[htb]\caption{A table}\end{table}
\begin{figure}[htb]\caption{A figure}\end{figure}
\begin{lstlisting}[caption={Some code}]
\end{lstlisting}
\todo[inline=true,caption={A todo note}]{Must do this.}
\begin{algorithm}[htb] \DontPrintSemicolon \KwIn{Input} \KwOut{Output} Code\; \caption{Some algorithm.} \end{algorithm}
\begin{table}[htb]\caption{A table}\end{table}
\begin{figure}[htb]\caption{A figure}\end{figure}
\begin{lstlisting}[caption={Some code}]
\end{lstlisting}
\todo[inline=true,caption={A todo note}]{Must do this.}
\begin{algorithm}[htb] \DontPrintSemicolon \KwIn{Input} \KwOut{Output} Code\; \caption{Some algorithm.} \end{algorithm}
\begin{table}[htb]\caption{A table}\end{table}
\begin{figure}[htb]\caption{A figure}\end{figure}
\begin{lstlisting}[caption={Some code}]
\end{lstlisting}
\todo[inline=true,caption={A todo note}]{Must do this.}
\begin{algorithm}[htb] \DontPrintSemicolon \KwIn{Input} \KwOut{Output} Code\; \caption{Some algorithm.} \end{algorithm}
\chapter{Chapter}
\section{Section}\section{Section}\section{Section}
\begin{table}[htb]\caption{A table}\end{table}
\begin{figure}[htb]\caption{A figure}\end{figure}
\begin{lstlisting}[caption={Some code}]
\end{lstlisting}
\todo[inline=true,caption={A todo note}]{Must do this.}
\begin{algorithm}[htb] \DontPrintSemicolon \KwIn{Input} \KwOut{Output} Code\; \caption{Some algorithm.} \end{algorithm}
\begin{table}[htb]\caption{A table}\end{table}
\begin{figure}[htb]\caption{A figure}\end{figure}
\begin{lstlisting}[caption={Some code}]
\end{lstlisting}
\todo[inline=true,caption={A todo note}]{Must do this.}
\begin{algorithm}[htb] \DontPrintSemicolon \KwIn{Input} \KwOut{Output} Code\; \caption{Some algorithm.} \end{algorithm}
\begin{table}[htb]\caption{A table}\end{table}
\begin{figure}[htb]\caption{A figure}\end{figure}
\begin{lstlisting}[caption={Some code}]
\end{lstlisting}
\todo[inline=true,caption={A todo note}]{Must do this.}
\begin{algorithm}[htb] \DontPrintSemicolon \KwIn{Input} \KwOut{Output} Code\; \caption{Some algorithm.} \end{algorithm}
\end{document}

1 Answer 1

1

To initialize the predefined values level, indent and numwidth for figure use:

\DeclareTOCStyleEntry[dynnumwidth]{tocline}{figure}

dynnumwidth enlarges the space for the entry number automatically, if the entry number is too long. But it needs an additional run.

Then you can copy (needs at least KOMA-Script version 3.28) this values for the other entry types:

\DeclareTOCStyleEntries[
  level:=figure,
  indent:=figure,
  numwidth:=figure,
  dynnumwidth
]{tocline}{table,lstlisting,algocf,todo}

and finally add lstlisting,algocf,todo to the following code snippet:

\DeclareTOCStyleEntries[
  rightindent=10em,
  pagenumberbox=\pagenumberbox
]{tocline}{section,subsection,subsubsection,paragraph,subparagraph,figure,table,lstlisting,algocf,todo}

Example:

\documentclass{book}
\usepackage[algosection]{algorithm2e} % For writing nice algorithms. 
\usepackage{etoolbox}
\usepackage{float}  %% The ordering matters here. 
\usepackage{listings} %% The ordering matters here. 
\usepackage{lipsum}
\usepackage{tocbasic}[2019/12/23] % For better TOC alignment
\usepackage[nottoc]{tocbibind} 
\usepackage{todonotes}
\usepackage[margin=10pt,font=small,labelfont=bf,labelsep=endash,figurewithin=section,tablewithin=section]{caption} % Caption figures and tables nicely. %% After cleveref.

% List of Algorithms (NB, requires french spelling of 'algorithmes')
\renewcommand*{\listalgorithmcfname}{List of algorithms}
\newcommand{\listofalgorithmes}{\tocfile{\listalgorithmcfname}{loa}}

% We want the list of listings to look like all the others. 
\let\Chapter\chapter
\def\chapter{\addtocontents{lol}{\protect\addvspace{10pt}}\Chapter}
\makeatletter
\patchcmd{\@chapter}{\chaptermark{#1}}{%
    \chaptermark{#1}%
    \addtocontents{lol}{\protect\addvspace{10\p@}}%
}{\typeout{Chapters patched for list-of-listings.}}{\typeout{Could not patch chapters for list-of-listings.}}
\makeatother

% So Roman page numbers are right aligned.
\DeclareTOCStyleEntry{tocline}{figure}
\DeclareTOCStyleEntries[
  level:=figure,
  indent:=figure,
  numwidth:=figure,
  dynnumwidth% enlarges numwidth automatically if the entry number is too long; needs an additional run
]{tocline}{table,lstlisting,algocf,todo}
\DeclareTOCStyleEntries[
  rightindent=10em,
  pagenumberbox=\pagenumberbox
]{tocline}{section,subsection,subsubsection,paragraph,subparagraph,figure,table,lstlisting,algocf,todo}
\newcommand*\pagenumberbox[1]{\mbox{\hspace{1em}#1}}

\AtBeginDocument{\counterwithin{lstlisting}{section}} % Ensures these are numbered enough
\setcounter{secnumdepth}{3}

\begin{document}
\tableofcontents
\listoffigures
\listoftables
\lstlistoflistings
\listofalgorithmes
\listoftodos
\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
\chapter{title}
\section{Some figures}
\subsection{title}
\subsubsection{title}
\begin{figure}[htb]\caption{something}\end{figure}
\begin{figure}[htb]\caption{something}\end{figure}
\begin{figure}[htb]\caption{something}\end{figure}
\begin{figure}[htb]\caption{something}\end{figure}
\begin{figure}[htb]\caption{something}\end{figure}
\chapter{Chapter}
\section{Section}\section{Section}\section{Section}
\begin{table}[htb]\caption{A table}\end{table}
\begin{figure}[htb]\caption{A figure}\end{figure}
\begin{lstlisting}[caption={Some code}]
\end{lstlisting}
\todo[inline=true,caption={A todo note}]{Must do this.}
\begin{algorithm}[htb] \DontPrintSemicolon \KwIn{Input} \KwOut{Output} Code\; \caption{Some algorithm.} \end{algorithm}
\begin{table}[htb]\caption{A table}\end{table}
\begin{figure}[htb]\caption{A figure}\end{figure}
\begin{lstlisting}[caption={Some code}]
\end{lstlisting}
\todo[inline=true,caption={A todo note}]{Must do this.}
\begin{algorithm}[htb] \DontPrintSemicolon \KwIn{Input} \KwOut{Output} Code\; \caption{Some algorithm.} \end{algorithm}
\begin{table}[htb]\caption{A table}\end{table}
\begin{figure}[htb]\caption{A figure}\end{figure}
\begin{lstlisting}[caption={Some code}]
\end{lstlisting}
\todo[inline=true,caption={A todo note}]{Must do this.}
\begin{algorithm}[htb] \DontPrintSemicolon \KwIn{Input} \KwOut{Output} Code\; \caption{Some algorithm.} \end{algorithm}
\chapter{Chapter}
\section{Section}\section{Section}\section{Section}
\begin{table}[htb]\caption{A table}\end{table}
\begin{figure}[htb]\caption{A figure}\end{figure}
\begin{lstlisting}[caption={Some code}]
\end{lstlisting}
\todo[inline=true,caption={A todo note}]{Must do this.}
\begin{algorithm}[htb] \DontPrintSemicolon \KwIn{Input} \KwOut{Output} Code\; \caption{Some algorithm.} \end{algorithm}
\begin{table}[htb]\caption{A table}\end{table}
\begin{figure}[htb]\caption{A figure}\end{figure}
\begin{lstlisting}[caption={Some code}]
\end{lstlisting}
\todo[inline=true,caption={A todo note}]{Must do this.}
\begin{algorithm}[htb] \DontPrintSemicolon \KwIn{Input} \KwOut{Output} Code\; \caption{Some algorithm.} \end{algorithm}
\begin{table}[htb]\caption{A table}\end{table}
\begin{figure}[htb]\caption{A figure}\end{figure}
\begin{lstlisting}[caption={Some code}]
\end{lstlisting}
\todo[inline=true,caption={A todo note}]{Must do this.}
\begin{algorithm}[htb] \DontPrintSemicolon \KwIn{Input} \KwOut{Output} Code\; \caption{Some algorithm.} \end{algorithm}
\end{document}

Run three times to get eg. LOA:

enter image description here

1
  • Works a treat. Integrating this with another similar question I have, I found making the modification \DeclareTOCStyleEntry[numwidth=3.5em]{tocline}{figure} (aka adding numwidth as an option) then ensured this was the same across all the list of somethings.
    – oliversm
    Commented Mar 4, 2020 at 14:24

You must log in to answer this question.

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