我想在 LOA 中添加“算法”一词。使用此方法:LOA 中的算法
将该术语添加到 LOA。但是,因为我想进一步构建我的 LOA(根据“零件”),所以我还打算将零件编号作为标题添加到 TOC/LOF/LOA 中。但是,使用此代码还会在“零件编号”前面写入“算法”
以下 MWE 说明了这个问题:
\documentclass{scrreprt}
\usepackage{algorithm}
\usepackage{algorithmic}
% Adding "Algorithm" in LOA
\let\oldlistofalgorithms\listofalgorithms
\renewcommand{\listofalgorithms}{%
\begingroup%
\let\oldnumberline\numberline%
\renewcommand{\numberline}{Algorithm~\oldnumberline}%
\oldlistofalgorithms%
\endgroup}
\begin{document}
% Adding headlines in LOA
\newcommand{\addtocentrylistof}[3]{%
\ifstr{#2}{}{%
\addcontentsline{lof}{#1}{#3}%
\addcontentsline{lot}{#1}{#3}%
\addcontentsline{loa}{#1}{#3}%
}{%
\addcontentsline{lof}{#1}{\protect\numberline{#2}#3}%
\addcontentsline{lot}{#1}{\protect\numberline{#2}#3}%
\addcontentsline{loa}{#1}{\protect\numberline{#2}#3}%
}%
}
\renewcommand*{\addparttocentry}[2]{%
\addtocentrydefault{part}{#1}{#2}%
\addtocentrylistof{part}{#1}{#2}%
}
\listofalgorithms
\part{A}
% Outline
\begin{algorithm}[!hbt]
\centering
\caption{Optimization}\label{alg:pso}
\begin{algorithmic}[1]
\STATE \textit{GenerateInitialPopulation}(pop)
\FOR {particle $ \leftarrow $ 1 \textit{to} numParticles}
\STATE \textit{Evaluate}(particle)
\ENDFOR
\end{algorithmic}
\end{algorithm}
\end{document}
我想要的:仅将零件编号(此处为:“I”)和零件标题(此处为:“A”)作为所有算法的标题,这些算法也被称为“算法 1...”
答案1
实现目标的一种方法是拦截\numberline
in的打印 - 负责在 ToC 相关文件中\l@part
打印类似条目的宏:\part
\documentclass{scrreprt}
\usepackage{algorithm,etoolbox}
% Adding "Algorithm" in LOA
\makeatletter
\newcommand{\partnumberline}[1]{Part~#1\quad\ignorespaces}
\patchcmd{\l@part}% <cmd>
{\leavevmode}% <search>
{\leavevmode\let\numberline\partnumberline}% <replace>
{}{}% <success><failure>
\let\oldlistofalgorithms\listofalgorithms
\renewcommand{\listofalgorithms}{%
\begingroup%
\let\oldnumberline\numberline%
\renewcommand{\numberline}{Algorithm~\oldnumberline}%
\oldlistofalgorithms%
\endgroup}
\makeatother
\begin{document}
% Adding headlines in LOA
\newcommand{\addtocentrylistof}[3]{%
\ifstr{#2}{}{%
\addcontentsline{lof}{#1}{#3}%
\addcontentsline{lot}{#1}{#3}%
\addcontentsline{loa}{#1}{#3}%
}{%
\addcontentsline{lof}{#1}{\protect\numberline{#2}#3}%
\addcontentsline{lot}{#1}{\protect\numberline{#2}#3}%
\addcontentsline{loa}{#1}{\protect\numberline{#2}#3}%
}%
}
\renewcommand*{\addparttocentry}[2]{%
\addtocentrydefault{part}{#1}{#2}%
\addtocentrylistof{part}{#1}{#2}%
}
\listofalgorithms
\part{A}
% Outline
\begin{algorithm}[!hbt]
\centering
\caption{Optimization}
\end{algorithm}
\end{document}
上述补丁(感谢etoolbox
) 用 替换,\numberline
我们将其定义为仅打印。\l@part
\partnumberline
Part~#1\quad