# Customizing the algorithm/algpseudocode package

After asking this question: Change FORALL to FOREACH in algorithms package, some issues arose in the comments. I have created the following sample document, and have three questions.

\documentclass{article}

\usepackage{algorithm}
% Need it for floating environment
\usepackage[noend]{algpseudocode}
% Hide endif .etc
\usepackage{caption}
% Need it for \caption*

\algrenewcommand{\algorithmicrequire}{\textbf{Input:}}
\algrenewcommand{\algorithmicensure}{\textbf{Output:}}
\algrenewcommand{\algorithmicforall}{\textbf{for each}}
% \algrenewcommand instead of \renewcommand according to manual
\newcommand{\To}{\textbf{to}}
% Not stated in manual, \Return and \algorithmicreturn are defined, but no \algorithmicstate, why?
\newcommand{\Dosth}{\textbf{Do Something }}
% Need space here, why?
\def\ForEach{\ForAll}
% Define new macro. Fail to use \algorithmicforall, why?
\begin{document}

\begin{algorithm}
\caption*{\textbf{Algorithm:} \textsc{Depth First Search}} \label{alg:dfs1}

\begin{algorithmic}
\Require Graph $\mathcal{G = (V,E)}$
\Ensure Graph $\mathcal{G}$ with its vertices marked with consecutive integers in the order they have been visited by the DFS traversal
\Statex
\State Mark each vertex in $\mathcal{V}$ with $0$ \Comment As unvisited''
\State $count \gets 0$
\ForEach{vertex $v \in \mathcal{V}$}
\If{$v$ is marked with $0$}
\State \Call{DFS}{$v$}
\EndIf
\EndFor
\State \Return
\Statex
\Function{DFS}{$v$}
\State $count \gets count + 1$
\State mark $v$ with $count$
\State \Dosth with $v$
\ForEach{vertex $w \in \mathcal{V}$ adjacent to $v$}
\If{$w$ is marked with $0$}
\State \Call{DFS}{$w$}
\EndIf
\EndFor
\State \Return
\EndFunction

\end{algorithmic}
\end{algorithm}

\end{document}

1. Why is there no \algorithmicstate defined?
2. Why, when I define a new command do I need to add a space after it?
3. Why does \def\ForEach{\ForAll} work but \def\ForEach{\algorithmicforall} not work
• I changed your question title to something a bit more useful, and moved your "questions in the comments" to real questions. It's not generally a good idea to ask questions in the code. Also, it's preferable to ask individual questions, unless there is a common thread to all of them. (In this case, that's a bit of a judgement call.) – Alan Munn May 17 '11 at 0:21
• @Alan Munn - Good, thx. And I have changed the question 3. – colinfang May 17 '11 at 0:32
• I've updated my answer for your new question 3 – Alan Munn May 17 '11 at 2:09

Why no \algorithmicstate?

Perhaps an oversight by the package writer? Perhaps never intended to exist. Either way, you can create your own version of the command using:

\algnewcommand{\algorithmicstate}{\textbf{State:}}


\newcommand{\Dosth}{\textbf{Do Something }} % Need space here, why?

This is answered here: \newcommand and spacing

Why does \def\ForEach{\ForAll} work but \def\ForEach{\algorithmicforall} not work?

Because \ForAll is a more complicated macro than \algorithmicforall. It is defined using the following command:

\algdef{S}[FOR]{ForAll}{\algorithmicforall\ #1\ \algorithmicdo}


The \algdef command does a lot of things, and one of the things it does with a for loop is to set up an expectation for the end of the for, i.e. the \EndFor.

When you define \ForEach to be ForAll, you don't change the definition of \ForAll you simply cause \ForEach to act like ForAll. But if you change ForEach into \algorithmicforall you fail to create the corresponding expectation for an end for, and the code fails when it encounters \EndFor.