| 1 |
# pseudo.sty |
| 2 |
|
| 3 |
The **pseudo** package permits writing pseudocode without much fuss and with |
| 4 |
quite a bit of configurability. Its main environment combines aspects of |
| 5 |
`enumeration`, `tabbing` and `tabular` for nonintrusive line numbering, |
| 6 |
indentation and highlighting, and there is functionality for typesetting |
| 7 |
common syntactic elements such as keywords, identifiers and comments. |
| 8 |
|
| 9 |
The package is written by [Magnus Lie Hetland](mailto:[email protected]) and |
| 10 |
released under the [MIT license](LICENSE). |
| 11 |
|
| 12 |
## Using pseudo |
| 13 |
|
| 14 |
You can find more detailed instructions in [the |
| 15 |
documentation](doc/pseudo.pdf), but it's really quite simple to get started. |
| 16 |
The package is [available via CTAN](https://ctan.org/pkg/pseudo), and so is |
| 17 |
part of several TeX distributions. If the newest version isn't in yours, try |
| 18 |
to run an update. In TeX Live, for example, you could run use the [TeX Live |
| 19 |
Utility](http://amaxwell.github.io/tlutility/) (part of distributions such as |
| 20 |
[MacTeX](https://tug.org/mactex/)). Alternatively, you can just download the |
| 21 |
[pseudo.sty](pseudo.sty) file and drop it where your `latex` can find it |
| 22 |
(possibly just in the same directory as your document). |
| 23 |
|
| 24 |
Assuming you want numbered lines (the default) and have most text set as |
| 25 |
keywords (set by the `kw` switch), you simply import **pseudo** using |
| 26 |
|
| 27 |
```tex |
| 28 |
\usepackage[kw]{pseudo} |
| 29 |
``` |
| 30 |
|
| 31 |
and then set your pseudocode in a `pseudo` environment. Lines are terminated |
| 32 |
by the normal `\\` command, which may be extended with one or more pluses or |
| 33 |
minuses to indicate a change in indentation level. You suppress the automatic |
| 34 |
numbering of the following line using a star (i.e., either `\\*`, or |
| 35 |
`\begin{pseudo}*`), and can typeset a procedure header using `\hd{Name}(...)`. |
| 36 |
For [example](https://mipmip.org/tidbits/pasa.pdf): |
| 37 |
|
| 38 |
```tex |
| 39 |
\begin{pseudo}* |
| 40 |
|
| 41 |
\hd{Backward}(V, E, v, i) \\ |
| 42 |
|
| 43 |
$v.\id{label} = i$ \\ |
| 44 |
|
| 45 |
for $(u,v)\in E$ \\+ |
| 46 |
if $0 < v.\id{label} < i$ \\+ |
| 47 |
\pr{Backward}(V, E, u, i) \\-- |
| 48 |
|
| 49 |
for $(u,v)\in E$ \\+ |
| 50 |
if $u.\id{label} \== 0$ \\+ |
| 51 |
\pr{Backward}(V, E, u, i+1) |
| 52 |
|
| 53 |
\end{pseudo} |
| 54 |
``` |
| 55 |
|
| 56 |
This produces: |
| 57 |
|
| 58 |
<img src="doc/fig/readmefig.svg"/> |
| 59 |
|
| 60 |
There is also a starred version of the environment (`pseudo*`), which |
| 61 |
is unnumbered. |
| 62 |
|
| 63 |
The commands `\pr` and `\id` indicate procedure calls and identifiers. |
| 64 |
If we hadn't used the `kw` option, the main text would not be set as keywords, |
| 65 |
but as plain text. We could then use `\kw` to mark up keywords. Some of these |
| 66 |
may be used a lot, so I might want to declare a shortcut. For example, if |
| 67 |
I use |
| 68 |
|
| 69 |
```tex |
| 70 |
\DeclarePseudoKeyword \While {while} |
| 71 |
``` |
| 72 |
|
| 73 |
I'd be able to use `\While` rather than `\kw{while}` later. Several such |
| 74 |
formatting and declaration commands exist: |
| 75 |
|
| 76 |
| Type of text | Command | Declaration | |
| 77 |
| ------------ | ------------------- | ----------------------------------------------- | |
| 78 |
| Keywords | `\kw{while}` | `\DeclarePseudoKeyword \While {while}` | |
| 79 |
| Constants | `\cn{false}` | `\DeclarePseudoConstant \False {false}` | |
| 80 |
| Identifiers | `\id{rank}` | `\DeclarePseudoIdentifier \Rank {rank}` | |
| 81 |
| Strings | `\st{Hello!}` | `\DeclarePseudoString \Hello {Hello!}` | |
| 82 |
| Procedures | `\pr{Euclid}(a, b)` | `\DeclarePseudoProcedure \Euclid {Euclid}` | |
| 83 |
| Functions | `\fn{length}(A)` | `\DeclarePseudoFunction \Length {length}` | |
| 84 |
| Comments | `\ct{Important!}` | `\DeclarePseudoComment \Important {Important!}` | |
| 85 |
|
| 86 |
For normal text (e.g., when using the `kw` switch), there is also `\tn` (along |
| 87 |
with `\DeclarePseudoNormal`). |
| 88 |
|
| 89 |
Since the short names of these commands are prone to collisions with other |
| 90 |
packages, **pseudo** won't insist on using them. If you import **pseudo** |
| 91 |
*after* some other package that already uses, say, `\id`, then **pseudo** |
| 92 |
won't define it. All of these are still available with the `pseudo` prefix, |
| 93 |
however (e.g., `\pseudoid`), and the declaration commands still work. |
| 94 |
|
| 95 |
Beyond the styling of text, there are also the `\==` and `\..` commands |
| 96 |
(also written `\eqs` and `\dts`) for typesetting double equals sign and double |
| 97 |
dots. |
| 98 |
|
| 99 |
This covers most of the core functionality, but there are also more obscure |
| 100 |
features (such as dimming or highlighting lines), support for algorithm floats |
| 101 |
(and inline boxes) with customizable numbering and appearance, and a host of |
| 102 |
configuration keys for getting the pseudocode looking more like you'd want it |
| 103 |
to. For details, consult [the documentation](doc/pseudo.pdf). |