How to auto-label a figure?

Many thanks @Y.D.X. I just needed to tweak a few things but it’s now working.

// Auto-figure for images: builds "prefix + <n> + ext" from the image-figure counter.
#let autoimagefig(
  caption: none,
  width: 100%,
  prefix: "plot_",
  ext: ".png",
  placement: none,
  gap: 0.65em,
) = context {
  let n = counter(figure.where(kind: image)).get().first() + 1 // <-- main change
  let fname = prefix + str(n) + ext
  let label-name = prefix + str(n)

  // Currently, labels can only be attached to elements in markup mode, not in code mode. This might change in the future.
  [#figure(
      image(fname, width: width),
      caption: caption,
      kind: image,
      placement: placement,
      gap: gap,
    )#label(label-name)]
}
1 Like