How can I add a label to a figure inside of a box?

Hey there

I’m using meander to reflow text around images. I want for the images to have a caption, so I’m using figure. What I’m doing looks like this:

#let my_box = box(
  width: 60%,
  figure(
    image("my_image"),
    caption: "Some caption"
  )
)

#meander.reflow({
  placed(top + right, my_box)

  container()

  content[
    #lorem(50)
  ]
})

I have the box to be able to control how large it is, such that meander can properly reflow the text around it and I have the figure, because I want a caption to the image.

What I would now like to do is to reference the figure from the content[] block. Usually, I would give the figure a label, like this:

#figure(
  image("my_image"),
  caption: "Some caption"
) <fig>

@fig shows ...

But since the figure is inside of a box, typst expects a comma after the closing parentheses of figure(...), and not a <fig>.

How can I add a label to the figure (or the box or the image, doesn’t really matter), such that I can reference it from within the text meander is reflowing?

Thanks for you help in advance!

1 Like

Hi, welcome to the forum!

The trick is switching from code mode to markup mode with […].

#let my_box = box(
  width: 60%,
  [#figure(
    image("my_image"),
    caption: "Some caption"
  )<fig>]
)
1 Like