diff --git a/README.md b/README.md index 8aa7dfc..970d4ea 100644 --- a/README.md +++ b/README.md @@ -68,11 +68,12 @@ 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( @@ -80,6 +81,7 @@ between lines with the `inset` parameter. 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$] @@ -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 diff --git a/algorithmic.typ b/algorithmic.typ index 8ddb5b0..e537b96 100644 --- a/algorithmic.typ +++ b/algorithmic.typ @@ -62,6 +62,7 @@ indent: 0.5em, vstroke: 0pt + luma(200), line-numbers: true, + line-numbers-format: x => [#x:], horizontal-offset: 1.63640em, ..bits, ) = { @@ -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, @@ -110,6 +113,7 @@ indent: 0.5em, vstroke: 0pt + luma(200), line-numbers: true, + line-numbers-format: x => [#x:], horizontal-offset: 1.63640em, ..bits, ) = { @@ -122,6 +126,7 @@ inset: inset, vstroke: vstroke, line-numbers: line-numbers, + line-numbers-format: line-numbers-format, horizontal-offset: horizontal-offset, ..bits, ), diff --git a/tests/line-numbers-format/.gitignore b/tests/line-numbers-format/.gitignore new file mode 100644 index 0000000..40223be --- /dev/null +++ b/tests/line-numbers-format/.gitignore @@ -0,0 +1,4 @@ +# generated by tytanic, do not edit + +diff/** +out/** diff --git a/tests/line-numbers-format/ref/1.png b/tests/line-numbers-format/ref/1.png new file mode 100644 index 0000000..ab55aa2 Binary files /dev/null and b/tests/line-numbers-format/ref/1.png differ diff --git a/tests/line-numbers-format/test.typ b/tests/line-numbers-format/test.typ new file mode 100644 index 0000000..d2a497a --- /dev/null +++ b/tests/line-numbers-format/test.typ @@ -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)) + } + }, +)