Skip to content

Thai text justification support #8413

Open

Description

@pungpondsalami

Description

Thai language does not use spaces between words, which causes Typst's justification engine to treat entire sentences as single words. This results in uneven spacing between characters when justify: true is set, as Typst cannot determine valid line-break positions within Thai text.

Use Case

Thai users writing academic papers, theses, or formal documents require proper text justification. Currently, setting #set par(justify: true) with Thai text produces uneven character spacing. Proper Thai justification would make Typst viable for Thai academic and professional document typesetting, which is a significant use case given that LaTeX remains the primary alternative solely due to this limitation.

Activity

  1. added
    feature request
    on Jun 3, 2026
  2. added
    text
    i18n
    on Jun 3, 2026
  3. isuffix commented on Jun 3, 2026

    @isuffix
    Collaborator

    Theoretically we should be able to linebreak Thai text. Typst uses the ICU4X line segmenter and currently constructs it with LineSegmenter::try_new_lstm_with_buffer_provider, whose documentation says:

    Constructs a LineSegmenter with an invariant locale, custom LineBreakOptions, and compiled LSTM data for complex scripts (Khmer, Lao, Myanmar, and Thai).

    The LSTM, or Long Term Short Memory, is a machine learning model. It is smaller than the full dictionary but more expensive during segmentation (inference).

    The current version does use ICU4X v1.4, although we are hoping to upgrade to v2.2, but I don't know whether that has improved any line segmenting for Thai text.

    If you could provide a code block of text that doesn't linebreak which should, I can look into this and see if updating the ICU4X version or changing the line segmenter affects it.

    Also, is this only an issue with justify: true, or are there issues with linebreaking in the other justification options?

  4. pungpondsalami commented on Jun 4, 2026

    @pungpondsalami
    Author

    Here are two comparison screenshots:
    Image
    Typst (with justify: true) — notice the large uneven gaps between words, especially visible in lines like "LaTeX ถือเป็น" and "ยาวนาน ด้วย"
    Image
    LibreOffice Writer (same text) — showing the expected Thai justification behavior, where spacing is distributed more evenly

    The issue appears with justify: true but I haven't tested other justification options yet.

  5. isuffix commented on Jun 23, 2026

    @isuffix
    Collaborator

    I investigated this just a bit more with Typst 0.15, and unfortunately there are no magic fixes from upgrading ICU4X.

    Here's a Webapp project that has copyable Thai text displaying the bad spacing. I didn't see any real changes from mucking with the other text or par parameters, but inserting zero width spaces in-between characters did improve things.

  6. admannon commented on Jul 12, 2026

    @admannon

    I did some testing with a recent Typst version and got different results from the original issue report.

    For reproducibility, I used:

    With plain justify: true, the output is indeed not ideal for Thai text. However, after adjusting justification-limits, the resulting paragraph looks significantly better and much closer to the behavior of LibreOffice or Word.

    #set par(
      justify: true,
      justification-limits: (
        tracking: (min: -1em, max: 1em)
      )
    )

    Based on these results, I don't think the current problem is that Typst completely lacks Thai justification support or treats Thai sentences as unbreakable words. At the very least, the current behavior seems significantly better than what is described in the original report.

    Instead, it seems that:

    • Thai line segmentation appears to be working.
    • Thai justification already appears possible.
    • The default justification parameters are often too restrictive for Thai text.
    • There may still be bugs or undesirable interactions with the optimized line-breaking algorithm (for example, extra spacing near the beginning or end of lines). (see: Wrong spacing for Thai words after a line break. #5543)

    I also observed that linebreaks: "simple" sometimes produces more natural-looking Thai paragraphs than the default optimized line breaker.

    For reference, I'm attaching the full test document used to generate the screenshots.

    Typst source
    #set page(paper: "a4", margin: 30mm)
    
    #set text(font: "Sarabun", size: 12pt)
    
    #set par(first-line-indent: (amount: 1.27cm, all: true))
    #show heading: set block(below: 1.2em)
    
    #let content = [
      ในแวดวงการจัดพิมพ์เอกสารทางวิชาการ LaTeX ถือเป็นมาตรฐานหลักที่ได้รับความนิยมมาอย่างยาวนาน ด้วยระบบนิเวศที่แข็งแกร่งและแพ็กเกจที่ครอบคลุมทุกการใช้งาน อย่างไรก็ตาม ผู้ใช้อาจต้องใช้เวลาในการเรียนรู้โครงสร้างโค้ดที่มีความซับซ้อน ในขณะที่ Typst ก้าวเข้ามาเป็นทางเลือกใหม่ที่น่าจับตามอง โดยซูจุด
      เด่นเรื่องไวยากรณ์ที่เรียบง่ายคล้าย Markdown  ซึ่งช่วยให้เรียนรู้ได้รวดเร็ว พร้อมระบบประมวลผลแบบทันที (Instant Preview) แม้ว่า Typst จะยังอยู่ในช่วงเริ่มต้นและมีชุมชนผู้ใช้งานขนาดเล็ก แต่ก็นับเป็นตัวเลือกที่มีประสิทธิภาพสำหรับผู้ที่มองหาความคล่องตัวในการทำงาน
    
      In the typesetting of academic documents, LaTeX has long been a standard, widely accepted for its robust ecosystem and comprehensive package availability. However, users may need to invest time in learning its complex code structure. In contrast, Typst emerges as an interesting new alternative, offering a simple Markdown-like syntax that facilitates rapid learning. Additionally, it provides an Instant Preview feature. While Typst is still in its early stages and has a smaller user base, it stands out as a capable option for those seeking flexibility in their work.
    ]
    
    == Original Content
    
    #content
    
    #set par(justify: true)
    
    == Justified Content
    
    #content
    
    #set par(justify: true, justification-limits: (tracking: (min: 0em, max: 1em)))
    
    == Justified Content with tracking max limit
    
    #content
    
    #set par(justification-limits: (tracking: (min: -1em, max: 1em)))
    
    == Justified Content with tracking min & max limit
    
    #content
    
    #set par(linebreaks: "simple")
    
    == Simple Line Breaks
    
    #content

    I also found a potentially unrelated issue involving U+2060 WORD JOINER not preventing line breaks in some Thai cases and may open a separate issue for that.

  7. MankhongGarden commented on Oct 9, 2026

    @MankhongGarden

    Adding a related data point on Thai line breaking (not justification), with justify: false, since @isuffix asked for code blocks. Typst 0.15.1, Windows, font Leelawadee UI (Tahoma gives the same breaks).

    The default segmenter breaks inside common compound words:

    #set page(width: 4.6cm, height: auto, margin: 3mm)
    #set text(font: "Leelawadee UI", size: 11pt, lang: "th")
    #set par(justify: false)
    
    ผู้ป่วยเดินทางไปโรงพยาบาลประจำจังหวัด
    
    ผู้ป่วยเดินทางไปพวกเราทุกคนโรงพยาบาลประจำจังหวัด
    
    แพทย์แนะนำให้ออกกำลังกายสม่ำเสมอ และพักผ่อนให้เพียงพอ

    Breaks produced: โรง|พยาบาล (hospital), ทุก|คน (everyone), จัง|หวัด (province), กำลัง|กาย (in ออกกำลังกาย, exercise). A Thai reader sees each of these as a broken word.

    What I tried:

    1. ZWSP between words only (from a dictionary segmenter, PyThaiNLP newmm): adds correct break points, but Typst still breaks inside the words as above.
    2. ZWSP between words + U+2060 WORD JOINER between grapheme clusters inside each word: worse. Typst breaks right at some of the joiners, mid-syllable (ออกกำลั|งกาย, ประชุ|ม). This matches the WORD JOINER observation in the comment above.
    3. ZWSP between words + wrap each Thai run in a box: correct in every case I tried, including headings and text that comes from string variables:
    #show regex("[\u{0E00}-\u{0E7F}]+"): it => box(it)

    Because ZWSP is outside the Thai block, each regex match is exactly one pre-segmented word, so the box makes it unbreakable and the only remaining break points are the ZWSPs. Keep justify: false with this, because boxes don't stretch.

    So a workaround exists, but it needs an external segmenter. It might also be a useful test case if the ICU4X upgrade or a dictionary-based Thai segmenter is evaluated: the LSTM model seems to treat these compounds as separable.

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

    feature requesti18ntext

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions