The following piece of code will insert Algorithm~
before each number of the algorithm in the List of Algorithms (LoA):
\begingroup
\let\oldnumberline\numberline
\renewcommand{\numberline}{Algorithm~\oldnumberline}
\listofalgorithms
\endgroup
It redefines the \numberline
macro that inserts and sets the algorithm number into the LoA. For a good discussion on the meanings and placement/usage of these macros, read the tocloft
package documentation (section 1.1 LaTeX's methods). Bracing (or grouping using \begingroup
...\endgroup
) localizes the scope so it does not affect \numberline
used by other contents structures. Here's the full code that redefines \listofalgorithms
so it can be cleanly used inside your document body:
\documentclass{book}
\usepackage[breaklinks]{hyperref}
\usepackage{algorithm,algorithmic}
\let\oldlistofalgorithms\listofalgorithms
\renewcommand{\listofalgorithms}{%
\begingroup%
\let\oldnumberline\numberline%
\renewcommand{\numberline}{Algorithm~\oldnumberline}%
\oldlistofalgorithms%
\endgroup}
\begin{document}
\listofalgorithms
\chapter{Sample}
% 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}
If you wish to avoid the horizontal space introduced between the margin and the algorithm contents entries, you can use the following redefinition of \listofalgorithms
:
\makeatletter
\renewcommand{\listofalgorithms}{%
\begingroup%
\let\oldnumberline\numberline%
\let\old@dottedtocline\@dottedtocline
\renewcommand{\@dottedtocline}[5]{\old@dottedtocline{##1}{0pt}{##3}{##4}{##5}}%
\renewcommand{\numberline}{Algorithm~\oldnumberline}%
\oldlistofalgorithms%
\endgroup}
\makeatother
The above update of \@dottedtocline
changes the default 1.5em
spacing usually placed in the second argument with 0pt
.
Here is my train of thought in terms of analysing the problem...
I first try and create a minimal example that replicates your problem. This means I keep an eye open for the things that you generate, and package that could influence them. For that, algorithm
and tocloft
stand out:
\documentclass{memoir}
\usepackage{algorithm,tocloft}
\renewcommand*{\cftdotsep}{1}
\begin{document}
\listoffigures
\listofalgorithms
\begin{figure}[ht]
\caption{A figure}
\end{figure}
\begin{algorithm}[ht]
\caption{An algorithm}
\end{algorithm}
\end{document}
That was easy...
Now we try and understand the difference between how \listoffigures
and \listofalgorithms
generate there output. Well, to understand this, it seems like tocloft
's \cftdotsep
has an influence on the former, but not the latter. Strange... Looking at the code of algorithms.dtx
- the source bundle for the algorithm
package - it is clear that the algorithm
float environment is generated by means of the float
package with \listofalgorithms
having the following definition:
\newcommand{\listofalgorithms}{\listof{algorithm}{\listalgorithmname}}
That is, \listofalgorithms
actually just calls some \listof
routine provided by float
. So, let's look at \listof
:
\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}}
Ahhh, with a call to \listof{<name>}{..}
, it creates \l@<name>
- the macro responsible for setting each entry inside the List of XX construct. From this I'm able to conclude that the in-line construction of \l@algorithm
is most likely not compatible with tocloft
, and that's why it's not affected in the same way.
The resolution is to attempt to make \l@algorithm
act exactly like \l@figure
. In TeX terms, this is as simple as \let\l@algorithm\l@figure
. We can use etoolbox
to patch \listof
to always make \l@XX
be equivalent to \l@figure
(for any XX
):
\usepackage{etoolbox}
\makeatletter
\patchcmd{\listof}% <cmd>
{\float@listhead}% <search>
{\@namedef{l@#1}{\l@figure}\float@listhead}% <replace>
{}{}% <success><failure>
\makeatother
The above patch inserts a variant of the above-mentioned \let
into the appropriate location of \listof
.
Here's a minimal example showing the output:
\documentclass{memoir}
\usepackage{etoolbox}
\usepackage{algorithm,tocloft}
\makeatletter
\patchcmd{\listof}% <cmd>
{\float@listhead}% <search>
{\@namedef{l@#1}{\l@figure}\float@listhead}% <replace>
{}{}% <success><failure>
\makeatother
\renewcommand*{\cftdotsep}{1}
\begin{document}
\listoffigures
\listofalgorithms
\begin{figure}[ht]
\caption{A figure}
\end{figure}
\begin{algorithm}[ht]
\caption{An algorithm}
\end{algorithm}
\end{document}
Best Answer
Simply worked for me, Added this to
frontmatter.tex
file. But formatting is different from rest of TOC. I will accept, if anyone else want to create an answer.