Search code examples
algorithmlatex

! Missing number, treated as zero error in latex when using for loop


\begin{algorithm}
\caption{AlgorithmCH election algorithm}
\label{algorithm}
\begin{algorithmic}[1]
\Procedure{CHElection}

\For{each node i }
\EndFor

\EndProcedure
\end{algorithmic}
\end{algorithm}

I have this code used to write an algorithm in LATEX but I received this error (! Missing number, treated as zero error) when I point to the error, I saw it at \End for. Can anyone tell me why I am getting this error please?

This are the packages I added for writing an algorithm

\usepackage[noend]{algpseudocode}  
\usepackage[ruled,noresetcount,noend]{algorithm2e}

Solution

  • Don't load algorithm2e when you're using algpseudocode. The former creates an algorithm floating environment, but in order to use algorithmic from algpseudocode, you should load algorithm instead (from the algorithms bundle).

    enter image description here

    \documentclass{article}
    \usepackage{algorithm}
    \usepackage[noend]{algpseudocode}
    \begin{document}
    
    \begin{algorithm}
      \caption{AlgorithmCH election algorithm}
      \label{algorithm}
      \begin{algorithmic}[1]
        \Procedure{CHElection}{}
          \For{each node~$i$}
          \EndFor
        \EndProcedure
      \end{algorithmic}
    \end{algorithm}
    
    \end{document}
    

    Note also the second (mandatory) argument for \Procedure, which designates the arguments passed to the procedure. It can also be left blank, but you need to explicitly include this.