How to customize the styling of caption supplements?

I want to have figures with a bold supplement+number+separator, but I don’t want my references to the figure to be in bold.

I have multiple figure types (Figure, Algorithm, Table) so I want the solution to be DRY if possible (I don’t want to repeat the same boilerplate for every figure/type).

However, the following code does not work, as it does not increment the figure number in the caption (but it does in the figure references):

#show figure.caption: it => box(
  inset: (left: 1em, right: 1em),
  align(left)[
    *#it.supplement~#it.numbering#it.separator*#it.body
  ]
)

#figure(
  box(stroke: 1pt, inset: 5pt)[This is a figure],
  caption: [This is a caption]
) <fig1>

#figure(
  box(stroke: 1pt, inset: 5pt)[This is a figure],
  caption: [This is a caption]
) <fig2>

@fig1 @fig2

How to fix this?

Hello, the it.numbering is actually the numbering scheme. You have to use it.counter.display().
Here is how I would write it

#show figure.caption: it => context [
  *#it.supplement~#it.counter.display()#it.separator*#it.body
]

image

2 Likes

Regarding #it.counter().display(), be careful if you use it with floating figures, see How to customize the style of equation numbering? - #8 by xkevio.

1 Like

Hey, the #it.counter.display(it.numbering) is outdated in typst 0.12. Thus, how to use context to rewrite the script? Thanks.

Use #context it.counter().display(it.numbering) instead

Could you provide a complete script? I found it hard to recognize the context.

The following is wrong…

#show figure.caption: it => [
    #text(weight: "bold")[
        #it.supplement S #context it.counter().display(it.numbering).~
    ]#it.body
]

Can you tell me what the error is, or what is wrong?

code here:

#set page("a4", margin: auto)

#show figure.caption: it => [
    [*#it.supplement S#context it.counter().display(it.numbering).*~]#it.body
]

// figure
#figure(
    rect(height:6cm, width: 11cm, fill: red),
    caption: [A curious figure.],
) <fig1>


#figure(
    rect(height:10cm, width: 5cm, fill: blue),
    caption: [A curious figure.],
) <fig2>

with error:

error: type content has no method `counter`
  ┌─ ask2.typ:4:34
  │
4 │     [*#it.supplement S#context it.counter().display(it.numbering).*~]#it.body
  │                                   ^^^^^^^
  │
  = hint: did you mean to access the field `counter`?

My bad, I did write counter() instead of counter, but I don’t understand what your original issue was?

I just want you to update your answer to this question by using context, avoiding the warning message “warning: counter.display without context is deprecated” in typst 0.12.

I found just removing the brackets of counter is okay. Such as #context it.counter.display(it.numbering) or #context(it.counter.display(it.numbering)) both works fine.

Thanks for your answer.

1 Like

The answer already is a context expression

1 Like