使用 tocbasic 刷新列表和算法列表的右页码

使用 tocbasic 刷新列表和算法列表的右页码

根据之前的问题将目录点扩展到大页码我一直在使用tocbasic罗马字页面,也有一些。在所有某事的清单罗马数字右对齐,以下数字除外:

  • 列表列表。
  • 算法列表。
  • 待办事项列表(不太担心这个)。

我怎样才能使它们正确对齐?

在此处输入图片描述

在此处输入图片描述

\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

初始化预定义值level并使用indentnumwidthfigure

\DeclareTOCStyleEntry[dynnumwidth]{tocline}{figure}

dynnumwidth如果输入的数字太长,则会自动扩大输入数字的空间。但需要额外运行一次。

然后您可以复制(至少需要 KOMA-Script 版本 3.28)这些值用于其他条目类型:

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

最后添加lstlisting,algocf,todo以下代码片段:

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

例子:

\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}

运行三次即可获得 LOA:

在此处输入图片描述

相关内容