Unwanted space before headings with `set heading`

Why is there a space before headings in the example below? In my app, I have a complex “if” inside the function, but below is the totally boiled-down version that also shows the space.

#set heading(numbering: (..nums) => none)


= Prospective
#lorem(20)

== Sub-Prospective
#lorem(20)

Hi, welcome to the forum!

It’s because when numbering is not none (in you case, it’s a function that returns none, and the function itself is not none), a small space will be inserted between the number and the body.
In other words, = Prospective becomes the following:

none + h(0.3em, weak: true) + [Prospective]

Related issue: Too wide spacing between heading numbering and title in CJK · Issue #5778 · typst/typst · GitHub

Solution A: Returns a negative space

#set heading(numbering: (..nums) => h(-0.3em))

Solution B: Replace if/else with where selectors

#show heading.where(level: 2): set heading(numbering: …)
#show heading.where(level: 3): set heading(numbering: …)
4 Likes

The first solution is a hack and does not fix the problem with PDF Document Outline, if that is a concern:

= Heading
#set heading(numbering: (..) => none)
= Heading

image

This is basically the same exact issue I faced not that long ago, and the solution was to properly disable numbering for specific headings. This is also the only way to do this properly. But to select the headings…well, you have to write some sort of hack, like in How to conditionally disable heading numbering for some headings? - #4 by Andrew. If the if statement is indeed complex, then the per-level show-set rule won’t help either. Though maybe a combination can make the hack smaller.

1 Like