How to dynamically get the figure number as a string?

I’m trying to solve this problem:

    #figure(
      {
        let fname = "plot_" + str(figure.counter.display("1")) + ".png"
        image(fname, width: 100%)
      },
      caption: [_Boxplot showing the difference in alpha diversity between responders and non-responders. All treatment timepoints are cumulated._],
    )

I’m open for suggestions. Using typst 0.14.0.

figure does not have a counter member, but counter can access the current figure counter through counter(figure). Since this is also context-aware function, you need to add context somewhere fitting.

#figure(
  context {
    let fname = "plot_" + str(counter(figure.where(kind: image)).display("1")) + ".png"
    image(fname, width: 100%)
  },
  caption: [_Boxplot showing the difference in alpha diversity between responders and non-responders. All treatment timepoints are cumulated._],
)

Also noticed, if you want all captions to have a specific style, you can add a #show figure.caption: ... rule à la How to make captions italic?

Edit: changed counter(figure) to counter(figure.where(kind: image)) to be more specific

I think you will need to use counter(figure.where(kind: image)) and similar, so that you get the right counter, since they are separate for images and tables and so on.

Yes, counter(figure) will keep track of figure elements of all kinds.

Many thanks! I was aware of How to make captions italic? and I was trying work out the solution that @Electron_Wizard showed me but I also add @bluss detail.

Oh, so I still learned something. I had no idea it did anything at all, I thought the kind-specific counters were the only ones.

Maybe it should be mentioned in Figure Function – Typst Documentation or Counter Type – Typst Documentation. But I think in most documents it doesn’t make sense to use.

ah yes, was a little too focused on image only figures :)