提示:本教程存在过时问题,更详细教程请阅读 小蓝书Typst 中文社区导航 FAQ
注意:该中文文档为社区驱动的非官网文档,可能存在错译、漏译或过时等问题,请以官网文档 Documentation 为准,如发现错漏,也欢迎 您的贡献镜像)。Typst 非官方中文交流 QQ 群:793548390
Warning: the Chinese document is a community-driven non-official document, there may be mistranslation, omission or outdated problems, please refer to the official website documentation.
Typst 中文文档

grid 元素
元素函数可以通过 set 和 show 规则进行样式自定义。

Arranges content in a grid.

The grid element allows you to arrange content in a grid. You can define the number of rows and columns, as well as the size of the gutters between them. There are multiple sizing modes for columns and rows that can be used to create complex layouts.

The sizing of the grid is determined by the track sizes specified in the arguments. Because each of the sizing parameters accepts the same values, we will explain them just once, here. Each sizing argument accepts an array of individual track sizes. A track size is either:

To specify a single track, the array can be omitted in favor of a single value. To specify multiple auto tracks, enter the number of tracks instead of an array. For example, columns: 3 is equivalent to columns: (auto, auto, auto).

Examples

The example below demonstrates the different track sizing options.

// We use `rect` to emphasize the
// area of cells.
#set rect(
  inset: 8pt,
  fill: rgb("e4e5ea"),
  width: 100%,
)

#grid(
  columns: (60pt, 1fr, 2fr),
  rows: (auto, 60pt),
  gutter: 3pt,
  rect[Fixed width, auto height],
  rect[1/3 of the remains],
  rect[2/3 of the remains],
  rect(height: 100%)[Fixed height],
  image("tiger.jpg", height: 100%),
  image("tiger.jpg", height: 100%),
)
Preview

You can also spread an array of strings or content into a grid to populate its cells.

#grid(
  columns: 5,
  gutter: 5pt,
  ..range(25).map(str)
)
Preview

参数
参数是函数的输入,它们在函数名称后面的括号中传入。

columns
auto int relative fraction array
可用 Set 规则
可以使用 Set 规则,为函数后续所有的使用设置默认参数。

The column sizes.

Either specify a track size array or provide an integer to create a grid with that many auto-sized columns. Note that opposed to rows and gutters, providing a single track size will only ever create a single column.

默认:()

rows
auto int relative fraction array
可用 Set 规则
可以使用 Set 规则,为函数后续所有的使用设置默认参数。

The row sizes.

If there are more cells than fit the defined rows, the last row is repeated until there are no more cells.

默认:()

gutter
auto int relative fraction array
可用 Set 规则
可以使用 Set 规则,为函数后续所有的使用设置默认参数。

The gaps between rows & columns.

If there are more gutters than defined sizes, the last gutter is repeated.

默认:()

column-gutter
auto int relative fraction array
可用 Set 规则
可以使用 Set 规则,为函数后续所有的使用设置默认参数。

The gaps between columns. Takes precedence over gutter.

默认:()

row-gutter
auto int relative fraction array
可用 Set 规则
可以使用 Set 规则,为函数后续所有的使用设置默认参数。

The gaps between rows. Takes precedence over gutter.

默认:()

children
content
必需参数位置参数
位置参数按顺序传入,不带名称。
可变参数
可变参数可以多次传入。

The contents of the grid cells.

The cells are populated in row-major order.