# Combine \listoflistings and \listofalgorithms into one list

The title said it all...I want to combine the two under one list with the title "Algorithms and program code". Style wise the page should match the usual style of the other lists (\listoffigures, \listoftables, ... itd.). Is there a way to do this?

• Should the counter be equal to? Or do you want any prefix? – Marco Daniel May 19 '13 at 7:25
• No prefix, as for the counter I don't really know...first time doing something with these lists – Veles May 19 '13 at 10:39
• Which algorithm-package do you use? I think a minimal working example is useful. – Marco Daniel May 19 '13 at 11:40

The contents lines and environment counters are added/incremented by \caption. So to change the list that an environment appears on, the caption command can be modified/extended. To keep the two environments in numerical sequence (ie as if they were the same environment) one can be incremented when the other is caption'd.

\documentclass{report}

\usepackage{listings}
\usepackage{algorithm}
\usepackage{etoolbox} % provides AtBeginEnvironment

% make a new caption command for algorithms
\newcommand{\algCaption}[1]{
\caption{#1}

\begin{document}
\begin{algorithm}
This is an algorithm
\algCaption{An algorithm}
\end{algorithm}
\begin{lstlisting}[caption=A listing]
This is a listing
\end{lstlisting}
\clearpage%
\begin{algorithm}
This is another algorithm
\algCaption{Another algorithm}
\end{algorithm}
\lstlistoflistings%
\listofalgorithms%
\end{document}


The MWE defines algCaption that adds its argument to the lstlisting and then increments the lstlisting counter. Whenever a lstlisting environment is used, the algorithm counter is incremented with AtBeginEnvironment. One problem that I can see with this solution is that there is still a listofalgorithms being produced by caption.

The general idea should be this: you redefine an attribute of the "caption making" parameters of the algorithm environment so that it uses the same as lstlisting; what's needed is \ext@algorithm that contains the file extension of the auxiliary file where the caption should be written.

\documentclass{report}

\usepackage{listings}
\usepackage{algorithm}
\makeatletter
\def\ext@algorithm{lol}% algorithm captions will be written to the .lol file
% share the list making commands and redefine the heading
\AtBeginDocument{%
\let\l@algorithm\l@lstlisting
\let\c@algorithm\c@lstlisting
\let\thealgorithm\thelstlisting
\renewcommand{\lstlistlistingname}{Algorithms and program code}%
}
\makeatother

\begin{document}

\lstlistoflistings

\clearpage

\begin{algorithm}
This is an algorithm
\caption{An algorithm}
\end{algorithm}

\begin{lstlisting}[caption=A listing]
This is a listing
\end{lstlisting}

\clearpage

\begin{algorithm}
This is another algorithm
\caption{Another algorithm}
\end{algorithm}

\end{document}


Let me try providing an answer. The example below uses algorithm2e. Of course related to your package the example must be modified.

As often it shows how important a minimal working example (MWE) is.

The modification must be done in several steps:

1. Change the name of the list of. This is done by:

\renewcommand\lstlistlistingname{Algorithms and program code}

2. Say algorithm2e to use the counter of listings This is done by:

\let\c@algocf\c@lstlisting

3. Say algorithm2e to write the material to the listings file lol:

\renewcommand{\algocf@list}{lol}%

4. Make the indent of the entries of algorithm and listings equal:

\renewcommand*\l@algocf{\@dottedtocline{1}{1.5em}{2.3em}}


Point 1 and 2 must be done after \begin{document}:

\documentclass{article}

\usepackage{listings}
\usepackage{algorithm2e}

\makeatletter
\AtBeginDocument{%
\renewcommand\lstlistlistingname{Algorithms and program code}
\let\c@algocf\c@lstlisting
}
\renewcommand{\algocf@list}{lol}%
\renewcommand*\l@algocf{\@dottedtocline{1}{1.5em}{2.3em}}
\makeatother
\begin{document}
\lstlistoflistings%
\begin{algorithm}
This is an algorithm
\caption{An algorithm}
\end{algorithm}
\begin{lstlisting}[caption=A listing]
This is a listing
\end{lstlisting}

\begin{algorithm}
This is another algorithm
\caption{Another algorithm}
\end{algorithm}
\end{document}