跳转至

Typst 现代排版系统

一句话说明: Typst 是 LaTeX 的现代替代品,语法简洁直观、编译速度极快(毫秒级),原生支持增量编译与实时预览。


为什么要学

  1. 编译速度碾压 LaTeX — 增量编译在毫秒级完成,告别等待数秒甚至数十秒的编译循环
  2. 语法极简 — 类 Markdown 的标记语言,学习成本远低于 LaTeX 的反斜杠地狱
  3. 内置现代特性 — 原生支持脚本语言、样式系统、包管理,无需安装数百兆宏包
  4. 错误信息友好 — 精确到行列的报错,不再面对 LaTeX 的天书日志
  5. 适用场景广 — 论文、简历、幻灯片、书籍、笔记均可胜任

核心概念详解

Typst 的三种模式

模式触发方式用途示例
标记模式默认写正文内容= 标题 *粗体*
代码模式# 前缀调用函数/逻辑#set text(size: 12pt)
数学模式$...$公式排版$sum_(i=0)^n i$

与 LaTeX 对比

特性TypstLaTeX
编译速度毫秒级增量秒级全量
语法风格类 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

验证安装

typst --version
# typst 0.12.0

快速上手

创建 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: [排版系统对比], )

编译:

```bash
# 编译为 PDF
typst compile hello.typ

# 实时监视(保存即编译)
typst watch hello.typ

进阶用法

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. 参考文献

// 使用 BibTeX 文件
#bibliography("refs.bib", style: "ieee")

// 正文中引用
 @smith2024 所述...

5. 幻灯片(Polylux)

#import "@preview/polylux:0.4.0": *

#set page(paper: "presentation-16-9")

#polylux-slide[
  #align(horizon + center)[
    = 演示标题
    作者名字

    2024年12月
  ]
]

#polylux-slide[
  == 要点
  - 第一点
  - 第二点
  - 第三点
]

常见问题与排错

Q: 中文显示为方块/乱码

// 确保设置了中文字体
#set text(font: ("Noto Serif CJK SC", "Source Han Serif SC"))

查看可用字体:typst fonts

Q: 图片找不到

Typst 使用相对路径,确保图片在 .typ 文件同级或子目录:

#image("figures/plot.png", width: 80%)

Q: 如何实现 LaTeX 中的 \newcommand

// 用函数替代
#let note(body) = {
  rect(fill: luma(240), inset: 10pt, width: 100%)[
    *注意:* #body
  ]
}

#note[这是一条提示信息]

Q: 编译报错 "unknown variable"

检查是否遗漏 # 前缀。在标记模式中调用函数必须加 #

Q: 如何锁定包版本

// @preview/ 包自带版本号,已自动锁定
#import "@preview/cetz:0.3.0"  // 明确版本

参考资源

  • 官方文档: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