How to add spacing around headings, but not between headings?

Hi !

I’m looking for the best way to automatically insert a line break (or a small visible gap) between a heading and the following body, but not add that gap when a heading is followed by another heading.

For now I use this rule : #show heading:it => {it + v(1em)}
But it also adds spaces between Two headings (e.g. Title & subtitle) which I’d like to remove.

Would you know how to achieve that ?

Thanks !

You might be interested in this reply by @vmartel08 in a similar post:

I didn’t have success getting your desired result with either of the suggested answers from that post unfortunately.

Thanks for your answer !
I’ve managed to get it working with this show rule, almost the same as in the github issue you linked :

#show heading: it => {
  // Clever trick to reduce spacing between consecutive headings
  // See https://github.com/typst/typst/issues/2953
  let previous_headings = query(selector(heading).before(here(), inclusive: false))
  if previous_headings.len() > 0 {
    let prev_loc = previous_headings.last().location().position()
    let it_loc = it.location().position()
    if (it_loc.page == prev_loc.page and it_loc.x == prev_loc.x and it_loc.y - prev_loc.y < 60pt) { // threshold
      v(-1.5em) // amount to reduce spacing, could make this dependent on it.level
    }
    else {}
  }
  [#it #v(1em)]
}
1 Like