[Tex/LaTex] Customizing the algorithm/algpseudocode package

algorithms

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

Best Answer

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:

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