summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/generic/FAQ-en/html/FAQ-algorithms.html
blob: aaf4f057efb5f9c6dcefd16c56fb8ea1e4bbcc6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<head>
<title>UK TeX FAQ -- question label algorithms</title>
</head><body>
<h3>Typesetting pseudocode in LaTeX</h3>
<p/>There is no consensus on the &lsquo;right&rsquo; way to typeset pseudocode.
Consequently, there are a variety of LaTeX packages to choose from
for producing &aelig;sthetically pleasing pseudocode listings.
<p/>Pseudocode differs from actual program listings in that it lacks
strict syntax and semantics.  Also, because pseudocode is supposed to
be a clear expression of an algorithm it may need to incorporate
mathematical notation, figures, tables, and other LaTeX features
that do not appear in conventional programming languages.
  <a href="FAQ-codelist.html">Typesetting program listings</a> is described
  elsewhere.

<p/>You can certainly create your own environment for typesetting
pseudocode using, for example, the <code>tabbing</code> or
<code>list</code> environments &mdash; it&rsquo;s not difficult, but it may
prove boring.  So it&rsquo;s worth trying the following packages, all
designed specifically for typesetting pseudocode.
<p/>The <i>algorithms</i> bundle (which contains packages
<i>algorithm</i> and <i>algorithmic</i>, both of which are
needed for ordinary use) has a simple interface and produces fairly
nice output.  It provides primitives for statements, which can contain
arbitrary LaTeX commands, comments, and a set of iterative and
conditional constructs.  These primitives can easily be redefined to
produce different text in the output.  However, there is no support
for adding new primitives.  Typesetting the pseudocode itself is
performed in <i>algorithmic</i>; the <i>algorithms</i> package
uses the facilities of the <i>float</i> package to number
algorithms sequentially, enable algorithms to float like figures or
tables, and support including a List of Algorithms in a document&rsquo;s
front matter.
<p/>Packages in the <i>algorithmicx</i> bundle are similar both in
concept and output form to <i>algorithmic</i> but additionally
provide support for adding new keywords and altering the formatting.
It provides the <i>algpseudocode</i> package which is (almost) a
drop-in replacement for <i>algorithmic</i>.  Another package in the
bundle, <i>algpascal</i>, uses Pascal-like keywords, indents
differently from <i>algpseudocode</i>, and puts command arguments
in maths mode instead of text mode.  There is no floating environment
but <i>algorithmicx</i>, like <i>algorithmic</i>, is compatible
with the <i>algorithm</i> package.
<p/>The <i>alg</i> package, like <i>algorithms</i>, offers a
floating algorithm environment with all of the ensuing niceties.
<i>alg</i>, however, can caption its floats in a variety of
(natural) languages.  In addition, <i>alg</i> unlike
<i>algorithms</i>, makes it easy to add new constructs.
<p/>The <i>newalg</i> package has a somewhat similar interface to
<i>algorithms</i>, but its output is designed to mimic the rather
pleasant typesetting used in the book &ldquo;<em>Introduction to Algorithms</em>&rdquo;
by Corman, Leiserson, Rivest and Stein. Unfortunately,
<i>newalg</i> does not support a floating environment or any
customisation of the output.
<p/>&ldquo;<em>Bona fide</em>&rdquo; use of the style of &ldquo;Introduction to
Algorithms&rdquo; may be achieved with Cormen&rsquo;s own <i>clrscode</i>:
this is the package as used in the second edition of the book.
<p/>Similarly, the style of 
&ldquo;<em>Combinatorial Algorithms: Generation, Enumeration and Search</em>&rdquo;
is supported by the <i>pseudocode</i> package, written by the
authors of the book.  It has the common &lsquo;Pascal-like&rsquo; style, and has
some interesting constructs for what one thinks of as Pascal blocks.
<p/>The <i>algorithm2e</i> is of very long standing, and is widely used
and recommended.  It loads the <i>float</i> package to provide the
option of floating algorithm descriptions, but you can always use the
&ldquo;<code>H</code>&rdquo; option of <i>float</i> to have the algorithm appear
&ldquo;where you write it&rdquo;.
<p/>The usage of the <i>program</i> package is a little different from
that of the other packages.  It typesets programs in maths mode
instead of text mode; and linebreaks are significant.
<i>program</i> lacks a floating environment but does number
algorithms like <i>alg</i> and <i>algorithms</i>.  Customisation
and extension are not supported.  Documentation of the
<i>program</i> package (such as it is) appears in a file
<i>program.msg</i> in the distribution.
<p/>None of the above are perfect.  The factors that should influence your
choice of package include the output style you prefer, how much you
need to extend or modify the set of keywords, and whether you require
algorithms to float like figures and tables.















<dl>
<dt><tt><i>algorithm2e.sty</i></tt><dd><a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/algorithm2e.zip">macros/latex/contrib/algorithm2e</a> (or <a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/algorithm2e/">browse the directory</a>)
<dt><tt><i>algorithmicx bundle</i></tt><dd><a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/algorithmicx.zip">macros/latex/contrib/algorithmicx</a> (or <a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/algorithmicx/">browse the directory</a>)
<dt><tt><i>algorithms bundle</i></tt><dd><a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/algorithms.zip">macros/latex/contrib/algorithms</a> (or <a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/algorithms/">browse the directory</a>)
<dt><tt><i>alg.sty</i></tt><dd><a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/alg.zip">macros/latex/contrib/alg</a> (or <a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/alg/">browse the directory</a>)
<dt><tt><i>clrscode.sty</i></tt><dd><a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/clrscode.zip">macros/latex/contrib/clrscode</a> (or <a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/clrscode/">browse the directory</a>)
<dt><tt><i>float.sty</i></tt><dd><a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/float.zip">macros/latex/contrib/float</a> (or <a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/float/">browse the directory</a>)
<dt><tt><i>newalg.sty</i></tt><dd><a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/newalg.zip">macros/latex/contrib/newalg</a> (or <a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/newalg/">browse the directory</a>)
<dt><tt><i>program.sty</i></tt><dd><a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/program.zip">macros/latex/contrib/program</a> (or <a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/program/">browse the directory</a>)
<dt><tt><i>pseudocode.sty</i></tt><dd><a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/pseudocode.zip">macros/latex/contrib/pseudocode</a> (or <a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/pseudocode/">browse the directory</a>)
</dl>
<p/><p>This question on the Web: <a href="http://www.tex.ac.uk/cgi-bin/texfaq2html?label=algorithms">http://www.tex.ac.uk/cgi-bin/texfaq2html?label=algorithms</a>
</body>