Home > Latex > FAQ > Latex - FAQ > How to write algorithm and pseudocode in Latex ?\usepackagealgorithm,\usepackage

How to write algorithm and pseudocode in Latex ?\usepackage{algorithm},\usepackage{algorithmic}

Friday 31 December 2010, by Nadir SOUALEM

Keywords: , , .

All the versions of this article: [English] [français]

We must use the following packages


Here is an exemple:

\caption{Calculate $y = x^n$}
\REQUIRE $n \geq 0 \vee x \neq 0$
\ENSURE $y = x^n$
\STATE $y \leftarrow 1$
\IF{$n < 0$}
\STATE $X \leftarrow 1 / x$
\STATE $N \leftarrow -n$
\STATE $X \leftarrow x$
\STATE $N \leftarrow n$
\WHILE{$N \neq 0$}
\IF{$N$ is even}
\STATE $X \leftarrow X \times X$
\STATE $N \leftarrow N / 2$
\ELSE[$N$ is odd]
\STATE $y \leftarrow y \times X$
\STATE $N \leftarrow N - 1$

Here are useful commands

Single line statements

\STATE <text>


\IF{<condition>} <text> \ENDIF

\IF{<condition>} <text> \ELSE <text> \ENDIF

\IF{<condition>} <text> \ELSIF{<condition>} <text> \ELSE <text> \ENDIF


There are two forms

\FOR{<condition>} <text> \ENDFOR

\FORALL{<condition>} <text> \ENDFOR


\WHILE{<condition>} <text> \ENDWHILE

Repeat until condition

\REPEAT <text> \UNTIL{<condition>}

Infinite loops



\REQUIRE <text>


\ENSURE <text>

Returning variables

\RETURN <text>

Printing variables

\PRINT <text>

Note: Due to a bug, the algorithmic package is not compatible with hyperref.

Forum posts

Any message or comments?


This forum is moderated before publication: your contribution will only appear after being validated by an administrator.

Who are you?
Your post
  • To create paragraphs, just leave blank lines.

If you want to help Math-Linux.com project , Send your contributions to our team. Thanks !!!