Skip to content

Redundant CJK-Latin space at manual line breaks #6539

Closed

Description

@YDX-2147483647

Original title: cjk-latin-space + manual linebreak + align(center) ⇒ Unexpected left shift

Description

If the line is broken manually between a CJK and a Latin character, then typst will insert an extra CJK-Latin space. This space becomes noticeable when text is aligned to the right or center.

In the following example, the text 国国 is intended to be horizontally centered, but it appears slightly shifted to the left.

#set text(font: "Noto Serif CJK SC")
#box(width: 2.3em, stroke: (x: green), align(center, [国国\ T T]))

Image

This issue only occurs when

  • cjk-latin-spacing is enabled (which is the default),
  • and the line break is manually created by linebreak() or \ .

This issue is NOT related to the linebreaks algorithm, as it occurs in both #set par(linebreaks: "simple") and #set par(linebreaks: "optimized").

More examples

Image

Full code
#let test(body, width: 2.3em) = box(width: width, stroke: (x: green), align(
  center,
  body,
))

#set page(width: auto, height: auto, margin: 1em)
#set text(
  font: "Noto Serif CJK SC",
  lang: "zh",
  top-edge: "ascender",
  bottom-edge: "descender",
)

#set raw(lang: "typc")
#show raw: set text(font: ("DejaVu Sans Mono", "Noto Serif CJK SC"))

#set table.hline(stroke: 0.5pt)

#table(
  columns: 3,
  stroke: none,
  align: (horizon, center + horizon, horizon),
  ..(
    ([✅], `[国国TT]`),
    table.hline(),
    ([❌], `[国国\ TT]`),
    (
      [❌],
      `[国国] + linebreak() + [TT]
// Equivalent to the above`,
    ),
    (
      [❌],
      `text(cjk-latin-spacing: auto)[国国\ TT]
// Equivalent to the above`,
    ),
    ([✅], `text(cjk-latin-spacing: none)[国国\ TT]`),
    table.hline(),
    ([❌], `[国国\ T国]`),
    ([✅], `[国T\ T国]`),
    ([✅], `[国T\ 国国]`),
    ([✅], `[国国\ 国国]`),
  )
    .map(row => if type(row) == array and row.len() == 2 {
      let (ok, body) = row
      (ok, test(eval(body.text)), body)
    } else { row })
    .flatten(),
  table.hline(),
  [❌],
  test(width: 2.3em)[国国\ TT],
  [`[国国\ TT] + 2.3em`, where `2.3em` is width],
  [❌], test(width: 2.5em)[国国\ TT], `[国国\ TT] + 2.5em`,
  [❌], test(width: 2.8em)[国国\ TT], `[国国\ TT] + 2.8em`,
  [❌], test(width: 3.5em)[国国\ TT], `[国国\ TT] + 3.5em`,
)

Additionally, the cjk-latin space also affects align(end).

Image

Links

Reproduction URL

No response

Operating system

Windows, Web app

Typst version

  • I am using the latest version of Typst

Activity

  1. added
    bug
    on Jul 1, 2025
  2. changed the title [-]cjk-latin-space + manual linebreak + align(center) ⇒ Unexpected left shift[/-] [+]Unexpected cjk-latin space at manual linebreaks[/+] on Jul 2, 2025
  3. changed the title [-]Unexpected cjk-latin space at manual linebreaks[/-] [+]Unexpected cjk-latin space at manual line breaks[/+] on Jul 2, 2025
  4. changed the title [-]Unexpected cjk-latin space at manual line breaks[/-] [+]Redundant cjk-latin space at manual line breaks[/+] on Jul 2, 2025
  5. YDX-2147483647 commented on Jul 2, 2025

    @YDX-2147483647
    ContributorAuthor

    Another mystery: the behaviour in show + eval is inconsistent.

    The reason is that cjk-latin-spacing is none in show + eval.

    Outdated example
    #set page(width: auto, height: auto, margin: 1em)
    
    
    #{
      show raw: it => text(1.25em, eval(it.text, mode: "markup"))
    
      ```typ
      #set text(font: "Noto Serif CJK SC")
      #box(width: 2.3em, stroke: (x: green), align(end, [国国\ T T]))
      ```
    }
    
    #{
      let it = ```typ
      #set text(font: "Noto Serif CJK SC")
      #box(width: 2.3em, stroke: (x: green), align(end, [国国\ T T]))
      ```
    
      eval(it.text, mode: "markup")
    }

    Image

  6. changed the title [-]Redundant cjk-latin space at manual line breaks[/-] [+]Redundant CJK-Latin space at manual line breaks[/+] on Jul 2, 2025
  7. bluss commented on Jul 2, 2025

    @bluss
    Contributor

    @YDX-2147483647 as an aside but I think that's because the raw element changes cjk-latin-spacing.

    out.set(TextElem::set_cjk_latin_spacing(Smart::Custom(None)));

    It also changes lang to "en" which is another setting to keep track of in this context - all raw's changes need to be reset/counteracted unfortunately in this scenario.

  8. added
    cjk
    on Jul 2, 2025
  9. added a commit that references this issue on Aug 4, 2025
    283e84d
  10. added a commit that references this issue on Sep 10, 2025
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

    bugcjk

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions