Skip to content

Indentation on multiline headings with full-width character #6527

Open

Description

@KiyanYang

Description

#set heading(numbering: "1)")

= A very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long heading

#set heading(numbering: "1)") // fullwidth brace

= A very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long heading

Image

Note: The red content was marked by myself.

Reproduction URL

No response

Operating system

Web app

Typst version

  • I am using the latest version of Typst

Activity

  1. added
    bug
    on Jun 30, 2025
  2. added
    layout
    text
    on Jun 30, 2025
  3. Enivex commented on Jun 30, 2025

    @Enivex
    Collaborator

    As far as I know, full width characters are present for compatibility purposes with older encoding standards, and shouldn't really be used. Especially outside the context of CJK writing.

    That being said, this does look like a bug.

  4. YDX-2147483647 commented on Jul 2, 2025

    @YDX-2147483647
    Contributor

    I have encountered this problem before, too.

    Workaround

    #set heading(hanging-indent: 2em)
    
    #show heading: it => {
      show h.where(amount: 0.3em): none
      it
    }

    Explanations

    hanging-indent
    The default value of auto indicates that the subsequent heading lines will be indented based on the width of the numbering.
    Default: auto

    As shown in the following examples, it relates to #5778 and punctuation width adjustment. A full-width Han character 口 will not trigger this issue.

    Image

    #set page(width: 12em, height: auto, margin: 1em)
    #set text(
      lang: "zh",
      font: "Noto Serif CJK SC",
      top-edge: "ascender",
      bottom-edge: "descender",
    )
    
    #let tests = {
      [
        #set heading(numbering: nums => "一、")
        = 国国国国国国国国国
      ]
    
      [
        #set heading(numbering: nums => "一口")
        = 国国国国国国国国国
      ]
    }
    
    The default hanging-indent is #auto.
    
    #tests
    
    Change hanging-indent to #2em.
    #set heading(hanging-indent: 2em)
    
    #tests
    
    Remove the #0.3em space. (See \#5778)
    #show heading: it => {
      show h.where(amount: 0.3em): none
      it
    }
    
    #tests
    
    Change hanging-indent back to #auto.
    #set heading(hanging-indent: auto)
    
    #tests
  5. added a commit that references this issue on Oct 28, 2025
    d6326af
  6. tianyi-smile commented on Aug 2, 2026

    @tianyi-smile

    A better fix (for Typst 0.15) might be to handle heading.numbering and heading.body separately, for example by wrapping the content of heading.numbering in a box.

    #show heading: it => {
      if type(it.numbering) == str {
        let fields = it.fields()
        _ = fields.remove("numbering")
        _ = fields.remove("body")
        counter(heading).update((..it) => {
          let p = it.pos()
          p.at(-1) = p.at(-1) - 1
          p
        })
        show heading: set text(text.size)
        heading(
          numbering: (..n) => {
            // here, you can add `#h(.3em, weak: true)` or not
            // maybe `heading` should have a field like `body-indent` to control the spacing between `heading.numbering` and `heading.body`
            box([#std.numbering(it.numbering, ..n)#h(.3em, weak: true)])
          },
          ..fields,
          it.body,
        )
      } else {
        it
      }
    }

    Some tests:

    #set page(height: auto, width: 5cm, margin: 1em)
    #set text(lang: "zh", region: "CN", font: "Noto Serif CJK SC")
    // #show heading: set text(size: 15pt)
    #let test = [永人永人永人永人]
    #set heading(numbering: "〔1〕")
    not good:
    = #test
    #set heading(numbering: "一、")
    = #test
    #set heading(numbering: "一(1)")
    == #test
    
    #line(length: 100%)
    
    #show heading: it => {
      if type(it.numbering) == str {
        let fields = it.fields()
        _ = fields.remove("numbering")
        _ = fields.remove("body")
        counter(heading).update((..it) => {
          let p = it.pos()
          p.at(-1) = p.at(-1) - 1
          p
        })
        show heading: set text(text.size)
        heading(
          numbering: (..n) => {
            // here, you can add `#h(.3em, weak: true)` or not
            // maybe `heading` should have a field like `body-indent` to control the spacing between `heading.numbering` and `heading.body`
            box([#std.numbering(it.numbering, ..n)#h(.3em, weak: true)])
          },
          ..fields,
          it.body,
        )
      } else {
        it
      }
    }
    
    fix:
    #set heading(numbering: "〔1〕")
    = #test
    
    #set heading(numbering: "一、")
    = #test
    #set heading(numbering: "一(1)")
    == #test
    
    #set heading(numbering: "一口")
    = #test
    
    
    #set heading(numbering: "1)")
    
    = Long title test
    
    #set heading(numbering: "1)") // fullwidth brace
    
    = Long title test
    Image

    If pass function to heading.numbering, then wrap it to box

    #set heading(numbering: (..num) => box({
      // custom
    }))

    e.g.

    #import "@preview/numbly:0.1.0": *
    #let box-numbly(..arr, default: "1.") = (..num) => box(numbly(..arr, default: "1.")(..num))
    #set heading(numbering: box-numbly(
      "{1:附录 A})", // use {level:format} to specify the format
      "{1:A}.{2})", // if format is not specified, arabic numbers will be used
    ))
    
    = #test
    == #lorem(2)
    == #test
    Image
  7. YDX-2147483647 commented on Aug 2, 2026

    @YDX-2147483647
    Contributor

    Interesting. It even generates better output than #7247 for 〔1〕.
    It never occurred to me, because box may cause vertical misalignment with the old compilers.

    Your solution can summarized into the following util function. It fixes the hanging-indent by replacing the numbering text with a measure-stable box.
    Here, measure-stable means that measure(it).width + 0.3em == measure(it + h(0.3em, weak: true)).width.

    #let boxed(pattern, spacing: 0.3em) = (..nums) => box({
      numbering(pattern, ..nums)
      h(spacing, weak: true)
    })
    
    #set heading(numbering: boxed("〔1〕"))

    The only caveat I found is that spacing does not work for general values:

    #set raw(lang: "typc")
    
    #set heading(numbering: boxed("一、", spacing: 0em))
    = 国 #0em
    #set heading(numbering: boxed("一、", spacing: 0.00000001em))
    = 国 `0.00000001em`
    #set heading(numbering: boxed("一、", spacing: 0.1em))
    = 国 #0.1em
    #set heading(numbering: boxed("一、", spacing: 0.3em))
    = 国 #0.3em
    #set heading(numbering: boxed("一、", spacing: 0.5em))
    = 国 #0.5em
    #set heading(numbering: boxed("一、", spacing: 2em))
    = 国 #2em
    Image
  8. tianyi-smile commented on Aug 2, 2026

    @tianyi-smile

    The only caveat I found is that spacing does not work for general values

    I think this is caused by weak: true in h. The following should work:

    #let boxed(pattern, spacing: 0.3em) = (..nums) => box({
      numbering(pattern, ..nums)
      h(spacing)
    })
  9. YDX-2147483647 commented on Aug 2, 2026

    @YDX-2147483647
    Contributor

    If strong h is used, is it still possible to decrease the actual space between numbering and body to less than 0.3 em?

  10. tianyi-smile commented on Aug 2, 2026

    @tianyi-smile

    If strong h is used, is it still possible to decrease the actual space between numbering and body to less than 0.3 em?

    I think we can set box.inset, like:

    #let my-heading(body-indent: 0pt, it) = {
      if type(it.numbering) == str {
        let fields = it.fields()
        _ = fields.remove("numbering")
        _ = fields.remove("body")
        counter(heading).update((..it) => {
          let p = it.pos()
          p.at(-1) = p.at(-1) - 1
          p
        })
        show heading: set text(text.size)
        heading(
          numbering: (..n) => {
            box(
              inset: (right: body-indent),
              [#std.numbering(it.numbering, ..n)],
            )
          },
          ..fields,
          [#it.body],
        )
      } else {
        it
      }
    }

    Tests

    #set page(height: auto, width: 5cm, margin: 1em)
    #set text(lang: "zh", region: "CN", font: "Noto Serif CJK SC")
    #let test = [永人永人永人永人]
    
    not good
    #set heading(numbering: "一、")
    = #test
    
    fine
    #show heading: my-heading.with(body-indent: 0em)
    = #test
    #show heading: my-heading.with(body-indent: -.3em)
    = #test
    #show heading: my-heading.with(body-indent: 0.2em)
    = #test
    #show heading: my-heading.with(body-indent: 0.3em)
    = #test
    #show heading: my-heading.with(body-indent: 0.4em)
    = #test
    #show heading: my-heading.with(body-indent: 0.5em)
    = #test
    Image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buglayouttext

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions