Skip to content

Add page numbering support for Devanagari digits. #4843

Closed

Description

@starryprabin

Description

There are many South Asian users (such as me) who can benefit with the Devanagari numbering systems.

devanagari-digits

Use Case

Documents created in Nepali, Hindi, Marathi and other languages which use Devanagari writing system will benefit from it.

Activity

  1. added
    feature request
    on Aug 26, 2024
  2. FlorentCLMichel commented on Aug 27, 2024

    @FlorentCLMichel
    Contributor

    In the meantime, this workaround seems to give sensible results. (Definitely not claiming this is the best solution - I'm sure people more familiar with Typst than I am can come up with a better one.)

    #let convert-number(x) = x.replace(regex("[0-9]"), 
      m => {
      let t = m.at("text");
      ((t == "0", "\u{0966}"),
       (t == "1", "\u{0967}"),
       (t == "2", "\u{0968}"),
       (t == "3", "\u{0969}"),
       (t == "4", "\u{096a}"),
       (t == "5", "\u{096b}"),
       (t == "6", "\u{096c}"),
       (t == "7", "\u{096d}"),
       (t == "8", "\u{096e}"),
       (t == "9", "\u{096f}")).find(x => x.at(0)).at(1)
      })
    #set page(numbering: (..numbers) => convert-number(numbering("1", numbers.pos().at(0))))
    
  3. starryprabin commented on Aug 27, 2024

    @starryprabin
    Author

    Thanks for the great workaround—it worked well for the page numbers.

    There are other sections of the report where the numbering needs to be updated, like the table of contents and headings. I believe using #set heading(numbering: "१.") should also apply Devanagari numerals to the headings.

    Similarly, setting the heading with #set heading(numbering: "१.क") should use Devanagari letters for the second-level headings. Here, १ is the Devanagari numeral for "1," and क is the Devanagari letter for A This pattern should continue with १.ख, १.ग, etc. But this might be a separate discussion.

  4. FlorentCLMichel commented on Aug 27, 2024

    @FlorentCLMichel
    Contributor

    You're most welcome! Maybe the code below could be a partial solution for the page numbers and headers. (You will need to add more rows to the function convert-letters-devanagari if some sections have more than three subsections; in the code below I set the next symbols to ? as I don't know how Devanagari letters should be ordered. This code will also likely fail if there are more than two levels of headings.) This is most likely not the best solution, though - and only a workaround.

    #let convert-digits-devanagari(x) = x.replace(regex("[0-9]"), 
      m => {
      let t = m.at("text");
      ((t == "0", "\u{0966}"),
       (t == "1", "\u{0967}"),
       (t == "2", "\u{0968}"),
       (t == "3", "\u{0969}"),
       (t == "4", "\u{096a}"),
       (t == "5", "\u{096b}"),
       (t == "6", "\u{096c}"),
       (t == "7", "\u{096d}"),
       (t == "8", "\u{096e}"),
       (t == "9", "\u{096f}")).find(x => x.at(0)).at(1)
      })
    
    #let convert-letters-devanagari(x) = x.replace(regex("[a-z]"), 
      m => {
      let t = m.at("text");
      ((t == "a", "क"),
       (t == "b", "ख"),
       (t == "c", "ग"),
       (true, "?")).find(x => x.at(0)).at(1)
      })
    
    #set page(numbering: (..numbers) => convert-digits-devanagari(numbering("1", numbers.pos().first())))
    
    #set heading(numbering: (..numbers) => {
      let level = numbers.pos().len()
      if (level == 1) {
        return convert-digits-devanagari(numbering("1", numbers.pos().at(0)))
      } else if (level == 2) {
        return convert-digits-devanagari(numbering("1", numbers.pos().at(0))) + "." + convert-letters-devanagari(numbering("a", numbers.pos().at(1)))
      }
    })
    
  5. starryprabin commented on Aug 28, 2024

    @starryprabin
    Author

    Thank you @FlorentCLMichel for your workaround. It worked beautifully. Wishing you all the best.

  6. added
    model
    on Aug 31, 2024
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 requestmodel

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions