#Algorithm package where we can reference lines?

19 messages · Page 1 of 1 (latest)

civic dust
#

I'm searching for an algorithm package like algo/lovelace/typst-algorithmic, but where we can reference line numbers, like in LaTeX with algorithm2e. For instance some line in the algorithm would be i <- i+1; <line:increment> and then we would reference it with @line:increment which would output something like "line 3".

I think that the previously cited packages do not have this feature, because the labels would be defined inside the scope of the algorithm, and therefore cannot be referenced outside. Maybe this could be resolved using show rules, which would expose the context of the algorithm to the outside, like with templates?

A second technical hurdle would be to define a custom label type for line numbers. I know that you could do theoretically do that using a show rule as explained in the docs (https://typst.app/docs/reference/model/ref/) but I don't see what would be the code used to detect that we are referencing an algorithm line, and how to retrieve the line number.

If this is not currently possible, do you have some ideas on how to resolve the above challenges?

Typst

Documentation for the ref function.

wanton vessel
#

I'm actually quite positive this wouldn't be too difficult for the package authors to implement, it's just not something they've yet thought about. On their respective typst universe pages will be a link to their repository, I'd suggest making an issue on one or more where appropriate

civic dust
#

I will do that probably, but I'm curious, how would you solve the 2 problems I mentioned?

  1. reference lines outside the scope of the algorithm
  2. detect in the custom show rule for line references that we are referencing an algo line, and retrieve the line number
#

On problem 2: I don't even know if this is possible to do this in typst. There are 2 options for the algorithm package:

  1. if the algorithm is given in script mode (like with typst-algorithmic), then you cannot label lines as when you do you have the following error:
#

?r

#let f(v) = [hello #v]

#{
  f[world]; <my-label>
}
zealous sirenBOT
civic dust
#
  1. if the algorithm is given in text mode (like with algo and lovelace), then you cannot label lines either as you have the following error:
#

?r

hello-world <my-label>

@my-label
zealous sirenBOT
wanton vessel
#

My thought was that you'd reference by line number, instead of referencing the content of that line

#

Each content representing the line number is wrapped in a figure with a kind that is then recognized in a show rule for rendering that reference

lone shoal
lone shoal
# civic dust ?r ``` hello-world <my-label> @my-label ```

a reference with @ syntax only works with a very limited range of elements, since it has to figure out what to replace itself with (e.g Figure 2, Section 3 and so on). It can't do that for arbitrary text or content.

in this case you can write #link(<my-label>)[Click me!] instead

civic dust
# lone shoal a reference with `@` syntax only works with a very limited range of elements, si...

I see, but can't you define a new reference type, like with the cleveref package in LaTeX? For instance:

  1. you create a new counter for line numbers: #let _algo-line-nb = counter("_algo-line-nb")
  2. you define a show rule to display "line "+the counter value for the references to labels that are placed after an #_algo-line-nb.step()
  3. you place a label after a step: #_algo-line-nb.step() #_algo-line-nb.display() $i <- i+1$; <line:increment> \
  4. you reference the line: @line:increment, which outputs line 3

This can be done in LaTeX, but can this also be done in typst?

lone shoal
#

for the counter you'd need the package you're using to support that somehow, or manually insert the counter by urself

#

and then u can access its value within the ref show rule using the referred element's location

civic dust
#

I think I will try to write my own algorithmic package, this is why I want to know how to proceed