Typst-table

Typst table 官网文档
Typst table guide

介绍

Table 用 cells 来组织内容. Cells 可以是任意 content.

创建 basic table

table 函数来创建 table, 主要需要给:

  • Columns 的数量
  • 每一个 Cell 的内容

如:

1
2
3
4
5
6
7
8
9
10
11
12
13
#table(
columns: 2,
[*Amount*], [*Ingredient*],
[360g], [Baking flour],
[250g], [Butter (room temp.)],
[150g], [Brown sugar],
[100g], [Cane sugar],
[100g], [70% cocoa chocolate],
[100g], [35-40% cocoa chocolate],
[2], [Eggs],
[Pinch], [Salt],
[Drizzle], [Vanilla extract],
)

一般用 table.header 函数来包裹 header (也就是第一行):

1
2
3
4
5
6
7
8
9
10
11
12
13
#table(
columns: 2,
table.header[*Amount*][*Ingredient*],
[360g], [Baking flour],
[250g], [Butter (room temp.)],
[150g], [Brown sugar],
[100g], [Cane sugar],
[100g], [70% cocoa chocolate],
[100g], [35-40% cocoa chocolate],
[2], [Eggs],
[Pinch], [Salt],
[Drizzle], [Vanilla extract],
)

利用 cell 的纵坐标进行匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#show table.cell.where(y: 0): strong

#table(
columns: 2,
table.header[*Amount*][*Ingredient*],
[360g], [Baking flour],
[250g], [Butter (room temp.)],
[150g], [Brown sugar],
[100g], [Cane sugar],
[100g], [70% cocoa chocolate],
[100g], [35-40% cocoa chocolate],
[2], [Eggs],
[Pinch], [Salt],
[Drizzle], [Vanilla extract],
)

调整 column sizes

通过修改 columns 的值 (这个时候就不是指定个数了). 默认情况下, typst 会以一列中 largest cell 的大小为 column 的宽度.

有多种调整 column width 的方式:

  • auto, typst 通过 content 自动调整
  • Lengths, 如 6cm, 0.7in, 120pt, 即提供一个确切长度, 包含单位
  • Percentage, 如 40%, 指 table total horizontal space 的 40%
  • 用单位 fr, fractional part of the free space. Typst 会先计算不用 fr 定义的 column 的宽度, 然后剩下的宽度平分到用 fr 定义的 column 上

如:

1
2
3
4
5
6
7
#table(
columns: (auto, auto, 1fr),
table.header[Date][°No][Description],
[24/01/03], [813], [Filtered participant pool],
[24/01/03], [477], [Transitioned to sec. regimen],
[24/01/11], [051], [Cycled treatment substrate],
)

给 table 添加 caption 和 label

需结合 figure 函数使用, 此时的 table 就像 image 一样用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#show table.cell.where(y: 0): set text(weight: "bold")

#figure(
table(
columns: 4,
stroke: none,

table.header[Test Item][Specification][Test Result][Compliance],
[Voltage], [220V ± 5%], [218V], [Pass],
[Current], [5A ± 0.5A], [4.2A], [Fail],
),
caption: [Probe results for design A],
) <probe-a>

The results from @probe-a show that the design is not yet optimal.
We will show how its performance can be improved in this section.

去掉 table 的边框

设置 stroke argument 为 none:

1
2
3
4
5
6
7
8
table(
columns: 4,
stroke: none,

table.header[Test Item][Specification][Test Result][Compliance],
[Voltage], [220V ± 5%], [218V], [Pass],
[Current], [5A ± 0.5A], [4.2A], [Fail],
),

条带化效果的 table

需使用 tablefill 以及 stroke argument, 其接受三种参数:

  • single color
  • A array of color, Typst 会循环使用 array 中的 color 来填充
  • function, 以 xy 为参数, 返回值用于 fill

使用 a array of color 如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#set text(font: "IBM Plex Sans")

// Medium bold table header.
#show table.cell.where(x: 1): set text(weight: "medium")

// Bold titles.
#show table.cell.where(y: 0): set text(weight: "bold")

// See the strokes section for details on this!
#let frame(stroke) = (x, y) => (
left: if x > 0 { 0pt } else { stroke },
right: stroke,
top: if y < 2 { stroke } else { 0pt },
bottom: stroke,
)

#set table(
fill: (rgb("EAF2F5"), none),
stroke: frame(rgb("21222C")),
)

#table(
columns: (0.4fr, 1fr, 1fr, 1fr),

table.header[Month][Title][Author][Genre],
[January], [The Great Gatsby], [F. Scott Fitzgerald], [Classic],
[February], [To Kill a Mockingbird], [Harper Lee], [Drama],
[March], [1984], [George Orwell], [Dystopian],
[April], [The Catcher in the Rye], [J.D. Salinger], [Coming-of-Age],
)

传入一个 function 如:

1
2
3
4
#set table(
fill: (_, y) => if calc.odd(y) { rgb("EAF2F5") },
stroke: frame(rgb("21222C")),
)

只是设置 tablestroke 达到的效果:

1
2
3
4
5
6
7
#set table(
stroke: (x, y) => (
y: 1pt,
left: if x > 0 { 0pt } else if calc.even(y) { 1pt },
right: if calc.even(y) { 1pt },
),
)

手动为每一个 cell 填充的示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#set text(font: "Roboto")

#let cdu(name) = ([CDU], table.cell(fill: black, text(fill: white, name)))
#let spd(name) = ([SPD], table.cell(fill: red, text(fill: white, name)))
#let fdp(name) = ([FDP], table.cell(fill: yellow, name))

#table(
columns: (auto, auto, 1fr),
stroke: (x: none),

table.header[Tenure][Party][President],
[1949-1959], ..fdp[Theodor Heuss],
[1959-1969], ..cdu[Heinrich Lübke],
[1969-1974], ..spd[Gustav Heinemann],
[1974-1979], ..fdp[Walter Scheel],
[1979-1984], ..cdu[Karl Carstens],
[1984-1994], ..cdu[Richard von Weizsäcker],
[1994-1999], ..cdu[Roman Herzog],
[1999-2004], ..spd[Johannes Rau],
[2004-2010], ..cdu[Horst Köhler],
[2010-2012], ..cdu[Christian Wulff],
[2012-2017], [n/a], [Joachim Gauck],
[2017-], ..spd[Frank-Walter-Steinmeier],
)

Typst-table
http://example.com/2024/05/16/Typst-table/
作者
Jie
发布于
2024年5月16日
许可协议