← 返回速查表合集

Typst 基本语法速查

覆盖:文本与样式、列表、代码、函数、公式、表格、引用与页面设置。

1) 文本与标题

语法
= 一级标题
== 二级标题

这是普通文本,支持 *强调* 和 _斜体_。
行内代码:`let x = 1`

#strong[加粗内容]
#emph[强调内容]
效果预览

一级标题

二级标题

这是普通文本,支持 强调斜体
行内代码:let x = 1
加粗内容
强调内容

2) 列表与任务项

语法
- 无序项 A
- 无序项 B
  - 子项

+ 有序项 1
+ 有序项 2

- [ ] 待办
- [x] 已完成
效果预览
  • 无序项 A
  • 无序项 B
    • 子项
  1. 有序项 1
  2. 有序项 2
☐ 待办
☑ 已完成

3) 函数与变量

语法
#let name = "Typst"
#let square(x) = x * x

你好,#name!
9 的平方是 #square(9)。

#for i in range(1, 4) [
  第 #i 行
]
效果预览
你好,Typst!
9 的平方是 81。
第 1 行
第 2 行
第 3 行

4) 条件与内容块

语法
#let score = 92

#if score >= 90 [
  等级:A
] else if score >= 80 [
  等级:B
] else [
  等级:C
]
效果预览
score = 92
等级:A

5) 数学公式

语法
行内公式:$a^2 + b^2 = c^2$

块公式:
$ sum_(i=1)^n i = (n(n+1))/2 $

$ integral_0^1 x^2 dif x = 1/3 $
效果预览
行内公式:a² + b² = c²
i=1n i = n(n + 1) / 2
01 x² dx = 1/3

5.1) 数学公式排版细节(对齐 / 矩阵 / 编号)

语法
// 公式编号与样式
#set math.equation(numbering: "(1)")
#show math.equation: set text(size: 11pt)

// 多行对齐(& 对齐点,\ 换行)
$ x &= y + z \
    &= t $

// 分段函数
$ f(x) = cases(
  x^2, x >= 0;
  -x,  x < 0;
) $

// 矩阵
$ A = mat(
  1, 2;
  3, 4;
) $

// 重音与标注
$ hat(x), bar(y), vec(v) $
$ overbrace(a + b, "sum") $

// 编号与引用
$ e^(i pi) + 1 = 0 $ <eq:euler>
见式 @eq:euler。
效果预览
x = y + z
= t
f(x) = { x², x ≥ 0; -x, x < 0 }
A = ⎡1 2⎤
⎣3 4⎦
x̂, ȳ, vec(v)
overbrace(a + b) = sum
e + 1 = 0 (1)
见式 (1)。

6) 表格与对齐

语法
#table(
  columns: (1fr, 1fr, 1fr),
  align: center,
  [Name], [Score], [Grade],
  [Alice], [95], [A],
  [Bob], [82], [B],
)
效果预览
NameScoreGrade
Alice95A
Bob82B

7) 图片与引用

语法
#figure(
  image("demo.png", width: 70%),
  caption: [示例图片]
) <fig:demo>

如图 @fig:demo 所示。
效果预览
demo.png
图 1:示例图片
如图 图 1 所示。

8) 页面与样式配置

语法
#set page(
  paper: "a4",
  margin: 2cm,
)

#set text(
  font: "Noto Serif CJK SC",
  size: 11pt,
)

#set heading(numbering: "1.")
效果预览
页面:A4,页边距 2cm
正文字体:Noto Serif CJK SC,11pt
标题编号:1. 2. 3. …

9) 宏定义与调用

语法
#let badge(text, color: blue) = [
  #set text(fill: color, weight: "bold")
  [#text]
]

#let callout(title, body) = [
  #block(
    inset: 8pt,
    stroke: 1pt + gray,
    radius: 4pt,
    [*#title*]
    #linebreak()
    #body
  )
]

#badge("NEW")
#badge("WARN", color: red)

#callout(
  title: "提示",
  body: [这是通过宏渲染的内容。]
)
效果预览
NEW
WARN
提示
这是通过宏渲染的内容。

10) 常见内置函数

语法
// 文本与强调
#text(), #strong(), #emph(), #underline()
#sub(), #super(), #smallcaps(), #link()

// 页面与布局
#page(), #par(), #block(), #box(), #align()
#pad(), #place(), #columns(), #stack()
#grid(), #table()

// 图像与图形
#figure(), #image(), #line(), #rect(), #circle()

// 引用与文献
#label(), #ref(), #footnote()
#bibliography(), #cite(), #outline()

// 代码与计算
#range(), #len(), #type(), #assert()
#calc.abs(), #calc.max(), #calc.sqrt()
#rgb(), #cmyk(), #luma()

// grid 示例
#grid(
  columns: (1fr, 1fr, 1fr),
  gutter: 6pt,
  [A], [B], [C],
  grid.cell(colspan: 2)[D],
  [E],
)
效果预览
常用分类
文本:text/strong/emph/link
布局:page/block/box/align/grid/table
图形:figure/image/line/rect/circle
引用:label/ref/cite/bibliography
计算:range/len/calc.xxx/rgb
grid 渲染示意
A
B
C
D(跨两列)
E

11) 高频函数参数速查(签名 + 含义)

函数 常见参数 参数含义 / 常见取值
text(...) fill, size, font, weight 文字颜色、字号、字体、字重(如 "bold" 或数值)。
page(...) paper, margin, header, footer 纸张尺寸(如 "a4")、页边距、页眉页脚内容。
block(...) inset, outset, stroke, fill, radius 块内边距/外边距、边框、背景、圆角,常用于卡片/提示框。
align(...) left/center/right,以及 top/bottom 内容对齐方式,可组合水平和垂直方向。
pad(...) x, y, left/right/top/bottom 给内容增加留白,常用于微调布局间距。
grid(...) columns, rows, gutter, align 网格布局核心参数:列宽/行高、间距、单元对齐。配合 grid.cell(colspan:, rowspan:) 做跨行跨列。
table(...) columns, align, stroke, inset, fill 表格列定义、对齐、边框、单元内边距和底色。
image(path, ...) width, height, fit, alt 图片尺寸与适配方式(如 contain/cover),alt 用于可访问性。
figure(body, ...) caption, kind, placement 包裹图/表并带编号标题,可用于交叉引用。
ref(label) label 引用目标标签(图、表、章节等),渲染为对应编号文本。
bibliography(file, ...) title, style 加载文献库(如 .bib),并控制参考文献输出样式。
cite(key, ...) form, style, loc 在正文插入引用,可调引用显示形式与附加定位。
range(start, end, step) start, end, step 生成序列,常配合 for 循环渲染重复结构。
calc.*(...) abs/min/max/sqrt/pow/round 数值计算函数集合;参数通常为数字或长度值。
rgb(...), cmyk(...), luma(...) 颜色通道值(0~255 或 0~1) 构造颜色对象,常传给 fill / stroke / text(fill: ...)
提示:Typst 大量语法都通过 #函数[内容]#set/#show 组合扩展。