How to make figure caption text have the same width as the figure?

Hi, you can use a show rule on the figure to measure its size, and use a nested show rule on the caption to restraining its size to the previously measured one.

I believe this to be what you’re going for:

#set page(width: auto, height: auto, margin: 2em)
#show figure: it => {
	let w = measure(it.body).width
	set par(justify: true)
	show figure.caption: cap => box(width: w, cap)
	it
}

#figure(
  rect(width:10cm, height: 5cm),
  caption:
    [_A coastal hydrodynamics simulation of wave impact on a light house using a particle based approach. Figure courtesy of Ihmsen et al._]
)

which creates
image

1 Like