[Tex/LaTex] Algorithms LaTeX \EndForAll and “Some Blocks are not closed!!!”

algorithmicxalgorithms

I'm new to the algorithm package in LaTeX and need help figuring out how to debug my MWE below, which gives six errors. It look complicated, but half the errors are the same thing and in one case I'm obviously not using \EndForAll correctly, even though it seems intuitive (and there's nothing online about it). I'm particularly curious about the \end{algorithmic} error, which says that Error: Some blocks are not closed!!! I provided End statements on every block I opened so I don't understand this error.

I would really appreciate any feedback.

\documentclass[]{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\renewcommand{\algorithmicforall}{\textbf{for each}}

\begin{document}


\begin{algorithm}
  \caption{algorithm}\label{euclid}
  \begin{algorithmic}[1]
  \Procedure{myalgo}{}
  \State $\textit{things} \gets \textit{stuff}$
  \State $g(thing)$ \gets \text{stuff }(\textit{material})$
  \Comment{comment}{}
  \ForAll{{$stuff$ in $things$}} :
  \State \text{Material }(\textit{stuff})
  \Comment{stuff}{}
  \State \text{\it{k}} = (\textit{stuff}, \textit{things})
  \EndForAll
  \If {$k$ is good}:
  \State $things$
  \EndIf
  \ForAll{{$things$ in $stuff$}} :
  \State $abs\bigg|{stuff} - {things}\bigg|$
  \State Material \bigg\{$abs \bigg|{stuff} - {things}\bigg|$\bigg\}
  \Comment{Ending}{}
  \EndProcedure
  \end{algorithmic}
\end{algorithm}


\end{document}

Best Answer

There is no \EndForAll, but \EndFor. You're missing one.

You're also inconsistent in how you treat variables and function names. Here's an edited version with a couple of helper macros. Be careful when entering and exiting math mode.

\documentclass[]{article}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\renewcommand{\algorithmicforall}{\textbf{for each}}

\newcommand{\var}[1]{\mathit{#1}}
\newcommand{\func}[1]{\mathrm{#1}}

\begin{document}


\begin{algorithm}
  \caption{algorithm}\label{euclid}
  \begin{algorithmic}[1]
  \Procedure{myalgo}{}
  \State $\var{things} \gets \var{stuff}$
  \State $g(\var{thing}) \gets \func{stuff}(\var{material})$
  \Comment{comment}{}
  \ForAll{$\var{stuff}$ in $\var{things}$}:
  \State $\func{Material}(\var{stuff})$
  \Comment{stuff}{}
  \State $k = (\var{stuff}, \var{things})$
  \EndFor
  \If {$k$ is good}:
  \State $\var{things}$
  \EndIf
  \ForAll{$\var{things}$ in $\var{stuff}$}:
  \State $\func{abs}\big|\var{stuff} - \var{things}\big|$
  \State $\func{Material} \bigl\{\func{abs} \big|\var{stuff} - \var{things}\big|\bigr\}$
  \Comment{Ending}{}
  \EndFor
  \EndProcedure
  \end{algorithmic}
\end{algorithm}


\end{document}

Note that \bigg is way too big!

enter image description here