Typst 现代排版系统¶
一句话说明: Typst 是 LaTeX 的现代替代品,语法简洁直观、编译速度极快(毫秒级),原生支持增量编译与实时预览。
为什么要学¶
- 编译速度碾压 LaTeX — 增量编译在毫秒级完成,告别等待数秒甚至数十秒的编译循环
- 语法极简 — 类 Markdown 的标记语言,学习成本远低于 LaTeX 的反斜杠地狱
- 内置现代特性 — 原生支持脚本语言、样式系统、包管理,无需安装数百兆宏包
- 错误信息友好 — 精确到行列的报错,不再面对 LaTeX 的天书日志
- 适用场景广 — 论文、简历、幻灯片、书籍、笔记均可胜任
核心概念详解¶
Typst 的三种模式¶
| 模式 | 触发方式 | 用途 | 示例 |
|---|---|---|---|
| 标记模式 | 默认 | 写正文内容 | = 标题 *粗体* |
| 代码模式 | # 前缀 | 调用函数/逻辑 | #set text(size: 12pt) |
| 数学模式 | $...$ | 公式排版 | $sum_(i=0)^n i$ |
与 LaTeX 对比¶
| 特性 | Typst | LaTeX |
|---|---|---|
| 编译速度 | 毫秒级增量 | 秒级全量 |
| 语法风格 | 类 Markdown + 函数式 | 反斜杠命令 |
| 包管理 | 内置 @preview/ | texlive/tlmgr |
| 错误提示 | 精确友好 | 晦涩冗长 |
| 脚本能力 | 内置图灵完备脚本 | TeX 宏(极难调试) |
| 中文支持 | 原生 Unicode | 需 xelatex/ctex |
| 学习曲线 | 1-2 天上手 | 1-2 周入门 |
核心概念¶
- Content — Typst 中一切输出都是 content 类型
- Set rules —
#set修改元素默认样式(级联生效) - Show rules —
#show自定义元素渲染方式 - 函数 — Typst 中的 heading、image 等本质都是函数
安装与配置¶
方式一:CLI 安装¶
# macOS
brew install typst
# Linux (cargo)
cargo install --git https://github.com/typst/typst --locked typst-cli
# Windows (scoop)
scoop install typst
方式二:在线编辑器¶
访问 typst.app 即可使用在线协作编辑器,无需本地安装。
编辑器集成¶
# VS Code 插件(推荐)
code --install-extension nvarner.typst-lsp
code --install-extension mgt19937.typst-preview
验证安装¶
快速上手¶
创建 hello.typ:
#set page(paper: "a4", margin: 2cm)
#set text(font: "Noto Serif CJK SC", size: 11pt)
#set heading(numbering: "1.1")
= 我的第一篇 Typst 文档
== 简介
Typst 是一个*现代排版系统*,具有以下特点:
- 编译速度极快
- 语法直观简洁
- 内置脚本语言
== 数学公式
行内公式 $E = m c^2$,行间公式:
$ integral_0^infinity e^(-x^2) dif x = sqrt(pi) / 2 $
== 代码块
```python
def hello():
print("Hello from Typst!")
== 图片与表格
figure(¶
table( columns: 3, [名称], [版本], [语言], [Typst], [0.12], [Rust], [LaTeX], [2024], [TeX], ), caption: [排版系统对比], )
进阶用法¶
1. 自定义模板¶
// template.typ
#let project(title: "", authors: (), body) = {
set document(title: title, author: authors)
set page(paper: "a4", margin: (x: 2.5cm, y: 3cm))
set text(font: "Noto Serif CJK SC", size: 11pt)
set par(justify: true, leading: 0.8em)
// 标题页
align(center)[
#v(4cm)
#text(size: 24pt, weight: "bold")[#title]
#v(1cm)
#authors.join(", ")
#v(1cm)
#datetime.today().display("[year]年[month]月[day]日")
]
pagebreak()
// 目录
outline(depth: 3)
pagebreak()
body
}
使用模板:
#import "template.typ": project
#show: project.with(
title: "研究报告",
authors: ("张三", "李四"),
)
= 引言
正文内容...
2. 使用社区包¶
// 引用社区包
#import "@preview/cetz:0.3.0": canvas, draw
#import "@preview/tablex:0.0.8": tablex
// 使用 cetz 绘图
#canvas({
draw.circle((0, 0), radius: 1)
draw.line((0, 0), (2, 1), stroke: blue)
})
3. 条件逻辑与循环¶
// 循环生成内容
#for i in range(1, 6) {
[第 #i 章内容概要 \ ]
}
// 条件渲染
#let draft = true
#if draft {
text(fill: red)[【草稿版本,请勿分发】]
}
4. 参考文献¶
5. 幻灯片(Polylux)¶
#import "@preview/polylux:0.4.0": *
#set page(paper: "presentation-16-9")
#polylux-slide[
#align(horizon + center)[
= 演示标题
作者名字
2024年12月
]
]
#polylux-slide[
== 要点
- 第一点
- 第二点
- 第三点
]
常见问题与排错¶
Q: 中文显示为方块/乱码¶
查看可用字体:typst fonts
Q: 图片找不到¶
Typst 使用相对路径,确保图片在 .typ 文件同级或子目录:
Q: 如何实现 LaTeX 中的 \newcommand¶
// 用函数替代
#let note(body) = {
rect(fill: luma(240), inset: 10pt, width: 100%)[
*注意:* #body
]
}
#note[这是一条提示信息]
Q: 编译报错 "unknown variable"¶
检查是否遗漏 # 前缀。在标记模式中调用函数必须加 #。
Q: 如何锁定包版本¶
参考资源¶
- 官方文档:https://typst.app/docs
- Typst 仓库:https://github.com/typst/typst
- 社区包索引:https://typst.app/universe
- Awesome Typst:https://github.com/qjcg/awesome-typst
- Typst 中文社区:https://github.com/typst-cn
- 从 LaTeX 迁移指南:https://typst.app/docs/guides/guide-for-latex-users