3

I want to write this algorithm but the error Undefined Control Sequence \For, \state, or \EndFor pops up.

\documentclass[titlepage,oneside,12pt]{article}
\usepackage[]{algorithm2e,algpseudocode}
\begin{document}
    \begin{algorithmic}[H]
     \KwData{Given the M detected objects}
     \KwResult{new clusters}
     \For (each object $i$)
        \state Create new cluster $C_i$;
        \state Initialize the cluster feature;
        \state $cluster Feature(i) = (x_i, y_i, v_{xi}, v_{yi}, 1, 0, i)$;
     \EndFor
     \caption{Initialization}
    \end{algorithmic}
\end{document}

I saw related questions but the case wasn't as mine.

Undefined control sequence \State \COMMENT

LaTeX algorithmic package: Undefined Control Sequence in for-Loop

  • 2
    Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Jul 3 '14 at 10:00
  • The error message shows \State but your code shows \state (lowercase). What's right? – user2478 Jul 3 '14 at 10:10
  • It is the same whether i used state or State. – Misaki Jul 3 '14 at 10:17
  • Is algpseudocode a known package? – user31729 Jul 3 '14 at 10:24
  • Yes, it is part of the algorithmicx package bundle. – LaRiFaRi Jul 3 '14 at 10:27
1

You are mixing up commands from the packages algorithm2e and algorithm. Well, I believe you do.

See my following MWE. This does compile and uses the syntax provided by the documentation here.

Is this what you wanted to type?

% arara: pdflatex

\documentclass[titlepage,oneside,12pt]{article}
\usepackage{algpseudocode,algorithm2e}
\begin{document}
    \begin{algorithm}[H]
     \KwData{Given the M detected objects}
     \KwResult{new clusters}
     \For{each object $i$}{
        Create new cluster $C_i$\;
        Initialize the cluster feature\;
        $\mathit{cluster Feature}(i) = (x_i, y_i, v_{xi}, v_{yi}, 1, 0, i)$\;
                }
     \caption{Initialization}
    \end{algorithm}
\end{document}

enter image description here

  • But why my version didn't work, although i am using the documentation in here en.wikibooks.org/wiki/LaTeX/Algorithms – Misaki Jul 3 '14 at 10:44
  • 1
    PLease see the headings on this wikipage. You took code from "Typesetting using the algorithmicx package". I took code from "Typesetting using the algorithm2e package". Please read the documentation I linked in my answer. Seems to be a good one, as I was able to use it in a few minutes (and never heard of the package before...) – LaRiFaRi Jul 3 '14 at 10:46
1

This uses algoritmm

enter image description here

Code

\documentclass[titlepage,oneside,12pt]{article}
\usepackage{algpseudocode}
\usepackage{algorithm}
\begin{document}
\begin{algorithm}
     \caption{Initialization}
    \begin{algorithmic}[1]
     \Require  Given the M detected objects
     \Ensure new clusters
     \For {(each object $i$)}
        \State Create new cluster $C_i$;
        \State Initialize the cluster feature;
        \State $cluster Feature(i) = (x_i, y_i, v_{xi}, v_{yi}, 1, 0, i)$;
     \EndFor
    \end{algorithmic}
\end{algorithm}
\end{document}

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.