This is a package inspired by the LaTeX algorithmicx
package for Typst. It’s useful for writing pseudocode and typesetting it all nicely.
Example:
#import "@preview/algorithmic:1.0.0"
#import algorithmic: algorithm
#algorithm({
import algorithmic: *
Procedure(
"Binary-Search",
("A", "n", "v"),
{
Comment[Initialize the search range]
Assign[$l$][$1$]
Assign[$r$][$n$]
LineBreak
While(
$l <= r$,
{
Assign([mid], FnInline[floor][$(l + r) / 2$])
IfElseChain(
$A ["mid"] < v$,
{
Assign[$l$][$m + 1$]
},
[$A ["mid"] > v$],
{
Assign[$r$][$m - 1$]
},
Return[$m$],
)
},
)
Return[*null*]
},
)
})
This DSL is implemented using the same trick as CeTZ uses: a code block of arrays gets those arrays joined together.
Reference
Documentation
algorithm(inset: 0.2em, ..bits)
This is the main function of the package. It takes a list of arrays and returns a typesetting of the algorithm. You can modify the inset between lines with the inset
parameter.
#algorithm(
inset: 1em, // more spacing between lines
{ // provide an array
import algorithmic: * // import all names in the array
Assign[$x$][$y$]
},
{ // provide another array
import algorithmic: *
Assign[$y$][$x$]
},
{ // provide a third array
import algorithmic: *
Assign[$z$][$x + y$]
}
)
algorithm-figure(title, supplement: "Algorithm", inset: 0.2em, ..bits)
The algorithm-figure
function is a wrapper around algorithm
that returns a figure element of the algorithm. It takes the same parameters as algorithm
, but also takes a title
and a supplement
parameter for the figure.
#let algorithm-figure(title, supplement: "Algorithm", inset: 0.2em, ..bits) = {
return figure(
supplement: supplement,
kind: "algorithm", // the kind of figure
caption: title,
placement: none,
algorithm(inset: inset, ..bits),
)
}
In order to use the algorithm-figure
function, you need to style the figure with the style-algorithm
show rule.
#import algorithmic: algorithm-figure, style-algorithm
#show: style-algorithm // Do not forget!
#algorithm-figure(
"Variable Assignement",
{
import algorithmic: *
Assign[$x$][$y$]
},
)
Control flow
Algorithmic provides basic control flow statements: If
, While
, For
, Else
, ElseIf
, and a IfElseChain
utility.
Statement | Description | Usage | Example |
---|---|---|---|
If | If(condition: content, ..bits) | | ![]() |
ElseIf | ElseIf(condition: content, ..bits) | | ![]() |
Else | Else(..bits) | | ![]() |
While | While(condition: content, ..bits) | | ![]() |
For | For(condition: content, ..bits) | | ![]() |
IfElseChain | IfElseChain(..bits) | | ![]() |
Commands
The package provides a few commands: Function
, Procedure
, Assign
, Return
, Terminate
and Break
.
Users can also define their own commands using both Call(..args)
and Fn(..args)
and their inline versions CallInline
and FnInline
.
#import "../../algorithmic.typ"
#import algorithmic: algorithm
#set page(margin: .1cm, width: 4cm, height: auto)
#algorithm({
import algorithmic: *
let Solve = Call.with("Solve")
let mean = Fn.with("mean")
Assign($x$, Solve[$A$, $b$])
Assign($y$, mean[$x$])
})
Comments
There are three kinds of comments: Comment
, CommentInline
, and LineComment
.
Comment
is a block comment that takes up a whole line.CommentInline
is an inline comment that returns content on the same line.LineComment
places a comment on the same line as a line of code to the right.
Command | Description | Usage | Example |
---|---|---|---|
Function | Function(name, args, ..bits) | | ![]() |
Procedure | Procedure(name, args, ..bits) | | ![]() |
Assign | Assign(var, value) | | ![]() |
Return | Return(value) | | ![]() |
Terminate | Terminate(value) | | ![]() |
Break | Break() | | ![]() |
Comment | Description | Usage | Example |
---|---|---|---|
Comment | Comment(content) | | ![]() |
CommentInline | CommentInline(content) | | ![]() |
LineComment | LineComment(line, comment) | | ![]() |