[Tex/LaTex] How to add a List of Algorithms to thethesis.cls

algorithmstable of contents

Currently I am using the algorithmic and algorithm packages for generating algorithms in my thesis using the mythesis class.

I tried simply using \listofalgorithms but it does not give the same format as my List of Figures/Tables and it doesn't get added to the Table of Contents.

I went into the class file and found this code related to the table of figures:

%
%   List of figures
%
\def\textofLoF#1{\gdef\@textofLoF{#1}}  \textofLoF{List of Figures}
\def\listoffigures{\chapter*{\@textofLoF\@mkboth{}{}}
   \thispagestyle{plain}
   \addcontentsline{toc}{chapter}{\protect\@textofLoF}
   {\let\footnotemark\relax  % in case one is in the title
   \@starttoc{lof}
    }
   }
%

So I tried duplicating this in the class for a list of algorithms:

%   List of algorithms
%
\def\textofLoAl#1{\gdef\@textofLoAl{#1}}  \textofLoAl{List of Algorithms}
\def\listofalgorithms{\chapter*{\@textofLoAl\@mkboth{}{}}
   \thispagestyle{plain}
   \addcontentsline{toc}{chapter}{\protect\@textofLoAl}
   {\let\footnotemark\relax  % in case one is in the title
   \@starttoc{loal}
    }
   }

but now I get the error:

! LaTeX Error: Command \listofalgorithms already defined.
               Or name \end... illegal, see p.192 of the manual.

I am assuming this is due to the conflict of \listofalgorithms already defined in one of the packages. So I tried replacing \listofalgorithms in the previous code with \listofalgs, at which it compiles successfully, but the List of Algorithms is empty.

I noticed in my file directory that after compiling there is a file called Thesis.lof, containing a list of all the figures, and also a Thesis.loal, but this file is empty. I have no idea how TeX populates this Thesis.lof file, does anybody know? With this knowledge I should be able to duplicate it for the List of Algorithms.

Best Answer

The problem with your setup is that you're adding the \listofalgorithms to mythesis.cls. This means that \listofalgorithms gets defined when you load your class

\documentclass[..]{mythesis}

Subsequently you load algorithm which tries to create \listofalgorithms via a \newcommand, and this fails.

Instead, use

\def\textofLoAl#1{\gdef\@textofLoAl{#1}}
\textofLoAl{List of Algorithms}
\AtBeginDocument{%
  \def\listofalgorithms{%
    \chapter*{\@textofLoAl}
    \@mkboth{}{}%
    \thispagestyle{plain}
    \addcontentsline{toc}{chapter}{\protect\@textofLoAl}%
    {\let\footnotemark\relax  % in case one is in the title
     \@starttoc{loa}
    }%
  }}

which should delay your \definition until \begin{document}.

Note that I've used \@starttoc{loa} since the algorithm package writes algorithm \captions to a .loa file, not a .loal file.

Ideally one shouldn't modify the .cls directly. Instead, write modifications in a separate style file, or as part of your preamble (possibly wrapping it with a \makeatletter ... \makeatother pair).

Related Question