Adaptive margins based on page size?

I want to set page margins that scale with the paper size, something like a percentage of the page dimensions, but clamped to a minimum and maximum in terms of line-height multiples.

The ideal would be something like:

let line-height = font-size * leading-ratio
set page(
  margin: (
    x: calc.clamp(page.width * 0.12, line-height * 2, line-height * 6),
    y: calc.clamp(page.height * 0.10, line-height * 2, line-height * 5),
  )
)

The problems I’ve run into:

  • page.width / page.height require a context, but set page(margin:) doesn’t accept a context block
  • calc.clamp won’t mix ratio and length types, so 12% and line-height * 2 can’t be compared
  • Wrapping the body in show: it => context { set page(...); it } works in isolation but creates a new page in my template

Is there any way to compute a length-valued margin that depends on the page dimensions? Or a way to use context inside set page?