![增加了附录中 ToC 中的算法编号宽度](https://linux22.com/image/464411/%E5%A2%9E%E5%8A%A0%E4%BA%86%E9%99%84%E5%BD%95%E4%B8%AD%20ToC%20%E4%B8%AD%E7%9A%84%E7%AE%97%E6%B3%95%E7%BC%96%E5%8F%B7%E5%AE%BD%E5%BA%A6.png)
我想增加来自附录的算法数字宽度。
梅威瑟:
\documentclass{report}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{algorithmic}
\numberwithin{algorithm}{chapter}
%\RequirePackage{tocloft}
%\addtolength{\cftalgnumwidth}{1em}
\NewCommandCopy{\oldlistofalgorithms}{\listofalgorithms}
\renewcommand{\listofalgorithms}{%
\begingroup
\NewCommandCopy{\oldnumberline}{\numberline}
\renewcommand{\numberline}[1]{Algorithm~\oldnumberline{##1:}}%
\oldlistofalgorithms
\endgroup
}
\begin{document}
\listofalgorithms
\chapter{Test}
\begin{algorithm}[!htb]
\caption{RANSAC}
\begin{algorithmic}[1]
\STATE Text
\end{algorithmic}
\end{algorithm}
\appendix
\chapter{Other}
\setcounter{algorithm}{95}
\begin{algorithm}[!htb]
\caption{RANSAC}
\begin{algorithmic}[1]
\STATE Text
\end{algorithmic}
\end{algorithm}
\begin{algorithm}[!htb]
\caption{RANSAC}
\begin{algorithmic}[1]
\STATE Text
\end{algorithmic}
\end{algorithm}
\begin{algorithm}[!htb]
\caption{RANSAC}
\begin{algorithmic}[1]
\STATE Text
\end{algorithmic}
\end{algorithm}
\end{document}
预期输出:
答案1
算法列表 (LoA) 中的条目通过 进行设置\l@algorithm
,它是在调用 时创建的\listofalgorithms
(使用float
包裹的\listof{algorithm}{List of Algorithms}
)。由于此创建仅在您创建 LoA 时完成,因此您可以使用新的 LaTeX3 钩子\l@algorithm
在创建后立即插入条目更新。要查看具体位置,这里是(格式化的)输出\ShowCommand{\listof}
(也可以在内部查看float.dtx
):
\newcommand*{\listof}[2]{%
\@ifundefined{ext@#1}{\float@error{#1}}{%
\@namedef{l@#1}{\@dottedtocline{1}{1.5em}{2.3em}}%
\float@listhead{#2}%
\begingroup\setlength{\parskip}{\z@}%
\@starttoc{\@nameuse{ext@#1}}%
\endgroup}}
注意\l@#1
(在你的情况下) 是如何通过调用之前\l@algorithm
创建的。因此,我们可以添加一个钩子,在下次调用时只执行一次,如下所示:\@namedef{<csname>}{<stuff>}
\float@list
\float@listhead
\AddToHookNext{cmd/float@listhead/before}{%
\@namedef{l@algorithm}{\@dottedtocline{1}{1.5em}{3em}}}% Update spacing of \numberline's width
默认2.3em
宽度已调整为3em
。将所有这些放在您的代码中:
\documentclass{report}
\usepackage{amsmath}
\usepackage{algorithm,algorithmic}
\numberwithin{algorithm}{chapter}
\NewCommandCopy{\oldlistofalgorithms}{\listofalgorithms}
\makeatletter
\renewcommand{\listofalgorithms}{%
\begingroup
\NewCommandCopy{\oldnumberline}{\numberline}%
\AddToHookNext{cmd/float@listhead/before}{%
\@namedef{l@algorithm}{\@dottedtocline{1}{1.5em}{3em}}}% Update spacing of \numberline's width
\renewcommand{\numberline}[1]{Algorithm~\oldnumberline{##1:}}%
\oldlistofalgorithms
\endgroup
}
\makeatother
\begin{document}
\listofalgorithms
\chapter{Test}
\begin{algorithm}
\caption{RANSAC}
\begin{algorithmic}[1]
\STATE Text
\end{algorithmic}
\end{algorithm}
\appendix
\chapter{Other}
\setcounter{algorithm}{95}
\begin{algorithm}
\caption{RANSAC}
\begin{algorithmic}[1]
\STATE Text
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\caption{RANSAC}
\begin{algorithmic}[1]
\STATE Text
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\caption{RANSAC}
\begin{algorithmic}[1]
\STATE Text
\end{algorithmic}
\end{algorithm}
\end{document}
如果希望算法与左边距齐平,请将的第二个参数更改\@dottedtocline
为0pt
:
\AddToHookNext{cmd/float@listhead/before}{%
\@namedef{l@algorithm}{\@dottedtocline{1}{0pt}{3em}}}% Update spacing of \numberline's width
如果你希望进行此调整仅有的对于附录区域,在附录开始的位置插入更改:
...
\appendix
\makeatletter
\addtocontents{loa}{\protect\renewcommand{\protect\l@algorithm}{\protect\@dottedtocline{1}{1.5em}{3em}}}
\makeatother
...