Adjusting fontsize so text fits with multiple columns

Perhaps putting columns() inside scale-text?

/// Scales down text by adjusting the fontsize, until the text fits within the container.
#let scale-text(columns: 1, gutter: 2em, body) = layout(
  available => context {
    let font-size = text.size
    let height = calc.inf * 1pt

    // Reduce font size until content fits
    while height / columns > available.height {
      font-size -= 0.2pt
      height = measure(
        text(size: font-size, body),
        width: (available.width + gutter) / columns - gutter,
      ).height
    }


    set text(size: font-size)
    std.columns(columns, gutter: gutter, body)
  },
)


#set page(paper: "a4", flipped: true)

#grid(
  columns: (3fr, 1fr),
  gutter: 2em,
  rect(fill: purple, height: 100%, width: 100%), scale-text(lorem(500)),
)

#pagebreak()

#grid(
  rows: (3fr, 1fr),
  gutter: 2em,
  rect(fill: aqua, height: 100%, width: 100%),
  scale-text(columns: 3, lorem(500)),
)

1 Like