Why is text.size not the expected text size for level 1 and 2 headings?

Hi,

I wanted to make my show rules for headings more elegant and flexible. The idea is to apply most settings generic and with parameters. And then after some experimenting (and code golfing :stuck_out_tongue_winking_eye:) I ended with this:

#let number_headings = 9

#show heading: it => {
  let text_size = text.size + (number_headings - it.level) * 0.25em // 11pt or text.size
  text(size: text_size)[#it]
  text(size: 11pt)[#text_size] // only for debugging 
}

#for level in range(number_headings, 0, step: -1) {
  heading(level: level)[Heading #level]
}

My idea to use text.size instead of 11pt is to make headings more responsive in terms of the default font size. This is true for all headings, except level 1 and 2. The font size is 15.4 and 13.2 pt there.

Is this intentional? Did I miss something? Am I holding it wrong? Is there a pattern (like a pythonic way in Python) for these kinds of use cases?

Thank you a lot!

It was hard for me to understand what the actual question was in your post, but I think I got it now.

You’re finding that the ā€œbase text sizeā€ is 11pt in all headings except for heading level 2 and heading level 1.

That’s just the default style for headings of level 1 and 2. Your code is inside the show function: show heading: it => { ... } and when you query text.size here you notice that the default styles for headings are already present.

And you don’t need to use text.size. 1em already means the same thing.

So when you apply a show rule to headings, you already have the default styles active. There’s in fact no way to revoke the default styles. That’s why text.size is already 15.2pt in H1, and you get an inflated effect in this case if you set text size again in terms of em, since em depends on the text size.

If you want to set heading sizes independently of the default styles, then you should reset the text size in absolute terms, for example we can reset them all to 11pt first:

#show heading: set text(size: 11pt)

(With the caveat that if this is done then heading size is no longer contextual, no longer dependent on the ā€œambientā€ text size! For example, imagine Part I of the book having base text size 10pt and Part II base text size 12pt, for some reason, if that would occur…)


The most generic resulting style would come from taking the default sizes and dividing them out so that all headings are set to size 1em in terms of the root text size, before you configure them further.

But that’s also the least general configuration setup since you will need to input the presumed default Typst styles to do this…

// reset heading sizes to root text size...
#show heading.where(level: 1): set text(size: 1em/1.4)
#show heading.where(level: 2): set text(size: 1em/1.2)
2 Likes

Thank you very much, now I see it. Somehow I didn’t notice, that level 1 and 2 are bigger, but after that headings are just the same size. So text.size is not the size of the text, but the size of the heading.

Resizing them back and then applying the styling fixes this. Thank you very much!

1 Like