How to measure leading in a paragraph?

I’m trying to place a box above a line in a paragraph. The box will occupy part of the leading of the line, which is intentional, but it can also block the previous line if it’s too tall. I can fix it (e.g. by putting a box in the current line) if I can measure the leading of the current line in the paragraph. How does one do that? Thanks.

#context par.leading will give you the current value of leading at that exact location in the document. However, anything with context returns content so you can’t use this as a numerical value like 5 * #context par.leading.

Rather, any calculations and their resulting content should be done within this context expression, like so (see Why is the value I receive from context always content?):

#context {
  let leading = par.leading
  // your place calculation
  // return your place + box
}
1 Like

Thank you, that worked! How do I know from the document that leading is available as a field of par?

It should be in the docs, there you can check what fields element functions have and usually, they should then be available in a context expression: Paragraph Function – Typst Documentation.