TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to add the word "Algorithm" before each entry in the List of Algorithms (because of unavoidable thesis guidelines).

I saw similar questions but the solutions listed there don't work for me. I'm not sure about reopening those questions, hence posting it separately. Apologize if that's not the right etiquette.

Is it possible to change the way a list of algorithms is generated to include the word "Algorithm" before each element in the list? (I'm using "algorithm" package instead of "algorithm2e")

Add algorithm label to list of algorithms (not sure if solutions listed there require other packages as listed solutions don't work for me)

Current output:

List of Algorithms
1 Optimization . . . . . . . . 3

Desired output:

List of Algorithms
Algorithm 1 Optimization . . . . . . . . 3

Here's a simplified code sample that illustrates the issue. Your help is much appreciated. Please let me know how to get desired behavior for this sample.

\documentclass[letter,10pt]{book}                                                                                                                                                                             
\usepackage[utf8x]{inputenc}

\usepackage[breaklinks]{hyperref}
\usepackage{algorithm}
\usepackage{algorithmic}

\title{Sample book}
\begin{document}
\listofalgorithms

\chapter{Sample}
% Outline
\begin{algorithm}[!hbt]
    \centering
    \caption{Optimization}\label{alg:pso}
    \begin{algorithmic}[1]
        \STATE \textit{GenerateInitialPopulation}(pop)
        \FOR {particle $ \leftarrow $ 1 \textit{to} numParticles}
          \STATE \textit{Evaluate}(particle)
        \ENDFOR
    \end{algorithmic}
\end{algorithm}

\end{document}
share|improve this question
1  
Welcome to TeX.SE! Thanks for providing a MWE :) – cmhughes Feb 6 '13 at 2:44
up vote 9 down vote accepted

The following piece of code will insert Algorithm~ before each number of the algorithm in the List of Algorithms (LoA):

\begingroup
\let\oldnumberline\numberline
\renewcommand{\numberline}{Algorithm~\oldnumberline}
\listofalgorithms
\endgroup

It redefines the \numberline macro that inserts and sets the algorithm number into the LoA. For a good discussion on the meanings and placement/usage of these macros, read the tocloft package documentation (section 1.1 LaTeX's methods). Bracing (or grouping using \begingroup...\endgroup) localizes the scope so it does not affect \numberline used by other contents structures. Here's the full code that redefines \listofalgorithms so it can be cleanly used inside your document body:

enter image description here

\documentclass{book}
\usepackage[breaklinks]{hyperref}
\usepackage{algorithm,algorithmic}

\let\oldlistofalgorithms\listofalgorithms
\renewcommand{\listofalgorithms}{%
  \begingroup%
  \let\oldnumberline\numberline%
  \renewcommand{\numberline}{Algorithm~\oldnumberline}%
  \oldlistofalgorithms%
  \endgroup}
\begin{document}

\listofalgorithms

\chapter{Sample}
% Outline
\begin{algorithm}[!hbt]
  \centering
  \caption{Optimization}\label{alg:pso}
  \begin{algorithmic}[1]
    \STATE \textit{GenerateInitialPopulation}(pop)
    \FOR {particle $ \leftarrow $ 1 \textit{to} numParticles}
      \STATE \textit{Evaluate}(particle)
    \ENDFOR
  \end{algorithmic}
\end{algorithm}

\end{document}

If you wish to avoid the horizontal space introduced between the margin and the algorithm contents entries, you can use the following redefinition of \listofalgorithms:

\makeatletter
\renewcommand{\listofalgorithms}{%
  \begingroup%
  \let\oldnumberline\numberline%
  \let\old@dottedtocline\@dottedtocline
  \renewcommand{\@dottedtocline}[5]{\old@dottedtocline{##1}{0pt}{##3}{##4}{##5}}%
  \renewcommand{\numberline}{Algorithm~\oldnumberline}%
  \oldlistofalgorithms%
  \endgroup}
\makeatother

The above update of \@dottedtocline changes the default 1.5em spacing usually placed in the second argument with 0pt.

enter image description here

share|improve this answer
    
Works great. Thanks for the quick response! – Sarat Feb 6 '13 at 3:37
    
This works but leaves horizontal space on the left. No way to override the \contentsline directly? – the swine Mar 11 at 0:14
    
@theswine: See my updated answer. – Werner Mar 11 at 0:32
    
@Werner oh, nice, just slapped \let\oldlistofalgorithms\listofalgorithms on top and works like a charm. Thanks a lot (upvoted yesterday already). – the swine Mar 11 at 9:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.