3

I want to change the layout of the list of algorithms provided by algorithm2e from this

1 Sorting...........................15

to this

Algorithm 1 -- Sorting..............15

I was already able to add the "Algorithm" word by following the instructions in this answer, but could not get the en dash to appear. I tried to renew the \numberline to add the en dash, but the results weren't satisfactory. I know the memoir class has the \cftfigureaftersnum for inserting text after the figure numbers in the LOF and the \cfttableaftersnum as the equivalent for tables, but I could not find an equivalent for algorithms.

Any thoughts on this?

EDIT: I am giving support to the LaTeX class from my university, which is based on memoir. It uses algorithm2e as the default package for creating algorithms and does not use tocloft.

1
  • Please tell us more about your document setup. E.g., which document class do you use? (It would seem to be memoir, but it's not entirely clear.) Also, please state (a) which package(s) you use to create algorithm-type environments and (b) how the List of Algorithms is produced. Having this information would let potential answer-writers eliminate a lot of inefficient and pointless guesswork.
    – Mico
    Commented Oct 24, 2016 at 5:55

2 Answers 2

2

A native way of setting the LoA entries within memoir (or default classes loading tocloft) is to make each entry mimic the style of the LoF. This is easily achieved by adding

\makeatletter
\let\l@algocf\l@figure
\makeatother

to the document preamble. Now the regular \cftX... macros can by used:

enter image description here

\documentclass{memoir}

\usepackage{algorithm2e}

\makeatletter
\let\l@algocf\l@figure% Handle LoA exactly like LoF
\makeatother

\let\oldlistofalgorithms\listofalgorithms
\renewcommand{\listofalgorithms}{{%
  % Locally update LoA entries as they are now similar to LoF
  \renewcommand{\cftfigurepresnum}[1]{\gdef\algonum{##1}}% Grab algorithm number
  \renewcommand{\cftfigureaftersnumb}{Algorithm~\algonum~--~}%
  \setlength{\cftfigurenumwidth}{0pt}%
  \oldlistofalgorithms
}}

\begin{document}

\listofalgorithms

\begin{algorithm}
  \caption{An algorithm}
\end{algorithm}

\end{document}
1
  • thanks, both of your answers worked like a charm! Since I already had some custom formatting in my list of figures, this answer worked better, but I tried both and they both worked. Commented Oct 24, 2016 at 16:27
1

There is no native support for algorithm2e in memoir (or the default document classes, even under the addition of tocloft). However, you can modify the LoA entry setter - \l@algocf - quite readily:

enter image description here

\documentclass{memoir}

\usepackage{algorithm2e}

\let\oldlistofalgorithms\listofalgorithms
\let\oldnumberline\numberline% Store \numberline
\newcommand{\algnumberline}[1]{Algorithm~#1 -- }
\renewcommand{\listofalgorithms}{%
  \let\numberline\algnumberline% Update \numberline
  \oldlistofalgorithms
  \let\numberline\oldnumberline% Restore \numberline
}

\begin{document}

\listofalgorithms

\begin{algorithm}
  \caption{An algorithm}
\end{algorithm}

\end{document}

If you wish to remove the indent of the LoA entries, also add the following:

\makeatletter
% https://tex.stackexchange.com/a/334060/5764
\renewcommand{\l@algocf}{\@dottedtocline{1}{0pt}{2.3em}}% Remove indent of algorithm in LoA
\makeatother

enter image description here

You must log in to answer this question.

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