[Tex/LaTex] Problems with \EndIf and \EndFor

algorithmspseudocode

I'm using the algpseudocode package and I want to write a pseudocode. This is my code

\documentclass[conference]{IEEEtran}

\usepackage[spanish, es-tabla]{babel}
\usepackage{graphicx, times, amsmath, colortbl, psfrag}
\usepackage[ruled, vlined, english, boxed, linesnumbered, lined]{algorithm2e}
\usepackage{algpseudocode}
\usepackage{multirow}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{float}

\begin{document}
...
\begin{algorithm}
    \caption{Buscar elemento en una lista}
    \KwData{$lista[1,...,x_n]$: lista en la que se va a buscar, $elem$: elemento que se va a buscar}
    \KwResult{$encontrado$: booleano que indica si se encontr\'o el elemento}
    \begin{algorithmic}[1]
        \Procedure{Buscar}{$lista$, $elem$}
            \State $encontrado \gets False$
            \For{i}{1}{n}
                \If{$lista[i]=elem$}
                    \State $encontrado \gets True$
                \EndIf
            \EndFor
            \Return $encontrado$
        \EndProcedure
    \end{algorithmic}
\end{algorithm}
...
\end{document}

but I get the following errors when I try to compile it

! Missing number, treated as zero.
<to be read again> 
               \ALG@b@2@EndIf@0 
l.113           \EndIf
?

! Missing number, treated as zero.
<to be read again> 
               \ALG@b@2@EndFor@0 
l.114           \EndFor
?

Does anyone know how can I compile it?

Thanks for answering.

Best Answer

You are using incompatible commands from the packages algorithm2e and algpseudocode; you can use only algpseudocode and replace \KwData, \KwResult with \Require, \Ensure:

\documentclass[conference]{IEEEtran}
\usepackage[spanish, es-tabla]{babel}
\usepackage{amsmath}
\usepackage{algorithm} 
\usepackage{algpseudocode}  


\begin{document}

\begin{algorithm}
    \caption{Buscar elemento en una lista}
    \begin{algorithmic}[1]
\Require $lista[1,...,x_n]$: lista en la que se va a buscar, $elem$: elemento que se va a buscar
\Ensure $encontrado$: booleano que indica si se encontr\'o el elemento
        \Procedure{Buscar}{$lista$, $elem$}
            \State $encontrado \gets False$
            \For{i}{1}{n}
                \If{$lista[i]=elem$}
                    \State $encontrado \gets True$
                \EndIf
            \EndFor
            \Return $encontrado$
        \EndProcedure
    \end{algorithmic}
\end{algorithm}

\end{document}

enter image description here

Notice, however, that the IEEEtran HowTo document recommends not to use algorithm from the algorithm package:

B. Algorithms

IEEE publications use the figure environment to contain algorithms that are not to be a part of the main text flow. Peter Williams’ and Rogerio Brito’s algorithmic.sty package [24] or Szász János’ algorithmicx.sty package [25] (the latter is designed to be more customizable than the former) may be of help in producing algorithm-like structures (although authors are of course free to use whatever LaTeX commands they are most comfortable with in this regard). However, do not use the floating algorithm environment of algorithm.sty (also by Williams and Brito) or algorithm2e.sty (by Christophe Fiorio) as the only floating structures IEEE uses are figures and tables. Furthermore, IEEEtran will not be in control of the (non-IEEE) caption style produced by the algorithm.sty or algorithm2e.sty float environments.

In this case, as recommended by the class, you can enclose your algorithm inside a figure environment:

\documentclass[conference]{IEEEtran}
\usepackage[spanish, es-tabla]{babel}
\usepackage{amsmath}
\usepackage{algpseudocode}  

\begin{document}

\begin{figure}
    \caption{Pseudocódigo para el algoritmo que busca un elemento en una lista}
    \begin{algorithmic}[1]
\Require $lista[1,...,x_n]$: lista en la que se va a buscar, $elem$: elemento que se va a buscar
\Ensure $encontrado$: booleano que indica si se encontr\'o el elemento
        \Procedure{Buscar}{$lista$, $elem$}
            \State $encontrado \gets False$
            \For{i}{1}{n}
                \If{$lista[i]=elem$}
                    \State $encontrado \gets True$
                \EndIf
            \EndFor
            \Return $encontrado$
        \EndProcedure
    \end{algorithmic}
\end{figure}

\end{document}

enter image description here