How can I access the figure's supplement from inside the figure itself?

How can I access the figure’s supplement from inside the figure itself?

I am trying to make a theorems-like environment. Here is a small example of what I’m doing:

// file name: lib.typ

#let translation = (
  definition: (en: "Definition", fr: "Définition", ko: "정의", ja: "定義", zh: "定义"),
)

#let definition(name: [], color: rgb("#0077ff19"), it) = figure(
  kind: "Definition",
  supplement: context [#translation.definition.at(text.lang, default: "Definition")],
  numbering: num => context [#counter(heading).display("1.")#numbering("a", num)],
  caption: [],
  block(
    width: 100%,
    inset: 1em,
    fill: color,
  )[
    #align(left)[
      #context [
        #figure.supplement
        #counter(heading.where(level:1)).display("1.")
        #counter(figure.where(kind:"Definition")).display("a")
        ]
        #h(1em) #name #linebreak()
        ]

    #align(left)[#it]
  ],
)
// import in another file
#import "./lib.typ":*
#set heading(numbering:"1.")

= #lorem(1)
= #lorem(1)

#definition[ some texts ] <def1>

#definition[ more texts and @def1 ] <def2>

outputs

problem

The figure’s supplement does not show properly. Has anyone encountered similar problems before? Please provide some advices.

A simple way is:

#let definition(name: [], color: rgb("#0077ff19"), it) = {
  let _supplement = context [#translation.definition.at(text.lang, default: "Definition")]
  figure(
    kind: "Definition",
    supplement: _supplement,
    numbering: (..num) => context [#counter(heading).display("1.")#numbering("a", ..num)],
    caption: [], // ???
    block(
      width: 100%,
      inset: 1em,
      fill: color,
    )[
      #align(left)[
        #context [
          #_supplement
          #counter(heading.where(level: 1)).display("1.")#counter(figure.where(kind: "Definition")).display("a")
        ]
        #h(1em) #name #linebreak()
      ]

      #align(left)[#it]
    ],
  )
}

Hi! I believe the proper way is to put configurations in show rules, so that they are globally available.

By the way, I also unify the numbering logic and correct cross-chapter references.

#let translation = (
  definition: (
    en: "Definition",
    fr: "Définition",
    ko: "정의",
    ja: "定義",
    zh: "定义",
  ),
)

#show figure.where(kind: "definition"): set figure(
  supplement: context translation.definition.at(
    text.lang,
    default: translation.definition.en,
  ),
  numbering: num => numbering(
    "1.a",
    counter(heading).get().first(),
    num,
  ),
  // If you really want to keep the caption, uncomment the following line.
  // caption: [],
)

#let definition(name: [], color: rgb("#0077ff19"), body) = figure(
  kind: "definition",
  block(width: 100%, inset: 1em, fill: color, {
    set align(left)

    context {
      [#figure.supplement~]
      counter(figure.where(kind: "definition")).display()
    }
    if name != [] {
      h(1em)
      name
    }
    linebreak()

    body
  }),
)


#set heading(numbering: "1.")
#show heading.where(level: 1): it => {
  counter(figure.where(kind: "definition")).update(0)
  it
}

= #lorem(1)

@def:a @def:b @def:c.
#definition(name: [Name])[A] <def:a>

==

= #lorem(1)
@def:a @def:b @def:c.

==
@def:a @def:b @def:c.

==
@def:a @def:b @def:c.

#definition[
  some texts
] <def:b>
#definition[
  more texts and @def:a
] <def:c>