LaTeX/Algorithms and Pseudocode
From Wikibooks, the open-content textbooks collection
LaTeX has a variety of packages that can help to format algorithms and "pseudocode". (Pseudocode is a loose way of expressing an algorithm in a way which resembles typical computer programming languages. It doesn't have much of a formal standardisation but uses only very common programming structures such as "if-else" blocks and "for" loops.)
Contents |
[edit] Typesetting using the algorithmic package
The algorithmic environment provides a number of popular constructs for algorithm designs. The command \begin{algorithmic} can be given the optional argument of a positive integer, which if given will cause line numbering to occur at multiples of that integer. E.g. \begin{algorithmic}[5] will enter the algorithmic environment and number every fifth line.
Below is an example of typesetting a basic algorithm using the algorithmic package (remember to add the \usepackage{algorithmic} statement to your document preamble):
\begin{algorithmic} \IF {$i\geq maxval$} \STATE $i\gets 0$ \ELSE \IF {$i+k\leq maxval$} \STATE $i\gets i+k$ \ENDIF \ENDIF \end{algorithmic}
The LaTeX source can be written to a format familiar to programmers so that it is easy to read. This will not, however, affect the final layout in the document.
There are several constructs provided by algorithmic detailed below
[edit] Single line statements
- \STATE <text>
- A simple statement, e.g. for setting a variable
\begin{algorithmic} \STATE i=0 \end{algorithmic}
would produce
i = 0
[edit] If-statements
There are three forms of this construct
- \IF{<condition>} <text> \ENDIF
- \IF{<condition>} <text> \ELSE <text> \ENDIF
- \IF{<condition>} <text> \ELSIF{<condition>} <text> \ELSE <text> \ENDIF
- The third form accepts as many \ELSIF{} clauses as required.
[edit] For-loops
There are two forms
- \FOR{<condition>} <text> \ENDFOR
- \FORALL{<condition>} <text> \ENDFOR
- A traditional "for" loop. The method of iteration is usually described in the first argument,
e.g.
\FOR{$i$ = 1 to 10} \STATE $i \leftarrow i + 1$ \ENDFOR
[edit] While-loops
- \WHILE{<condition>} <text> \ENDWHILE
[edit] Repeat until condition
- \REPEAT <text> \UNTIL{<condition>}
[edit] Infinite loops
- \LOOP <text> \ENDLOOP
[edit] Precondition
- \REQUIRE <text>
[edit] Postcondition
- \ENSURE <text>
[edit] Returning variables
- \RETURN <text>
[edit] Printing variables
- \PRINT <text>
- This is included because it is used so frequently it is considered an operation in its own right.
[edit] Comments
- \COMMENT{<text>}
Note that you can not use \COMMENT as the first statement of any closed structure, such as \IF..\ENDIF, \FOR..\ENDFOR, \FORALL..\ENDFORALL, \WHILE..\ENDWHILE, and \begin{algorithmic}..\end{algorithmic}. An error "LaTeX Error: Something's wrong--perhaps a missing \item" will be reported (It does not make much sense). There are two workaounds:
1. Use \STATE \COMMENT{<text>}.
2. Use the optional arguments in those closed structures.
For example, \WHILE[<comment-text>]{<condition>}
[edit] The algorithm environment
It is often useful for the algorithm produced by algorithmic to be "floated" to the optimal point in the document to avoid it being split across pages. The algorithm environment provides this and a few other useful features. Include it by adding the
\usepackage{algorithm} to your document's preamble. It is entered into by
\begin{algorithm} \caption{<your caption for this algorithm>} \label{<your label for references later in your document>} \begin{algorithmic} <algorithmic environment> \end{algorithmic} \end{algorithm}
[edit] Algorithm numbering
The default numbering system for the algorithm package is to number algorithms sequentially. This is often not desirable, particularly in large documents where numbering according to chapter is more appropriate. The official documentation is not very clear on how to change this, but it can be done by inserting a \numberwithin{} into the preamble:
\usepackage{algorithmic} \usepackage{algorithm} \numberwithin{algorithm}{chapter} % <--- chapter, section etc. depending on what is required
When using hyperref and referencing algorithms latex gives an error: ! Undefined control sequence. <argument> algorithm.\theHalgorithm This can be solved adding at the preamble:
\newcommand{\theHalgorithm}{\arabic{algorithm}}
[edit] An example from the manual
This is an example taken from the manual ( official manual, p.7)
\begin{algorithm} % enter the algorithm environment \caption{Calculate $y = x^n$} % give the algorithm a caption \label{alg1} % and a label for \ref{} commands later in the document \begin{algorithmic} % enter the algorithmic environment \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}
- More information about all possible commands available at the project page
- http://developer.berlios.de/docman/?group_id=3442
- The official manual is located at
- http://developer.berlios.de/docman/display_doc.php?docid=800&group_id=3442
[edit] Code formating using the Listings package
A complete reference manual can be found here: http://tug.ctan.org/tex-archive/macros/latex/contrib/listings/listings.pdf
This is a basic example for some Pascal code:
\documentclass{article} \usepackage{listings} % Include the listings-package \begin{document} \lstset{language=Pascal} % Set your language (you can change the language for each code-block optionally) \begin{lstlisting} % Start your code-block for i:=maxint to 0 do begin { do nothing } end; Write(’Case insensitive ’); WritE(’Pascal keywords.’); \end{lstlisting} \end{document}
[edit] References
- The official manual for the algorithms package, Rogério Brito (2005), http://developer.berlios.de/docman/display_doc.php?docid=800&group_id=3442
| Previous: Indexing | Index | Next: Letters |

