#Minimum width/height of a box()

13 messages · Page 1 of 1 (latest)

visual mango
#

I want to set a minimum width for a box(): if the width of the content is smaller than the minimum width min-width, then the box's width is min-width, otherwise it is the size of the content.

I know that there exists a hacky solution to do that by using a stack(): box(stroke: 1pt, stack(dir: ttb, h(min-width), [This is my content])).

However, isn't there a more idiomatic solution? The last mention that I found of this solution on this Discord servers was 7 months ago. Also, isn't there a solution that takes into account the potentiel inset/outset of the box?

The following solutions do not work:

  • #box(width: calc.min(min-width, auto))[This is my content]

Error: cannot compare auto and length

  • #box(min-width: min-width)[This is my content]

Error: unexpected argument: min-width

worldly raft
#

?r

#let boxm(min_width, content, ..options) = context{
  assert(type(min_width) == length)
  let w = measure(content).width
  if w < min_width{
    w = min_width
  }
  box(width: w, ..options, content)
}
#boxm(5cm, stroke: 1pt)[
  Short
] \
#boxm(5cm, stroke: 1pt)[
  Very loooooooooooooooooooooooooooooong
] \
#boxm(5cm, stroke: 1pt, inset: 1cm)[
  Very loooooooooooooooooooooooooooooong
]
full anvil
#

you were faster haha

worldly raft
#

I don't think it's entirely correct though

full anvil
#

I'll still provide my solution also because it adds two details

#

?r t=l ```
#let my-box(content, min-width: none, ..args) = context {
let normal-box = box(content, ..args)
if min-width == none { return normal-box }
let target-width = measure(normal-box).width

return box(width: calc.max(min-width.to-absolute(), target-width), content, ..args)
}

#set box(fill: yellow)

#my-box("a", min-width: 30pt)
#my-box("abcdefgh", min-width: 30pt)

#my-box("a", min-width: 30pt, inset: 4pt)
#my-box("abcdefgh", min-width: 30pt, inset: 4pt)

full anvil
#

@visual mango As of now, there is no "built-in" solution. The best thing would be to write your own function once and for all, something like the suggestions above.

full anvil
visual mango
#

Thanks for all the solutions, I indeed presumed that you could do it using measure, but I didn't know if there was a simpler solution

visual mango
visual mango
#

Here is a completed version also supporting the min-height parameter