Skip to content

Commit

Permalink
Merge pull request #37 from earmer/cjk
Browse files Browse the repository at this point in the history
Cjk in Raw Update
  • Loading branch information
flaribbit authored Jan 26, 2025
2 parents a4bfcc2 + 5b3c723 commit 739377e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/FAQ/chinese-in-raw.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ tags: [font, chinese, raw]
#show raw : set text(font: ("DejaVu Sans Mono", "Noto Sans CJK SC"))
```

其中,「DejaVu Sans Mono」是等宽字体,「Noto Sans CJK SC」是一个你需要的中文字体。工作原理是等宽字体中没有中文字符,因此 `fallback` 到第二字体上。

因此,你需要需要保证你没有如 `#show raw: set text(fallback: false)` 的命令在你的文档中。**同时**要保证你的等宽字体中没有任何的 CJK 字符,否则会造成中文字体不统一。

相关 issue:https://github.com/typst/typst/issues/5748

![示例](../images/chinese-in-raw.png)

## 相关内容

- [代码块中西文间有多余的空格](./cjk-latin-spacing-in-raw.md)
- [代码块里多了空格/代码块的对齐非常奇怪](./code-block-justify.md)
42 changes: 42 additions & 0 deletions docs/FAQ/cjk-latin-spacing-in-raw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
tags: [font, chinese, raw, bug, spacing, layout]
---

# 代码块中西文间有多余的空格?

首先,你可能需要参考 [为什么中文字体这么奇怪](./strange-fonts.md) 对正文字体进行配置。然后参照 [为什么代码块里面的中文字体显示不正常?](./chinese-in-raw.md) 对于代码块内中文的进行修改。

## 它为什么是一个 Bug?

这是一个被官方开发者承认的 Bug(参见 [The feature CJK-Lantin-Spacing Misapplied on the Raw block](https://github.com/typst/typst/issues/5760)),并且需要注意的是,它可能看起来无关紧要,但是实际上有很大的造成误解的风险。例如,对于这种情况,这个特性就是无害的:

```cpp
while (true) {} // 典型的Textbook式死循环
```

但对于

```cpp
printf("这是%d段代码", 1);
```
就是有害的。因为在这种情况下,如果我们在编译的结果中在 `%d` 和 `段代码` 之间添加一个空格,它有歧义,因为我们无法判断它是否是一个空格还是一个中西文间距。
```typst
#show raw: set text(font: ("Monospace", "Source Han Serif SC"))
#raw(lang: "cpp", "printf(\"这是%d段代码\", 1)")
```

# 如何解决?

只要为源代码块取消这个特性即可。

```typst no-render
#show raw: set text(cjk-latin-spacing: none)
```

## 相关内容

- [为什么代码块里面的中文字体显示不正常?](./chinese-in-raw.md)
- [代码块中西文间有多余的空格](./cjk-latin-spacing-in-raw.md)
- [代码块里多了空格/代码块的对齐非常奇怪](./code-block-justify.md)

0 comments on commit 739377e

Please sign in to comment.