5

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}
lockstep
  • 250,273
dulek
  • 53

2 Answers2

2

There are doubtless many, many reasons not to do this...

Caveat emptor...

The idea is essentially to write the list of algorithms to the auxiliary files twice.

We let the algorithm package do its own thing: it has its own counter, its own contents lines, its own format. The lot. We just never print the list of algorithms it produces.

Instead, we modify the algorithm environment using etoolbox so that it increments two counters, writes two lots of information to the auxiliary files etc.:

\AtBeginEnvironment{algorithm}{%
  \let\oldcaption\caption
  \renewcommand\caption[1]{%
    \oldcaption{#1}%
    \stepcounter{alg}%
    \addcontentsline{loalg}{alg}{\protect\numberline{\thealg}{ #1}}}}

This saves the old definition of \caption and then redefines it to also increment tocloft's counter and write a contents line to tocloft's auxiliary file. These are created in the usual way:

\newlistof[chapter]{alg}{loalg}{\listalgorithmname}

Hence, we can apply the same customisation to it as in the case of the list of figures:

\setlength{\cftafterloalgtitleskip}{6pt}
\renewcommand{\cftloalgtitlefont}{\clearpage \large\bfseries\MakeUppercase}
...
\renewcommand{\cftalgaftersnum}{.}
...
\setlength{\cftalgindent}{0pt}

This is, of course, not a terribly efficient solution. For one thing, it wastes a counter. However, minimal testing suggests it can be made to work:

lists of figures and algorithms

Complete code:

\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,etoolbox}
\newlistof[chapter]{alg}{loalg}{\listalgorithmname}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{tikz}

\setlength{\cftafterloftitleskip}{6pt}
\renewcommand{\cftloftitlefont}{\clearpage \large\bfseries\MakeUppercase}
\setlength{\cftafterloalgtitleskip}{6pt}
\renewcommand{\cftloalgtitlefont}{\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}{.}
\renewcommand{\cftalgaftersnum}{.}
\setlength{\cftfigindent}{0pt}
\setlength{\cfttabindent}{0pt}
\setlength{\cftalgindent}{0pt}

\AtBeginEnvironment{algorithm}{%
  \let\oldcaption\caption
  \renewcommand\caption[1]{%
    \oldcaption{#1}%
    \stepcounter{alg}%
    \addcontentsline{loalg}{alg}{\protect\numberline{\thealg}{ #1}}}}

\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}{\listfigurename}
\listofalg
\addcontentsline{toc}{chapter}{\listalgorithmname}
\end{document}
cfr
  • 198,882
2

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}
Werner
  • 603,163
  • 1
    It may be relevant to also add \renewcommand{\thealgorithm}{\thechapter.\arabic{algorithm}}. – Werner Aug 30 '15 at 02:44
  • 1
    @Wener : Thanks. I tried your example. It does format the 'list of algorithms' in the same format as 'list of figures'. However, the word 'Figure' appears instead of 'Algorithm' in the toc as well as the list. What can I do? – Abhinav Feb 27 '20 at 13:55
  • @Abhinav: this helped me: https://tex.stackexchange.com/questions/186077/adjust-format-of-list-of-algorithms – Malte Oct 23 '20 at 09:24
  • @Abhinav: I can't replicate your issue with my example. Can you provide a minimal example that replicates that problematic behaviour? – Werner Oct 23 '20 at 15:25