Math-Linux.com

Knowledge base dedicated to Linux and applied mathematics.

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}

All the versions of this article: <English> <français>


We must use the following packages

\usepackage{algorithm}
\usepackage{algorithmic}

Here is an exemple:

\begin{algorithm}
\caption{Calculate $y = x^n$}
\begin{algorithmic}
\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$
\ELSE
\STATE $X \leftarrow x$
\STATE $N \leftarrow n$
\ENDIF
\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$
\ENDIF
\ENDWHILE
\end{algorithmic}
\end{algorithm}

Here are useful commands

Single line statements

\STATE <text>

If-statements

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

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

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

For-loops

There are two forms

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

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

While-loops

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

Repeat until condition

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

Infinite loops

\LOOP <text> \ENDLOOP

Precondition

\REQUIRE <text>

Postcondition

\ENSURE <text>

Returning variables

\RETURN <text>

Printing variables

\PRINT <text>

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

Also in this section

  1. How to write number sets N Z D Q R C with Latex: \mathbb, amsfonts and \mathbf
  2. How to write angle in latex langle, rangle, wedge, angle, measuredangle, sphericalangle
  3. Latex numbering equations: leqno et fleqn, left,right
  4. How to write a vector in Latex ? \vec,\overrightarrow
  5. Latex how to insert a blank or empty page with or without numbering \thispagestyle,\newpage,\usepackage{afterpage}
  6. Latex arrows
  7. Latex natural numbers
  8. Latex real numbers
  9. Latex binomial coefficient
  10. Latex overset and underset
  11. LateX Derivatives, Limits, Sums, Products and Integrals
  12. Latex absolute value
  13. Latex piecewise function
  14. Latex how to hide page number
  15. How to get dots in Latex \ldots,\cdots,\vdots and \ddots
  16. How to write algorithm and pseudocode in Latex ?\usepackage{algorithm},\usepackage{algorithmic}
  17. How to display formulas inside a box or frame in Latex ? \boxed
  18. Latex how to write underscore
  19. How to write table in Latex ? begin{tabular}...end{tabular}
  20. Horizontal and vertical curly braces: \left\{,\right\},\underbrace{} and \overbrace{}
  21. Latex norm for vector and sum
  22. Latex infinity symbol
  23. Latex how to write percent
  24. Latex how to write a fraction
  25. Latex how to write bar
  26. Latex how to write text in math mode
  27. Latex rational numbers
  28. Latex quaternion numbers
  29. Latex complex numbers
  30. Latex indicator function
  31. Latex plus or minus symbol
  32. Latex symbol for all x
  33. Latex symbol exists
  34. Latex symbol not exists
  35. How to write matrices in Latex ? matrix, pmatrix, bmatrix, vmatrix, Vmatrix