How to reference a figure from a variable?

I’m trying to refer to my figure, but the figure label name is stored in a variable.

If I did not have a mix of tables and figures in my document counter(figure).at(label("sample_c")) would work. However as I have a table first this is included in the counter.

Here is my attempts:

#figure(
  table(columns:2,[test],[test]),
  caption: [A table],
  kind: table
) <table_1>

#let samples = ((tag:"a"),(tag:"b"),(tag:"c"),)
#for s in samples {
  block([
  #figure(
    [Some figure],
    caption: [Statistics for measurement #s.tag.],
    ) #label("sample_" + s.tag)        
  ])
}

// The below of cause works as expected
@sample_c \
@table_1

// But if I have the lable name in a variable I struggle to get the correct figure number
// The counter does not differentiate between tables and figures.
#context locate(label("sample_c")).position()

#context counter(figure).at(label("sample_c")) // The figure number is 3 - Returns 4 as the table is counted as well

Hi @Stine,

figure includes all kinds of figures, the built-in kinds image, table, and raw, and custom ones. Typst automatically uses the build in ones: if you wrap a table with a figure, it automatically creates a figure of kind table. By default, and in your case, the 3 figures generated by the for loop are of kind image.

You can get the correct counter with .where(kind: image).

#context counter(figure.where(kind: image)).at(label("sample_c"))

If you do something special with your figures I would recommend to use a custom kind.

Thanks a lot for your time, this was just what I needed.