How to detect whether heading is directly preceeded by other heading?

This seems to be the latest development: Collapse adjacent headings · Issue #2953 · typst/typst · GitHub

#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 ploc = previous_headings.last().location().position()
    let iloc = it.location().position()
    if (iloc.page == ploc.page and iloc.x == ploc.x and iloc.y - ploc.y < 30pt) {
      // threshold
      v(-10pt) // amount to reduce spacing, could make this dependent on it.level
    }
  }
  it
}
1 Like