You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Say, if typ file is using unicode, it is possible to know which language any character belongs to.
Then can typst provide a method to set different font for different language.
The code seems does not work even for texts explicitly specified language:
The font SimHei contains English letters, but Times New Roman doesn't contain any Chinese Character. So, this is a variant method of setting both Chinese and English font.
Reacted by Leedehai, Sunny Huang, Ilia, QuantumGhost, Yi-Xin Liu, suucss, Chao Kong, Nianyi Wang, Maplefurr and Zheyun
While the idea of language-dependent font settings works for the language pair illustrated above, since Unicode code points for Chinese characters are not in English alphabets at all, it could be a bit challenging/infeasible for Typst to scale this solution to other language pairs like Chinese/Japanese, since they have overlapping Unicode code points. Other language pairs may have similar issues as well.
To address this, maybe Typst needs to go a level lower: provide Unicode code range matching utilities (like CSS for web) so that users or package authors can configure fonts for different Unicode code points as they wish.
For Japanese, because it uses three scripts Han (Chinese characters, 漢字 Kanji), Hiragana and Katakana, we need to select all of them.
#settext(font: "Noto Serif CJK JP") // Set base font#showregex("[\p{scx:Han}\p{scx:Hira}\p{scx:Kana}]"): settext(font: "Noto Sans CJK JP") // Set Japanese font*Typstにおける和欧混植のフォント設定法*
For Chinese, we only need Han.
#settext(font: "Noto Serif CJK SC") // Set base font#showregex("[\p{scx:Han}]"): settext(font: "Noto Sans CJK SC") // Set Chinese font*利用Typst中西文混排不同字体*
Reacted by Fainabi, Hang Yan, Eîar Abramski-Kronenberg, LChris314, Logan Wang and Y.D.X.
After some research, I find out that the real problem is #1024 where selectors do not work on set rules, so that text.where(...) is not selecting anything.
The ideal descriptive approach of language selection is to firstly set the default language for each script used, then specify the language explicitly if the script is shared by multiple languages. As for now, this is not working because of the mentioned issue.
#settext(font: "Noto Serif CJK SC", lang: "zh", script: "HanS")
// #show regex("\p{sc:Latn}"): set text(lang: "en") // You cannot use regex here because of the following reason below#leten= text.with(lang: "en")
#letfr= text.with(lang: "fr")
#letja= text.with(lang: "ja")
#letHanT= text.with(script: "HanT")
#showtext.where(lang: "en"): settext(fill: red)
#showtext.where(lang: "fr"): settext(fill: blue)
#showtext.where(lang: "ja"): settext(fill: yellow)
#showtext.where(lang: "zh", script: "HanT"): settext(fill: green)
中文中混有#en[English]和#fr[français]以及#ja[日本語]的时候,#HanT[我們默認拉丁文字是英文,漢字是簡體字中文,除非例外指定]。
For now, you can use let keyword to specify style directly as an imperative approach:
For the regex approach to set a default language / style, because of show regex / show str has a higher priority (somewhat similar to specificity in CSS) than set rules, so even if you set English as default saying English和#fr[français] with #let fr = text.with(lang: "fr", fill: blue), since français matches /\p{sc:Latn}/, it will always be red same as English rather than blue. I wonder if it is possible to overwrite the priority (ignore text/regex matches inside a function).
Reacted by Eîar Abramski-Kronenberg and DongHo Jung
Activity
poetlife commentedon Nov 15, 2023
Seems we can use
regexexpression to set Chinese Character.This is the rendering result:

alerque commentedon Nov 15, 2023
Won't that result in
typstsetting the font explicitly for every character separately? Won't that screw with shaping and possibly bloat the output?laurmaedje commentedon Nov 15, 2023
Indeed. Adding a + to the regex would help somewhat, but it's probably still a bit brittle.
poetlife commentedon Nov 16, 2023
Maybe using Font Family is a better choice.
The font
SimHeicontains English letters, butTimes New Romandoesn't contain any Chinese Character. So, this is a variant method of setting both Chinese and English font.Leedehai commentedon Dec 22, 2023
While the idea of language-dependent font settings works for the language pair illustrated above, since Unicode code points for Chinese characters are not in English alphabets at all, it could be a bit challenging/infeasible for Typst to scale this solution to other language pairs like Chinese/Japanese, since they have overlapping Unicode code points. Other language pairs may have similar issues as well.
To address this, maybe Typst needs to go a level lower: provide Unicode code range matching utilities (like CSS for web) so that users or package authors can configure fonts for different Unicode code points as they wish.
mkpoli commentedon Jan 8, 2024
I just wrote a detailed article related to this issue in Japanese, for a temporary solution.
https://zenn.dev/mkpoli/articles/6234c1d2a595bd
To summary, it's the same to poetlife's approach, but with the usage of Unicode Properties.
For Japanese, because it uses three scripts
Han(Chinese characters, 漢字 Kanji),HiraganaandKatakana, we need to select all of them.For Chinese, we only need Han.
mkpoli commentedon Jan 8, 2024
After some research, I find out that the real problem is #1024 where
selectors do not work on set rules, so thattext.where(...)is not selecting anything.The ideal descriptive approach of language selection is to firstly set the default language for each script used, then specify the language explicitly if the script is shared by multiple languages. As for now, this is not working because of the mentioned issue.
For now, you can use
letkeyword to specify style directly as an imperative approach:For the regex approach to set a default language / style, because of
show regex/show strhas a higher priority (somewhat similar to specificity in CSS) than set rules, so even if you set English as default sayingEnglish和#fr[français]with#let fr = text.with(lang: "fr", fill: blue), sincefrançaismatches/\p{sc:Latn}/, it will always be red same asEnglishrather than blue. I wonder if it is possible to overwrite the priority (ignore text/regex matches inside a function).[-]setting different font with different language?[/-][+]Language-dependant font configuration[/+]