[Tex/LaTex] undefined control sequence ‘\For’ – algorithmicx package

algorithmicx

I have to write some algorithm, thus I've decided to use algorithmicx package:

\usepackage{algorithm}
\usepackage{algorithmicx}

\begin{algorithm}
    \begin{algorithmic}[1]
        \For{aa}
            \State $Sim(i,j) \gets \frac{v_{i} \cdot v_{j}}{\left|v_{i}\right|\left|v_{j}\right|}$
        \EndFor
    \end{algorithmic}
    \label{alg:rAP}
    \caption{algorithm}
\end{algorithm}

Everything is fine but LaTeX saysUndefinde control sequence \For

I'm very confused because I've just follow the instruction of algorithmicx

Also there is no problem to use \State (when I've commented out \For)

I've also tried \FOR, \for but nothings work.

Seems algorithmicx package are not deprecated and it makes me more confused.

Best Answer

From the algorithmicx documentation:

The package algorithmicx itself doesn’t define any algorithmic commands, but gives a set of macros to define such a command set. You may use only algorithmicx, and define the commands yourself, or you may use one of the predefined command sets.

These predefined command sets (layouts) are:

algpseudocode...

algcompatible...

algpascal...

algc...

This means that loading the algorithmicx package only, you have to define yourself the commands.

If you want to use one of the predefined sets of commands, you have to load the corresponding package, in your case algpseudocode.

MWE:

\documentclass{article}

\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}
    \begin{algorithmic}[1]
        \For{aa}
            \State $Sim(i,j) \gets \frac{v_{i} \cdot v_{j}}{\left|v_{i}\right|\left|v_{j}\right|}$
        \EndFor
    \end{algorithmic}
    \label{alg:rAP}
    \caption{algorithm}
\end{algorithm}

\end{document} 

Output:

enter image description here