How to control whitespace before and after run-in headings

The whitespace spacing is fixable with a weak spacing:

#let body-size = 11pt
#set text(font: "New Computer Modern", size: body-size)
#set par(justify: true, spacing: 0.6em, first-line-indent: 1em)
#set heading(numbering: "1.1.")

#show heading.where(level: 1): set align(center)
#show heading.where(level: 1): set text(size: 13pt, weight: "regular")
#show heading.where(level: 1): smallcaps
#show heading.where(level: 1): it => {
  show h.where(amount: 0.3em): sym.space.quad
  it
}

#let space-above-inline-headings = 1em

// Destructive rule before constructive.
#show selector.or(..(2, 3).map(n => heading.where(level: n))): it => {
  v(space-above-inline-headings)
  // The alternative to negative h() is the trick from https://forum.typst.app/t/how-can-i-prevent-indent-on-paragraphs-with-run-in-headings/3214/2 which breaks the other trick to reduce space between consecutive headings.
  h(-par.first-line-indent.amount)
  counter(heading).display()
  sym.space.quad
  it.body
  let message = "Have to convert to string with t4t package after all"
  assert("text" in it.body.fields(), message: message)
  if it.body.text.last() not in ".?!" [.]
  sym.dash.em
  h(0pt, weak: true)
}

#show heading.where(level: 2): set text(size: body-size)

#show heading.where(level: 3): set text(size: body-size, weight: "regular")
#show heading.where(level: 3): emph

// Clever trick to reduce spacing between consecutive headings.
// Must be defined after the destructive show rules.
// See https://github.com/typst/typst/issues/2953#issuecomment-3187505828
#show heading: it => {
  let previous-headings = query(
    selector(heading).before(here(), inclusive: false),
  )
  if previous-headings.len() > 0 {
    let prev = previous-headings.last().location().position()
    let cur = it.location().position() // Curent position.
    let delta = 30pt // Threshold
    if (cur.page == prev.page and cur.x == prev.x and cur.y - prev.y < delta) {
      // Amount to reduce spacing, could make this dependent on it.level
      v(-10pt)
    }
  }
  it
}

= Introduction
#lorem(25)

#lorem(15)

== Motivation
#lorem(20)

=== Subsubsection
#lorem(15)

== Etc.
=== Subsubsection
#lorem(10)

// == Bad: indeed
// #lorem(20)

The space after numbering in block-level heading is different because the numbering spacing was defined incorrectly. There was also a big repeating show rule. Though it’s rather annoying that you can’t just use #show heading.where(level: 2): set par(first-line-indent: 0pt) for run-in headings and instead use a negative spacing hack.

1 Like