How can I set font size relative to a block’s size?

I’m producing a flyer to print in various sizes. I’m getting a bit lost on the distinction of ratios, lengths and relatives.

What I want is something like #text(size: 1%) or #text(size: 1%.length) (exact percentage to be figured out experimentally.)

Are you looking for layout?

#block(/*width: ..., height: ..., */ layout(size => [
  #set text(size: 1% * size.width)

  #lorem(300)
]))

Ok, that helped, when I finally got it working, thank you!

However I’m astounded at this complicated solution. Why in the world are measurements not simply convertible into the various aspects at the current spot (e.g. absolute to relative and vice versa, optionally with ratio?)

As a (documented, but) weird side effect of obtaining the size, the content becomes a block, which broke my layout. So I tried to merge it with the block I already had, but didn’t manage.

You could use a box instead of a block. It would keep things inline with your normal content.

1 Like

I guess in theory there is nothing preventing the font size from accepting ratios, but allowing this would likely be confusing to users. For example, a block that has a width and height of 100% does not mean its width and height are actually the same. If we set the font to have size 5%, should it have 5% of the block’s height, or the block’s width? If it should be 5% of the current font size, there is already a way to do this, 0.05em.

If you’re annoyed at the extra boilerplate of layout, this is because accessing any information about e.g. the size of elements inherently requires some context about the document. Working with contextual expressions is a bit tricky, but in case you are curious I’d recommend you to read this answer by one of the developers: Why is the value I receive from context always content? - #2 by laurmaedje