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.


% Need it for floating environment
% Hide endif .etc
% Need it for \caption*

\algrenewcommand{\algorithmicforall}{\textbf{for each}}
% \algrenewcommand instead of \renewcommand according to manual
% Not stated in manual, \Return and \algorithmicreturn are defined, but no \algorithmicstate, why?
\newcommand{\Dosth}{\textbf{Do Something }}
% Need space here, why?
% Define new macro. Fail to use \algorithmicforall, why?

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

        \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
        \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$}
        \State \Return
            \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$}
            \State \Return


  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
  • 2
    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

Here are the answers to your questions.

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:


\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}[1]{\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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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