Set width of rectangle to a calculated value

I’m rather new to Typst and I’m curious about options to generate diagrams. I’m not looking for a package, but would like to understand better how Typst works to get a feeling for what’s possible. So my current focus is learning and understanding. In that context:

I have a large rectangle like this:

#let large = rect(radius:5pt)[
  a rather long text without line break
]

and a small one:

#let small = rect(radius:5pt)[
  small
}

I’m able to calculate their sizes using measure. I would like to calculate the max width of both rectangles and make both of that width.

I cannot wrap my head around how this is done. Can somebody show me a simple example? I assume I miss something or got some concept wrong.

So my goal is to have boxes having the same size, automatically adjusting to the largest one.

Hello, I believe you would need to separate your texts from the rectangles. That way, the maximum width can be obtained before you construct the rectangles.

Here’s a solution I would use:

#let texts = (
  "a rather long text without line break",
  "small",
)

#context {
  let shp = rect // Or `circle`, `ellipse` ...
  let txt-max = calc.max(..texts.map(txt => measure(shp(txt)).width))
  for txt in texts { shp(width: txt-max, txt) }
}
Output

I don’t think the txt-max is particularly dreadful.