8

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.

  • You have to load algpseudocode and not algorithmicx – karlkoeller Feb 26 '14 at 11:12
  • Thanks a lot, it works. However, the instruction says as if algorithmicx also works. Is it wrong? cs.indstate.edu/CS695/algorithmicx.pdf , page 7. – Jeon Feb 26 '14 at 11:16
  • BTW: Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Feb 26 '14 at 11:30
10

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

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.