#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.
@sijo My previous reply contains a link to a github issue that shows a minimized example.
You will only see it if - without justification - the caption is smaller than the figure width. Then - with justification - it is not streched horizontally to the width of the figure, but only to the width of the longest line.
For the example given above, the difference between the width of the longest line and the figure might be a few pixels only, and thus it might not be apparent. The example I linked makes it clear.