#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._]
)
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._]
)
Just one question, if you use an actual image and set width to: width: 60% does the function you made also break for you? I get a width of 0pt in that case…
in that case you need to still wrap in in a layout call, to give it the context it needs
#show figure: it => layout( sz => {
let w = measure(
it.body,
width: sz.width
).width
set par(justify: true)
show figure.caption: cap => box(width: w, align(left,emph(cap)))
it
})
I find typst has great documentation, it’s worth going through to find out what’s even possible.
The layout function makes the dimensions of the parent container available. Setting your width: 60% is 60% of the parent’s dimensions + 0pt of absolute size. Without the knowledge of the parent’s dimensions only the 0pt remains, which is why the first solution broke for relative sizes.