How to get widths of content and layout as a number (length type in Typst).
What I tried is to use layout() and measure() as suggested by Typst doc. Using these functions, I can show the width, for example 453.54pt, on the pdf file, but the data type of 453.54pt is content, and I cannot convert it to length type.
More specifically, I tried the following code:
#let layoutwidth = layout(size => {return size.width})
#let textwidthof(body) = context { return measure(body).width} // a function
#let atextwidth = textwidthof[Hello World]
Although we can print the two widths layoutwidth and atextwidth on the pdf file, it is of type content, not length. I also tried to convert it to string str() and then eval(), but it does not work! Any ideas?
I've been facing the exact same problem today, and finally found that it's due to the
context.In the measure doc page they say it has to be used in a given context because it depends on where it is in the document.
The thing is you have to be in a context to access the fields of
widthandheight:However this would not work because the variable only exist in the
context:And the following is what returns a content that we can't use, I'm not sure of the reason but I would guess that the content type comes from the context keyword :
Edit : If the goal is to spread text across the page, it's better to use
frinstead ofcontextandmeasure.