同一份技术报告的三语对决
主线位置:全景第二站 —— 同一个排版任务,三份可运行源码并排对比,用实测数据说话。
我们设定一个科研与工程报告里最常见的五合一任务:
- A4 纸张、2cm 页边距、底部居中显示页码;
- 章节自动按
1.1格式编号,并能在正文里通过标签交叉引用(显示为Section 1); - 一条行内欧拉公式 ,以及一条带自动编号
(1)的块级高斯积分公式,并在正文里交叉引用该公式; - 一张 3 列数据表格。
1. 三份源码交互式切换对照
点击下方标签页,可直接切换对比 Typst(17 行)、LaTeX(25 行) 与 Markdown(13 行) 编写同一份技术报告的源码差异:
#set page(paper: "a4", margin: 2cm, numbering: "1")
#set heading(numbering: "1.1")
#set math.equation(numbering: "(1)")
= Introduction <intro>
In @intro, we test *strong*, _emphasis_, and inline math $e^(i pi) + 1 = 0$.
$ integral_0^oo e^(-x^2) dif x = sqrt(pi) / 2 $ <gauss>
See @gauss and table below:
#table(
columns: 3,
[Engine], [Mode], [Pass],
[Typst], [Incremental], [1--2],
[LaTeX], [Macro], [2--3],
)\documentclass[a4paper]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{amsmath}
\usepackage{booktabs}
\begin{document}
\section{Introduction}\label{sec:intro}
In Section~\ref{sec:intro}, we test \textbf{strong}, \emph{emphasis}, and inline math $e^{i\pi} + 1 = 0$.
\begin{equation}\label{eq:gauss}
\int_0^\infty e^{-x^2}\,dx = \frac{\sqrt{\pi}}{2}
\end{equation}
See Equation~(\ref{eq:gauss}) and table below:
\begin{tabular}{lll}
\toprule
Engine & Mode & Pass \\
\midrule
Typst & Incremental & 1--2 \\
LaTeX & Macro & 2--3 \\
\bottomrule
\end{tabular}
\end{document}# Introduction
In Section 1, we test **strong**, *emphasis*, and inline math $e^{i\pi} + 1 = 0$.
$$\int_0^\infty e^{-x^2}\,dx = \frac{\sqrt{\pi}}{2}$$
| Engine | Mode | Pass |
| --- | --- | --- |
| Typst | Incremental | 1-2 |
| LaTeX | Macro | 2-3 |2. 逐行拆解三者的核心差异
| 任务点 | Markdown | LaTeX | Typst |
|---|---|---|---|
| 页面与边距配置 | 需写 YAML frontmatter 传给 Pandoc(geometry: margin=2cm) | \documentclass[a4paper]{article} + \usepackage[margin=2cm]{geometry} | #set page(paper: "a4", margin: 2cm, numbering: "1") |
| 章节与公式编号 | 无内置支持(需装 pandoc-crossref 滤镜) | 章节与 equation 默认编号,取消编号需写 \section* / equation* | 默认清爽不编号;按需开启 #set heading(numbering: "1.1") 与 #set math.equation(numbering: "(1)") |
| 打标签与引用 | 不支持,只能手写死数字 Section 1 | \label{sec:intro} + Section~\ref{sec:intro}(必须跑 2 遍 pdflatex,第 1 遍显示 Section ??) | 标题或公式末尾直接挂 <intro> / <gauss>,正文写 @intro / @gauss 自动带上前缀 Section 1 与 Equation (1) |
| 数学分式与上下标 | 借用 LaTeX 语法 \frac{\sqrt{\pi}}{2} | \int_0^\infty e^{-x^2}\,dx = \frac{\sqrt{\pi}}{2} | integral_0^oo e^(-x^2) dif x = sqrt(pi) / 2(/ 自动变分式,无需写 \frac{}{}) |
sequenceDiagram participant User as 开发者 participant Typst as typst compile one.typ participant LaTeX as pdflatex one.tex User->>Typst: 执行 1 条命令 Note over Typst: eval(1次) -> layout第1遍(收集<intro>/<gauss>) -> layout第2遍(填入Section 1与Eq 1) Typst-->>User: 37.3 ms 产出完整 PDF(引用全部就绪) User->>LaTeX: 第 1 次执行 pdflatex LaTeX-->>User: 299.7 ms 产出 PDF(正文显示 Section ?? 和 Equation (??)) User->>LaTeX: 第 2 次执行 pdflatex(读取 one.aux) LaTeX-->>User: 累计 584.2 ms 产出最终 PDF
3. M2 Max 编译耗时实测对比
我们在 /Users/hal/build/tools/exp-typst/ 下同时测试了两个规模的文档:
- 1 页短报告(
one.typ/one.tex/one.md):即上面的示例; - 60 节长文档(
math20.typ/math20.tex,共 12 页):包含开头自动目录(#outline()/\tableofcontents)、60 个编号小节、60 条带标签的求和公式以及 120 处跨节交叉引用。
每个命令连跑 7 轮取中位数,实测结果如下:
| 编译规模 | typst(--ignore-system-fonts) | typst(含系统字体扫描) | pdflatex(单遍,引用为 ??) | pdflatex(双遍,引用完整) | xelatex(双遍) | pandoc -> pdflatex |
|---|---|---|---|---|---|---|
| 1 页报告 | 37.3 ms | 178.7 ms | 299.7 ms | 584.2 ms | 831.5 ms | 1078.1 ms |
| 60 节长文档(含目录+120处引用) | 74.1 ms | 215.3 ms | 312.5 ms | 618.4 ms | 946.0 ms | 1642.0 ms |
从实测点图可以看到两个鲜明的规律:
- 单次命令完成收敛,速度领先一个数量级:在 60 节长文档且必须解析全部目录和交叉引用的公平条件下,
typst --ignore-system-fonts仅需 74.1 ms,比双遍pdflatex(618.4 ms)快 8.3 倍,比双遍xelatex(946.0 ms)快 12.8 倍。 - 文档增长 60 倍,排版耗时仅增加 36.8 ms:对比
typst带系统字体扫描的两列数据(1 页178.7 msvs 60 节215.3 ms),刨去固定的 ~141 ms macOS 系统字体目录扫描后,真正花在词法、求值、两遍收敛布局与 PDF 导出上的时间只从37.3 ms增长到了74.1 ms。
❗IMPORTANT
动脑筋:如果忘写
#set math.equation(numbering: "(1)") 会发生什么?在上面的 one.typ 中,如果我们删掉第 3 行的 #set math.equation(numbering: "(1)"),保留第 8 行的 $ ... $ <gauss> 和第 10 行的 See @gauss,运行 typst compile one.typ 会像 LaTeX 那样输出一个问号 ??,还是直接中断编译报错?报错信息会提示什么?
动脑筋答案
会直接中断编译并给出精准修复提示,绝不会悄悄生成带 ?? 的残缺 PDF。在 typst 0.15.1 下实测输出原文如下:
error: cannot reference equation without numbering
┌─ one.typ:6:4
│
6 │ See @gauss and table below:
│ ^^^^^^
│
= hint: you can enable equation numbering with `#set math.equation(numbering: "1.")`
这就是 Typst 与 LaTeX 在工程体验上的典型差异:LaTeX 默认允许对未就绪或无编号的标签写出 ?? 警告混过编译,而 Typst 把「引用一个没有编号的元素」视为编译期契约错误,并在 hint: 里直接把修复代码 #set math.equation(numbering: "1.") 喂到你嘴边。
小结
- 写 Typst 时,用
=替代#/\section,用<label>+@label替代\label+\ref,用#set替代导言区宏包配置。 - 下一部分(
02-syntax-vs-md-latex)我们将系统拆解 Typst 的三种词法模式与日常标记语法。
💬 关联讨论
加载中...

