How to properly justify text in a figure caption?

Dear All,

I am trying to style my figure captions in Typst to have a smaller font size, a constrained width, and fully justified text. I’m running into two problems: a syntax error and the counter not displaying correctly.

My goal is to have captions that look like:

Figure 1: This is the caption text
The caption text continues.

Here is the code I have tried:

#show figure.caption: it => block(
  width: 45%,
  [
    #set text(size: 0.85em)
    #set par(justify: true)
    *Figure #it.counter:* #it.body]
)

Could someone please show me the correct syntax to achieve this? How do I apply multiple set rules (for text and paragraph) inside a block for the caption?

Thanks in advance for your help!

Counters only work in context, and you need to use their display method.

#show figure.caption: set text(0.85em)
#show figure.caption: set par(justify: true)
#show figure.caption: it => context block(width: 45%)[
  *#it.supplement #it.counter.display():* #it.body
]

Thank you very much indeed! That works!

It’s better to avoid using square bracket blocks in most cases. See Create a dedicated section on dangers of using square brackets carelessly (multi-line) · Issue #6844 · typst/typst · GitHub. Also, the whitespace should be replaced with non-break whitespace. Additionally, depending on use cases, a correct re-implementation would be something like this:

#show figure.caption: set text(0.85em)
#show figure.caption: set par(justify: true)
#show figure.caption: it => context {
  show: block.with(width: 45%)
  if it.numbering != none and it.supplement != none {
    let sequence = [].func()
    let supplement = it.supplement
    let numbers = if it.numbering != none {
      it.counter.display(it.numbering)
    }
    let is-empty-supplement = {
      it.supplement.func() == sequence and it.supplement.children.len() == 0
    }
    if not is-empty-supplement { supplement += [~] }
    strong[#supplement#numbers]
    it.separator
  }
  it.body
}

// #set figure(supplement: none)
// #figure(caption: "hey", numbering: none, rect())
#set figure.caption(separator: [ --- ])
#figure(caption: "hey", numbering: "I", rect())

The less you need to (potentially) tweak, the shorter you can make the show rule.

A very short yet hacky solution would be text (regex) show rule:

#show figure.caption: set text(0.85em)
#show figure.caption: set par(justify: true)
#show figure.caption: set block(width: 45%)
#show figure.caption: it => {
  show regex("Figure" + sym.space.nobreak + "\\d+:"): strong
  it
}

#figure(caption: lorem(7), rect())

But you also should stay away from text (string/regex) show rules. From @laurmaedje:

here:

Regex show rules have, in my view, primarily proven useful as a very flexible last resort tool for situations that have no really good solutions.

and here:

you could hack something together with a regex show rule that selects words and creates boxed skews, but both typography and performance will be very bad if this is used for more than a few words

If you profile your document compilation and this does not meaningfully increase compilation time, or you don’t care about it, then you can happily use it. But it’s a bad habit that you might accidentally share with others that don’t have all this context. Which is even worse.

Hi Andrew,

Thank you so much for taking the time to improve the solution — your advice is really appreciated!

I understand now why it’s better to avoid multi-line square-bracket blocks, and I like your revised implementation using context {...}. Ensuring the whitespace is non-breaking ([~]) makes a lot of sense to avoid subtle formatting glitches. Thanks also for including the optional separator and caption re-implementation to keep things clean.

I especially like how you’ve structured the logic to conditionally include numbering and supplements—it makes the caption styling more robust and flexible. I’ll give it a try in my document.

Thanks again for your clear, practical insights!

Thanks, that’s nice to hear. If you look at the previously linked code, that’s basically Typst alternative of the Rust code for the default show rule. I’ve created a few of these for other elements here: Add default typst implementation code where possible · Issue #5095 · typst/typst · GitHub.