Hmm, interesting. The code snippet you provided works for typst v.0.12, but v.0.13 changed how paragraphs work and for some reason this doesn’t work. I’m not 100% sure, but I think this is a bug (as e.g. changing set par(justify: true) → set text(fill: blue) produces the intended behavior)
There is an unoptimal workaround, where you wrap the baragraph body in a block element:
#show par: it => context if measure(it).width > 5000pt {
set par(justify: true)
block[
#it.body
]
} else {
it
}
Which works, but gets rid of paragraphs stylings for long paragraphs (e.g. first-line-indents).
Maybe somebody more knowledgeable can chime in with their thoughts
It doesn’t work because the element (it) in a show rule has already “materialized”, meaning that its styles and properties have already been set in stone. Any set rules for the same element then aren’t applied anymore (but rules for children are, thus set text(..) works inside). Instead, you need to create a new justified paragraph, which you can do like this:
#show par: it => {
if it.justify {
// Already justified, so don't try to apply the show rule again.
return it
}
context if measure(it).width > 5000pt {
// Create a new paragraph with the same properties, but justified.
let fields = it.fields()
let body = fields.remove("body")
par(..fields, justify: true, body)
} else {
it
}
}