[Tex/LaTex] \caption outside float. Algorithms

algorithm2ecaptions

I'm sorry if this is repeated but I'm not able to find my answer.
I am using the following code. But I receive the error:

! LaTeX Error: \caption outside float.

\usepackage[algo2e]{algorithm2e}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Euclid's algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

Best Answer

Your code uses nothing involving the algorithm2e package and the use of the packages conflict with one another. Instead add the algorithm package to your preamble.

enter image description here

\documentclass{article}
\usepackage{algorithm,algpseudocode}

\begin{document}
\begin{algorithm}
  \caption{Euclid's algorithm}\label{euclid}
  \begin{algorithmic}[1]
    \Procedure{Euclid}{$a,b$}\Comment{The gcd of $a$ and $b$}
      \State $r \gets a \bmod b$
      \While{$r \neq 0$}\Comment{We have the answer if $r$ is $0$}
        \State $a \gets b$
        \State $b \gets r$
        \State $r \gets a \bmod b$
      \EndWhile\label{euclidendwhile}
      \State \textbf{return} $b$\Comment{The gcd is $b$}
    \EndProcedure
  \end{algorithmic}
\end{algorithm}

\end{document}
Related Question