Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,20 @@ arrays gets those arrays joined together.

### Documentation

#### `algorithm(inset: 0.2em, indent: 0.5em, vstroke: 0pt + luma(200), line-numbers: true, ..bits)`
#### `algorithm(inset: 0.2em, indent: 0.5em, vstroke: 0pt + luma(200), line-numbers: true, line-numbers-format: x => [#x:], ..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.
If you want to customize line numbers, you can pass a function, that takes a number and returns **content**, to the `line-numbers-format` parameter instead of a default value.

```typst
#algorithm(
inset: 1em, // more spacing between lines
indent: 0.5em, // indentation for the algorithm
vstroke: 0pt + luma(200), // vertical stroke for indentation guide
line-numbers: true, // show line numbers
line-numbers-format: x => [#x:], // change the line numbers format
{ // provide an array
import algorithmic: * // import all names in the array
Assign[$x$][$y$]
Expand All @@ -96,7 +98,7 @@ between lines with the `inset` parameter.
```
![image of the algorithm with three lines of code assigning x to y, y to x, and z to x + y. The inset is set to 1em, the indent to 0.5em](https://raw.githubusercontent.com/typst-community/typst-algorithmic/refs/tags/v1.0.6/tests/algorithm/ref/1.png)

#### `algorithm-figure(title, supplement: "Algorithm", inset: 0.2em, indent: 0.5em, vstroke: 0pt + luma(200), line-numbers: true, ..bits)`
#### `algorithm-figure(title, supplement: "Algorithm", inset: 0.2em, indent: 0.5em, vstroke: 0pt + luma(200), line-numbers: true, line-numbers-format: x => [#x:], ..bits)`

The `algorithm-figure` function is a wrapper around `algorithm` that returns a
figure element of the algorithm. It takes the same parameters as
Expand Down
7 changes: 6 additions & 1 deletion algorithmic.typ
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
indent: 0.5em,
vstroke: 0pt + luma(200),
line-numbers: true,
line-numbers-format: x => [#x:],
horizontal-offset: 1.63640em,
..bits,
) = {
Expand All @@ -86,7 +87,9 @@
}

while line-number <= content.len() {
if line-numbers { grid-bits.push([#line-number:]) }
if line-numbers {
grid-bits.push(line-numbers-format(line-number))
}
grid-bits = grid-bits + indent-content.at(line-number - 1)
grid-bits.push(grid.cell(
content.at(line-number - 1).line-content,
Expand All @@ -110,6 +113,7 @@
indent: 0.5em,
vstroke: 0pt + luma(200),
line-numbers: true,
line-numbers-format: x => [#x:],
horizontal-offset: 1.63640em,
..bits,
) = {
Expand All @@ -122,6 +126,7 @@
inset: inset,
vstroke: vstroke,
line-numbers: line-numbers,
line-numbers-format: line-numbers-format,
horizontal-offset: horizontal-offset,
..bits,
),
Expand Down
4 changes: 4 additions & 0 deletions tests/line-numbers-format/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# generated by tytanic, do not edit

diff/**
out/**
Binary file added tests/line-numbers-format/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions tests/line-numbers-format/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#import "../../algorithmic.typ"
#import algorithmic: *
#set page(margin: .1cm, width: 2cm, height: auto)

#algorithm(
line-numbers-format: num => str(num) + "|",
{
import algorithmic: *
for i in range(1, 5) {
Assign($y$, str(i))
}
},
)