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}
- Why is there no
\algorithmicstatedefined? - Why, when I define a new command do I need to add a space after it?
- Why does
\def\ForEach{\ForAll}work but\def\ForEach{\algorithmicforall}not work