Algorithm not formatting correctly

algorithmicalgorithms

I'm trying to write an algorithm in latex, but after I put my if statement it gives me back an error and says that there's a missing number. What does that mean? Where is my flaw? This is what I've written (mind you I'm trying to write a generalized algorithm that doesn't necessarily examine things, hence the general statement in if):

\For{j = length($y_i$)}{
   \If{$\{x_j$ and $\neg x_j\}$ in $y_i$}{
     \State \text{$a_i$ = F}
        }

Can I do that?

Best Answer

It seems you're mixing up the syntax of algpseudocode with that of algorithm2e.

With the former package, the syntax is like as follows

\documentclass{article}
\usepackage{amsmath}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithmic}[1]
\For{$j = \operatorname{length}(y_i)$}
   \If{$\{x_j$ and $\neg x_j\}$ in $y_i$}
     \State $a_i = \mathrm{F}$
   \EndIf
\EndFor
\end{algorithmic}

\end{document}

The latter package has no \State command, so I guess you're using algpseudocode.

enter image description here

Note the changes I made to your input to use math mode wherever it's needed.

Related Question