Skip to content

Fix auto hanging-indent for 〔1〕 heading numbering - #7247

Merged
laurmaedje merged 16 commits into
typst:mainfrom
YDX-2147483647:indent-auto
Aug 4, 2026
Merged

laurmaedje merged 16 commits into
typst:mainfrom
YDX-2147483647:indent-auto

Conversation

@YDX-2147483647

@YDX-2147483647 YDX-2147483647 commented Oct 28, 2025 •

Copy link
Copy Markdown
Contributor

Resolves a part of #6527.

Previously, if the heading numbering starts or ends with a CJ punctuation and its width is allowed to be shrunk or stretched, then hanging-indent: auto resolves to the wrong length, resulting in multi-line headings misaligned.

This PR amends the logic of measure(heading.numbering).width to fix that.

#set page(height: auto, width: 12em, margin: 1em)
#set text(lang: "zh", region: "CN", font: "Noto Serif CJK SC", fallback: false)

#set heading(numbering: "〔1〕")
= 犹可说也不可说也

#set heading(numbering: "一、")
= 犹可说也不可说也

#set heading(numbering: "一口")
= 犹可说也不可说也

#set heading(numbering: "一、")
#show heading: it => {
  show h.where(amount: 0.3em): none
  it
}
= 犹可说也不可说也
Before (v0.15.1) After (this PR)
图片 图片
  • The first two examples are improved
  • The third example is already optimal and unchanged
  • The fourth example is still not optimal and may be dealt with future PRs. At present, it can be circumvented by #set heading(hanging-indent: 2em).

Previous discussions

See Typographic measure theory (or the backup on the forum) for more testing.

Discord, 2025-10-30:

Hi! There is a 0.3-em spacing between the heading number and the body. At present, the spacing is weak.
Does anyone know why it has to be weak? Could it be changed to a strong spacing? How about h(0.3em, weak: true) + h(0pt, weak: false)?
图片

It is weak to eat up a potential leading space in the body. For inline gaps that are intended to be precise, I prefer weak for that reason.

@YDX-2147483647 YDX-2147483647 changed the title Fixed auto hanging indent for 一、 numbering Fix auto hanging-indent for 一、 heading numbering Oct 28, 2025
@laurmaedje laurmaedje added layout Related to the layout category, which is about composing, positioning, etc. fix A bug fix. labels Nov 7, 2025
@laurmaedje laurmaedje added the waiting-on-author Pull request waits on author label Nov 17, 2025
@laurmaedje

Copy link
Copy Markdown
Member

What's the state of this PR?

@YDX-2147483647

YDX-2147483647 commented Jan 8, 2026 •

Copy link
Copy Markdown
Contributor Author

It fixes the original issue, but does not work well with show h.where(amount: 0.3em): none.
In practice, there's no space after 一、, so show h.where(…): none is usually (if not always) necessary.
Therefore, this PR alone does not fix the real problem, yet introduces a breaking change.

Also the implementation depends on quirks of measure.

I guess there exists a better fix, so I've marked it as a draft.

@laurmaedje laurmaedje added waiting-on-design This PR or issue is blocked by design work. and removed waiting-on-author Pull request waits on author labels Jan 8, 2026
@laurmaedje

Copy link
Copy Markdown
Member

I think the supposed quirk of measure is rather a quirk of either horizontal spacing or CJ punctuation adjustment. h(0pt) will be discarded during inline layout, while it seems like h(0.1pt) will prevent the adjustment.

From a Western perspective, the patch here seems unproblematic, but if it doesn't really solve the problem for CJ users, that's unfortunate. I can't properly judge it, but from gut feeling perhaps the CJ punctuation adjustment is the root cause and needs change? I'd overall like to see the CJK handling in inline layout reworked as it has various subtle issues and is not that cleanly implemented.

@YDX-2147483647

YDX-2147483647 commented Jan 28, 2026 •

Copy link
Copy Markdown
Contributor Author

I haven't carefully inspected how the CJ punctuation adjustment is implemented; but based on what DeepWiki told me, I think it looks logical. It's just that the punctuation adjustment doesn't rhyme with higher-level adjustment (e.g., justification).

Higher-level adjustment is irrelevant in this case, because the issue occurs even if justification is disabled and linebreaks are controlled manually.

I believe the root cause lies in this comment:

// We don't have a locator for the numbering here, so we just
// use the measurement infrastructure for now.

  1. We don't have a locator after the numbering, so we have to resort to measure.
  2. The measure infrastructure can't measure things in context. It has to isolate the object before measurement.
  3. The space occupied by the content may vary depending on the preceding and following content.

@laurmaedje

Copy link
Copy Markdown
Member

Having a locator wouldn't really change anything about measurement for this case. It lets things resolve "in context" in the sense that introspections resolve properly, but it does not let measure peak ahead and see that a weak space would not collapse.

Overall, I'm not sure how to proceed here. As I mentioned, the current diff looks unproblematic, but you mentioned that it does not fully address the issue. I think we should come to a decision here on whether we actually think we can merge something here or whether to close this.

@laurmaedje laurmaedje added the waiting-on-author Pull request waits on author label Jul 31, 2026
@YDX-2147483647

YDX-2147483647 commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor Author

Oh, my opinion has changed a bit. Someone once came across a use case in their real life that involves full-width punctuations and don't need to remove the 0.3em space (but I can't recall the speicifc case right now…).
In other words, this PR does indeed solve some real problems, so I now think it's worth merging.

I'll check my chat history, replace the currently suboptimal test case with that case, and then mark this PR as ready for review.

Update:
That case is numbering: "〔1〕", reported by 盒子Bruce on 2025-06-17.

Update 2:
Somehow, this PR does not fix the case for 〔1〕.
heading-hanging-indent-auto-cj-punct

Update 3:
The cause looks like to be #4011

Test
#set page(height: auto, width: 120pt, margin: 10pt)
#set text(lang: "zh", region: "CN", font: "Noto Serif CJK SC", fallback: false)

#set text(10pt)
#show heading: set text(10pt, weight: "regular")
#set page(width: 12em, margin: 1em)
#set heading(numbering: "〔1〕")

= #context here().position().x;犹可说也不可说也

〔1〕#h(0.3em, weak: true)#context here().position().x;犹可说也不可说也

#h(0.00001pt)〔1〕#h(0.3em, weak: true)#context here().position().x;犹可说也不可说也

#block(inset: (left: 22.71pt))[
  #h(-22.71pt)〔1〕#h(0.3em, weak: true)#context here().position().x;犹可说也不可说也
]

#context here().position().x
- #context measure("〔1〕" + h(0.3em)).width
- #context measure("〔").width
- #context measure("1").width
- #context measure("〕").width
图片

@YDX-2147483647 YDX-2147483647 changed the title Fix auto hanging-indent for 一、 heading numbering Fix auto hanging-indent for 〔1〕 heading numbering Jul 31, 2026
@YDX-2147483647
YDX-2147483647 marked this pull request as ready for review July 31, 2026 14:47
@YDX-2147483647

Copy link
Copy Markdown
Contributor Author

Hi! I've revised the code and update the PR description. It's ready for review now.

@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author waiting-on-design This PR or issue is blocked by design work. labels Jul 31, 2026
@laurmaedje

Copy link
Copy Markdown
Member

Thanks!

@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label Aug 4, 2026
@YDX-2147483647

YDX-2147483647 commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Don't merge yet! In the previous version, the two spaces are different! One positive, one negative.

Update: Oh, I see what's going here… Never mind.

@laurmaedje

Copy link
Copy Markdown
Member

I felt a weak space was a bit cleaner since it doesn't just cancel out, but actually disappears during layout.

@laurmaedje
laurmaedje added this pull request to the merge queue Aug 4, 2026
Merged via the queue into typst:main with commit e2bd925 Aug 4, 2026
9 checks passed
@YDX-2147483647
YDX-2147483647 deleted the indent-auto branch August 4, 2026 10:25
Dikluwe pushed a commit to Dikluwe/typst-crystalline that referenced this pull request Aug 7, 2026
93 commits novos desde v0.15.1 (tag 9dfd3a0) até upstream/main (a51e028).
397 ficheiros alterados, 5782 inserções, 2831 remoções.

Destaques do upstream:
- Store required features in bindings (typst#8599)
- Support raw serialization format for typst eval (typst#8295)
- Add diagnostics-format environment variable (typst#8438)
- More robust overhang (typst#8131)
- Fix logical indices of hyphens with respect to surrounding tags (typst#8714)
- Bump krilla and hayro (typst#8716)
- Fix line breaking for hosts starting with a digit (typst#8039)
- Fix auto hanging-indent for heading numbering (typst#7247)

Build vanilla verificado: cargo build ok (3m02s).
Dikluwe added a commit to Dikluwe/typst-crystalline that referenced this pull request Sep 12, 2026
93 commits novos desde v0.15.1 (tag 9dfd3a0) até upstream/main (a51e028).
397 ficheiros alterados, 5782 inserções, 2831 remoções.

Destaques do upstream:
- Store required features in bindings (typst#8599)
- Support raw serialization format for typst eval (typst#8295)
- Add diagnostics-format environment variable (typst#8438)
- More robust overhang (typst#8131)
- Fix logical indices of hyphens with respect to surrounding tags (typst#8714)
- Bump krilla and hayro (typst#8716)
- Fix line breaking for hosts starting with a digit (typst#8039)
- Fix auto hanging-indent for heading numbering (typst#7247)

Build vanilla verificado: cargo build ok (3m02s).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix A bug fix. layout Related to the layout category, which is about composing, positioning, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants