Search code examples
algorithmlatex

Latex algorithm. Can't get it to work


Im trying to write an algorithm using the algorithm package, but when I use any keyword (if, while, state, etc) it won't compile

Here's what I type in (snippet)

\documentclass[9pt]{article}
\usepackage{algorithm}
\begin{document}

\begin{algorithm}
\caption{Calculate $A_{nxn}$}
\label{Algorithm 1}

\IF{$n<0$}
\STATE $x \leftarrow X$
\ENDIF


\end{algorithm}

\end{document}

geometry auto-detecting driver geometry detected driver: pdftex (/usr/share/texmf/tex/context/base/supp-pdf.mkii [Loading MPS to PDF converter (version 2006.09.02).] ) [1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}] [2] ! Undefined control sequence. l.94 \IF {$n<0$} ? q

update

These are all the packages i'm using.

\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{url}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{algorithm}

\usepackage{multicol}
\usepackage{algorithmic}

\usepackage{fancyhdr}

Any thoughts? I'm kind of lost.

Thanks in advance.


Solution

  • Take a peek here: http://en.wikibooks.org/wiki/LaTeX/Algorithms_and_Pseudocode

    Are you missing a \begin{algorithmic} declaration, perhaps? From the examples, it appears that \begin{algorithm} is a container for the actual algorithmic environment where the work takes place...

    \documentclass[9pt]{article} 
    \usepackage{algorithm} 
    \begin{document} 
    
    \begin{algorithm} 
    
    \caption{Calculate $A_{nxn}$} 
    \label{Algorithm 1} 
    
    \begin{algorithmic}
    
    \IF{$n<0$} 
    \STATE $x \leftarrow X$ 
    \ENDIF 
    
    \end{algorithmic}
    \end{algorithm} 
    
    \end{document}
    

    That wiki page also includes a link to the official algorithms manual.