Math & Science\caption outside float in algorithm package

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
vivek9837
Posts: 1
Joined: Mon Apr 04, 2016 12:55 pm

\caption outside float in algorithm package

Post by vivek9837 »

I am trying to write the following algorithm using latex

Code: Select all

\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}
but i am getting the following error

Code: Select all

./test2.tex:6:\caption outside float. \caption
Kindly help

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

\caption outside float in algorithm package

Post by Johannes_B »

Welcome, the code you posted works fine. Maybe you forgot an algorithm environment in your real document.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply