platformer / typst-algorithms

MIT License
141 stars 4 forks source link

How to: reference with @ #16

Closed xy9485 closed 11 months ago

xy9485 commented 11 months ago

I find it's a pleasing experience to write pseudo code with this package. However, I find myself stuck with how to reference an algorithm using "@".I tried below to label my algorithm but it doesn't work with error: cannot reference sequence. I label my algorithm with @algo1[Algorithm]as below:

#algo(
  title: [                    // note that title and parameters
    #set text(size: 15pt)     // can be content
    #emph(smallcaps("Curiosity driven exploration for molecule generation"))
  ],
  parameters: ([#math.italic("n")],),
  comment-prefix: [#sym.triangle.stroked.r ],
  comment-styles: (fill: rgb(100%, 0%, 0%)),
  indent-size: 15pt,
  indent-guides: 1pt + gray,
  row-gutter: 5pt,
  column-gutter: 5pt,
  inset: 5pt,
  stroke: 2pt + black,
  fill: none,
)[
"content of algorithm"
] <algo1>

I'm a beginner of typst, this is what I learned to achieve reference. I'm not sure if it also works with returns of #algo. Otherwise is there any other workaround to refer an algorithm as we do in Latex? I just need at least a basic way to refer my own algorithms.

platformer commented 11 months ago

You are correct, algo returns "plain content", so you cannot directly attach a label. You can get around this by manually wrapping it in a figure:

#let my-algo(...) = {
  #figure(algo(...), kind: "algorithm")
}

#my-algo(...)[...] <algo1>

In the future, I will do a complete rewrite of the package, and one of the changes I'll make is have algo output a figure to bring it in line with similar packages. Currently, I'm waiting for some critical features to land in Typst so I can implement all my planned improvements at once.