Why do I get a `cannot reference context` error when referencing a custom figure?

That is the problem. The label attaches to the element returned by the function scfigure. Regardless if it’s wrapped in context or if you apply custom styles, in both cases it’s not a figure anymore unfortunately. The problem can be reproduced in a minimal example like this:

#let scfigure(..args) = {
  set text(weight: "bold")
  figure(..args)
}
#scfigure([A], caption: [My A])<fig>
See @fig

Error: Cannot reference styled

So for scfigure to work, it should return just a figure and the rest of the customization needs to be done through a template. That’s the first option. (If you want to smuggle parameters from the function to the template’s action, it would be possible through adding metadata to the caption or the figure body, I think.)

The second option is to change the interface and allow passing the label as an argument instead:

#let scfigure(..args, label: none) = {
  set text(weight: "bold")
  [#figure(..args)#label]
}
#scfigure([A], caption: [My A], label: <fig>)
See @fig

Which is one way to get it to work.

2 Likes