[Tex/LaTex] Style \listofalgorithms from algorithm package

algorithmstable of contentstocloft

I have some weird requirements for thesis formatting at my university. I was able to manage to mimic almost all of them, but List of Algorithms from algorithm package is something I cannot go through – I don't even know where to start looking. Basically what I want to achieve is to have same formatting for LoA like for LoF.

Minimum working example is below:

\documentclass[10pt,a4paper,twoside]{report}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage[left=3.5cm, right=2.5cm, top=2.5cm, bottom=2.5cm]{geometry}

\usepackage{tocloft}
\setlength{\cftafterloftitleskip}{6pt}
\renewcommand{\cftloftitlefont}{\clearpage \large\bfseries\MakeUppercase}
\renewcommand{\cftdotsep}{0.0}
\setlength{\cftparskip}{6pt}
\setlength{\cftbeforechapskip}{0pt}
\renewcommand{\cftchapfont}{}
\renewcommand{\cftchapleader}{\cftdotfill{\cftdot}}
\renewcommand{\cftchappagefont}{}
\renewcommand{\cftchappresnum}{\chaptertitlename}
\renewcommand{\cftchapaftersnum}{.}
\renewcommand{\cftsecaftersnum}{.}
\renewcommand{\cftsubsecaftersnum}{.}
\renewcommand{\cftfigaftersnum}{.}
\setlength{\cftfigindent}{0pt}
\setlength{\cfttabindent}{0pt}

\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{tikz}

\begin{document}

\chapter{Chapter}

\section{Section}

\begin{algorithm}
    \begin{algorithmic}[1]
        \State $a \gets 0$
    \end{algorithmic}
    \caption{Alg}\label{alg:algorithm}
\end{algorithm}

\begin{figure}[h]
    \begin{tikzpicture}
        \coordinate [label=left:$m$,circle,fill,inner sep=1pt] (m) at (-12em,-4.em);
        \coordinate [label=below:$O$,circle,fill,inner sep=1pt] (O) at (2em,-10em);
        \draw[blue, ->, thick] (m) -- node[above] {} (O);
    \end{tikzpicture}
    \caption{Fig}
    \label{fig:figure}
\end{figure}

\listoffigures
\addcontentsline{toc}{chapter}{List of Figures}
\listofalgorithms
\addcontentsline{toc}{chapter}{List of Algorithms}

\end{document}

Best Answer

The following segment of code makes an entire copy of the macros involved with \listoffigures, applying them to a redefined \listofalgorithms:

\usepackage{etoolbox}

\makeatletter
\AtBeginDocument{%
  \let\l@algorithm\l@figure%
  \let\listofalgorithms\listoffigures% Copy \listoffigures
  \let\@cftmakeloatitle\@cftmakeloftitle% Copy LoF title
  % Update LoA-related macros
  \patchcmd{\listofalgorithms}{\@cftmakeloftitle}{\@cftmakeloatitle}{}{}%
  \patchcmd{\listofalgorithms}{\@starttoc{lof}}{\@starttoc{loa}}{}{}%
  \patchcmd{\@cftmakeloatitle}{\listfigurename}{\listalgorithmname}{}{}%
  % Add per-chapter LoA space (similar to LoF)
  \patchcmd{\@chapter}{\addtocontents}{%
    \addtocontents{loa}{\protect\addvspace{10\p@}}%
    \addtocontents}{}{}%
}
\makeatother

Here's your example in action:

enter image description here

\documentclass{report}

\usepackage{tocloft}
\setlength{\cftafterloftitleskip}{6pt}
\renewcommand{\cftloftitlefont}{\clearpage \large\bfseries\MakeUppercase}
\renewcommand{\cftdotsep}{0.0}
\setlength{\cftparskip}{6pt}
\setlength{\cftbeforechapskip}{0pt}
\renewcommand{\cftchapfont}{}
\renewcommand{\cftchapleader}{\cftdotfill{\cftdot}}
\renewcommand{\cftchappagefont}{}
\renewcommand{\cftchappresnum}{\chaptertitlename}
\renewcommand{\cftchapaftersnum}{.}
\renewcommand{\cftsecaftersnum}{.}
\renewcommand{\cftsubsecaftersnum}{.}
\renewcommand{\cftfigaftersnum}{.}
\setlength{\cftfigindent}{0pt}
\setlength{\cfttabindent}{0pt}

\usepackage{algorithm,etoolbox}

\makeatletter
\AtBeginDocument{%
  \let\l@algorithm\l@figure%
  \let\listofalgorithms\listoffigures%
  \let\@cftmakeloatitle\@cftmakeloftitle%
  \patchcmd{\listofalgorithms}{\@cftmakeloftitle}{\@cftmakeloatitle}{}{}%
  \patchcmd{\listofalgorithms}{\@starttoc{lof}}{\@starttoc{loa}}{}{}%
  \patchcmd{\@cftmakeloatitle}{\listfigurename}{\listalgorithmname}{}{}%
  \patchcmd{\@chapter}{\addtocontents}{%
    \addtocontents{loa}{\protect\addvspace{10\p@}}%
    \addtocontents}{}{}%
}
\makeatother

\begin{document}

\listoffigures

\listofalgorithms

\chapter{Chapter}

\begin{algorithm}
  \caption{Alg}
\end{algorithm}

\begin{figure}[h]
  \caption{Fig}
\end{figure}

\end{document}
Related Question