Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I found the algorithmicx package on this page, and tried to compile this example from the pdf file.

\documentclass{article}
\usepackage{algorithmicx}
\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}

With pdflatex example.tex, I got this error.

! LaTeX Error: Environment algorithm undefined.

What's wrong with this?

share|improve this question
add comment

2 Answers

up vote 12 down vote accepted

You need to remove

\usepackage{algorithmicx}

and use the following in your preamble:

\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx

The reason for this is because the algorithmicx package provides (in addition to just defining the algorithmic environment through algorithmicx.sty) a bundle of style files with predefined macros (like \Procedure, \Comment, etc), including algpseudocode.sty. See the package documentation (section 2.1 The package for more on this).

One thing the algorithmicx package does not define is the floating algorithm environment. That is provided by the algorithms bundle. Hence the requirement to include that as well.

share|improve this answer
add comment

You need to remove

\usepackage{algorithmicx}

and use the following in your header.

\usepackage{algorithm}
\usepackage{algorithmic}

I don't why. But what I do know is that the algpseudocode package don't work for me.

I'm using CTeX, hope this would help CTeX user. :P

share|improve this answer
1  
algpseudocode defines the algorithmic environment - that's why. Why does algpseudocode not work for you? –  Werner Jun 5 '12 at 16:00
    
Both didn't work for me. i am using winedit and elsevier.cls Please how o get it right –  user17474 Aug 11 '12 at 9:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.