Download chp06.typ from m-rui001/typst_reference: direct link, hf CLI and curl.
- Browser
- Download file 115 kB
-
https://huggingface.co/datasets/m-rui001/typst_reference/resolve/main/chp06.typ
- Command line
-
hf download hf://datasets/m-rui001/typst_reference/chp06.typ
-
curl -L -o chp06.typ https://huggingface.co/datasets/m-rui001/typst_reference/resolve/main/chp06.typ
115 kB
#let rtext(body)={
text(fill:maroon,body)
}
= 布局库(Layout)
以不同的方式在页面上排列元素。
通过组合布局函数,您可以创建复杂且自动化的布局。
== 对齐(align)<align>
水平和垂直对齐内容。
=== 示例(Example)
我们先从将内容水平居中开始:
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#set page(height: 120pt)
#set align(center)
Centered text, a sight to see \
In perfect balance, visually \
Not left nor right, it stands alone \
A work of art, a visual throne
```
],[
#set align(horizon)
#image("images/6001.png",width: auto)
]
)
要将对象垂直居中,请使用水平对齐:
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#set page(height: 120pt)
#set align(horizon)
Vertically centered, \
the stage had entered, \
a new paragraph.
```
],[
#set align(horizon)
#image("images/6002.png",width: auto)
]
)
=== 合并比对结果(Combining alignments)<align_Combining-alignments>
你可以使用 + 运算符组合两种对齐方式。我们改用函数形式而非set规则,让该设置仅作用于一段内容:
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#set page(height: 120pt)
Though left in the beginning ...
#align(right + bottom)[
... they were right in the end, \
and with addition had gotten, \
the paragraph to the bottom!
]
```
],[
#set align(horizon)
#image("images/6003.png",width: auto)
]
)
=== 嵌套对齐(Nested alignment)<align_Nested-alignment>
您可以为布局容器及其内部元素使用不同的对齐方式。通过这种方式,您可以创建复杂的布局:
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#align(center, block[
#set align(left)
Though centered together \
alone \
we \
are \
left.
])
```
],[
#set align(horizon)
#image("images/6004.png",width: auto)
]
)
=== 同一行内的对齐(Alignment within the same line)
align函数执行块级对齐,因此总会中断当前段落。若要让同一行的不同部分采用不同的对齐方式,则应改用分数间距:
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
Start #h(1fr) End
```
],[
#set align(horizon)
#image("images/6005.png",width: auto)
]
)
=== 参数(Parameters)
```typc
align(
alignment: alignment,
body: content,
) → content
```
- *alignment*: alignment
双轴方向上的#link(<alignment>)[对齐方式]。默认值:```typc start+top```。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#set page(height: 6cm)
#set text(lang: "ar")
مثال
#align(
end + horizon,
rect(inset: 12pt)[ركن]
)
```
],[
#set align(horizon)
#image("images/6006.png",width: auto)
]
)
- *body*: content
待对齐的内容。
== 对齐方式(alignment)<alignment>
沿坐标轴对齐对象的位置。
可选值如下:
-- start:按#link(<text_dir>)[文本方向]的#link(<direction_start>)[起始端]对齐。
-- end:按#link(<text_dir>)[文本方向]的#link(<direction_end>)[末尾端]对齐。
-- left:左对齐。
-- center:水平居中对齐。
-- right:右对齐。
-- top:顶部对齐。
-- horizon:垂直居中对齐。
-- bottom:底部对齐。
这些值在全局范围内以及对齐类型的作用域内均可用,因此你可以编写下面两种形式中的任意一种:
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#align(center)[Hi]
#align(alignment.center)[Hi]
)
```
],[
#set align(horizon)
#image("images/6007.png",width: auto)
]
)
=== 二维对齐方式(2D alignments)<alignment_2D_alignments>
若要同时沿两个轴对齐,可使用+运算符叠加两种对齐方式。例如,top + right会将内容对齐至右上角。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#set page(height: 3cm)
#align(center + bottom)[Hi]
)
```
],[
#set align(horizon)
#image("images/6008.png",width: auto)
]
)
=== 域(Fields)<alignment_Fields>
x 和 y 字段分别保存对齐方式的水平分量与垂直分量(作为另一种对齐设置),二者可以为#text(fill: red,"none")。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#(top + right).x \
#left.x \
#left.y (none)
)
```
],[
#set align(horizon)
#image("images/6009.png",width: auto)
]
)
=== 定义(Definitions)
- *axis*
该对齐方式所属的轴。
-- "horizontal" 对应 start、left、center、right 和 end
-- "vertical" 对应 top、horizon 和 bottom
-- none 用于二维对齐
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#left.axis() \
#bottom.axis()
)
```
],[
#set align(horizon)
#image("images/6010.png",width: auto)
]
)
```typc
self.axis() → none str
```
- *inv*
反向对齐。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#top.inv() \
#left.inv() \
#center.inv() \
#(left + bottom).inv()
)
```
],[
#set align(horizon)
#image("images/6011.png",width: auto)
]
)
== 角度(angle)<angle>
描述旋转的角度。
Typst支持以下角度单位:
-- 度:180度
-- 弧度:3.14弧度
=== 示例(Example)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#rotate(10deg)[Hello there!]
```
],[
#set align(horizon)
#image("images/6012.png",width: auto)
]
)
=== 定义(Definitions)
- *rad*
将此角度转换为弧度。
```typc
self.rad() → float
```
- *deg*
将该角度转换为度。
```typc
self.deg() → float
```
== 块(block)<block>
块级容器。
这类容器可用于分隔内容、设置尺寸,并为其添加背景或边框。
块元素也是控制文本是否属于段落的主要方式。更多详情可查阅#link(<Paragraph_What-becomes-a-paragraph>)[段落相关文档]。
=== 示例(Example)
使用块元素,你可以为内容设置背景,同时仍允许内容跨多页分页显示。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#set page(height: 100pt)
#block(
fill: luma(230),
inset: 8pt,
radius: 4pt,
lorem(30),
)
```
],[
#set align(horizon)
#image("images/6013.png",width: auto)\
#image("images/6014.png",width: auto)
]
)
块还可用于将原本为行内的元素强制转为块级元素,在编写show规则时尤其有用。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#show heading: it => it.body
= Blockless
More text.
#show heading: it => block(it.body)
= Blocky
More text.
```
],[
#set align(horizon)
#image("images/6015.png",width: auto)
]
)
=== 参数(Parameters)
```typc
block(
width: auto relative,
height: auto relative fraction,
breakable: bool,
fill: none color gradient tiling,
stroke: none length color gradient stroke tiling dictionary,
radius: relative dictionary,
inset: relative dictionary,
outset: relative dictionary,
spacing: auto relative fraction,
above: auto relative fraction,
below: auto relative fraction,
clip: bool,
sticky: bool,
body: none content,
) → content
```
- *width*: auto relative<block_width>
该区块的宽度。默认值:```typc auto```。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#set align(center)
#block(
width: 60%,
inset: 8pt,
fill: silver,
lorem(10),
)
```
],[
#set align(horizon)
#image("images/6016.png",width: auto)
]
)
- *height*: auto relative fraction<block_height>
区块的高度。当该高度大于页面剩余空间,且#link(<block_breakable>)[可分页属性]为#text(fill: red,"true" )时,区块会将剩余高度部分延续至下一页。默认值:```typc auto```。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#set page(height: 80pt)
#set align(center)
#block(
width: 80%,
height: 150%,
fill: aqua,
)
```
],[
#set align(horizon)
#image("images/6017.png",width: auto)\
#image("images/6018.png",width: auto)\
]
)
- *breakable*: bool<block_breakable>
该块是否可以断开并在下一页继续显示。默认值:```typc true```。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#set page(height: 80pt)
The following block will
jump to its own page.
#block(
breakable: false,
lorem(15),
)
```
],[
#set align(horizon)
#image("images/6019.png",width: auto)\
#image("images/6020.png",width: auto)\
]
)
- *fill*: none color gradient tiling<block_fill>
该区块的背景颜色。更多详情请参阅#link(<rect_fill>)[矩形相关文档]。默认值:```typc none```。
- *stroke*: none length color gradient stroke tiling dictionary<block_stroke>
该区块的边框颜色。更多详情请参阅#link(<rect_stroke>)[矩形文档]。默认值:```typc (:)```。
- *radius*: relative dictionary<block_radius>
区块边角的圆角大小。更多详情请参见#link(<rect_radius>)[矩形相关文档]。默认值:```typc (:)```。
- *inset*: relative dictionary<block_inset>
块内容的内边距大小。更多详情请参阅#link(<box_inset>)[盒子相关文档]。默认值:```typc (:)```。
- *outset*: relative dictionary<block_outset>
在不影响布局的前提下,该区块需要扩容至多大。更多详情请查阅#link(<box_outset>)[该组件的文档说明]。默认值:```typc (:)```。
- *spacing*: auto relative fraction<block_spacing>
块周围的间距。设为auto时,会继承段落间距。默认值:```typc 1.2em```。
对于两个相邻块,取第一个块的下方间距与第二个块的上方间距中的较大值。此外,块间距优先级高于段落间距。
请注意,这只是将上方和下方间距设置为相同值的简写方式。由于上方和下方间距的值可能不同,上下文块仅支持访问block.above和block.below,无法直接访问block.spacing。
该属性可结合显示规则使用,用来调整任意块级元素周边的间距。
#table(
columns: 1fr,
align: left,
stroke: none,
fill: luma(86.27%),
[
```typ
#set align(center)
#show math.equation: set block(above: 8pt, below: 16pt)
This sum of $x$ and $y$:
$ x + y = z $
A second paragraph.
```
],[
#set align(horizon)
#image("images/6021.png",width: auto)
]
)
- *above*: auto relative fraction<block_above>
此区块与其前一个区块之间的间距。默认值:```typc auto```。
- *below*: auto relative fraction<block_below>
此块与其后续块之间的间距。默认值:```typc auto```。
- *clip*: bool<block_clip>
是否裁剪块内的内容。默认值:```typc false```。
当块的内容大于块本身时,裁剪功能很有用,所有超出块边界的内容都会被隐藏。
#table(
columns: 1fr,
align: left,
stroke: none,
fill: luma(86.27%),
[
```typ
#block(
width: 50pt,
height: 50pt,
clip: true,
image("tiger.jpg", width: 100pt, height: 100pt)
)
```
],[
#set align(horizon)
#image("images/6022.png",width: auto)
]
)
- *sticky*: bool<block_sticky>
此段落是否必须紧挨下一段,中间不能有空行。默认值:```typc false```。
默认情况下,这是针对标题块设置的,以防止页面底部出现孤立的标题。
#table(
columns: 1fr,
align: left,
stroke: none,
fill: luma(86.27%),
[
```typ
// Disable stickiness of headings.
#show heading: set block(sticky: false)
#lorem(20)
= Chapter
#lorem(10)
```
],[
#set align(horizon)
#image("images/6023.png",width: auto)\
#image("images/6024.png",width: auto)
]
)
- *body*: none content<block_body>
区块中的内容
== 盒子(box)<box>
一种用于设定内容尺寸的行内容器。
除行内公式、文本和盒子外,所有元素均为块级元素,不能放置在#link(<Paragraph>)[段落]内部。box函数可用于将这类元素嵌入段落。盒子默认根据自身内容确定尺寸,也可显式指定大小。
=== 示例(Example)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
Refer to the docs
#box(
height: 9pt,
image("docs.svg")
)
for more information.
```
],[
#set align(horizon)
#image("images/6025.png",width: auto)]
)
=== 参数(Parameters)
```typc
box(
width: auto relative fraction,
height: auto relative,
baseline: auto relative dictionary alignment,
fill: none color gradient tiling,
stroke: none length color gradient stroke tiling dictionary,
radius: relative dictionary,
inset: relative dictionary,
outset: relative dictionary,
clip: bool,
body: none content,
) → content
```
- *width*: auto relative fraction<box_width>
盒子的宽度。默认值:```typc auto```。
如下例所示,盒子可以设置#link(<fraction>)[小数]宽度。
注意:目前,段落内仅盒子支持小数尺寸,且仅宽度可设为小数。后续可能会增加对图片、形状等元素的小数尺寸支持。
#table(
columns: 1fr,
align: left,
stroke: none,
[
```typst
Line in #box(width: 1fr, line(length: 100%)) between.
```
],
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6026.png",width: 80%)
]
)
]
)
- *height*: auto relative<box_height>
箱子的高度。默认值:```typc auto```。
- *baseline*: auto relative dictionary alignment<box_baseline>
盒子基线的垂直位置。该属性用于将盒子与其段落内周围的文本对齐,默认情况下基线位于文本正下方。默认值:```typc (at:auto,shift:0%+0pt)```。
默认状态下,盒子的基线与其内容(例如盒内的文本或公式)的基线保持一致,这就是#text(fill: red,"auto")选项。不过基线可通过两种方式调整。第一种是直接选择一种垂直#link(<alignment>)[对齐方式],如顶端、中线或底端,将基线定位在相对于盒子总高度(包含内边距)的对应位置。
另一种调整方式是按指定的相对长度,将默认基线沿垂直方向偏移一定距离。例如,取值#text(fill: red,"2pt")会将基线向上移动该长度(对齐点上移,使得内容相对下移);而#text(fill: red,"-40%")代表基线向下偏移盒子总高度(含内边距)的40%(内容因此相对上移)。
这两种选项可以同时设置,使用包含at和shift键的字典分别指定。例如设置(at: bottom, shift: 10pt)时,盒子基线会定位在距离内容底端向上恰好#text(fill: red,"10pt")的位置。
#table(
columns: 1fr,
align: left,
stroke: none,
[
```typst
Image: #box(baseline: 40%, image("tiger.jpg", width: 2cm)).
```
],
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6027.png",width: 80%)
]
)
]
)
- *fill*: none color gradient tiling<box_fill>
该盒子的背景颜色。更多信息请参阅#link(<rect_fill>)[矩形组件文档]。默认值:```typc none```。
- *stroke*: none length color gradient stroke tiling dictionary<box_stroke>
该盒子的边框颜色。更多详情请参阅#link(<rect_stroke>)[矩形组件的文档]。默认值:```typc (:)```。
- *radius*: relative dictionary<box_radius>
盒子圆角的大小。更多详情请参阅#link(<rect_radius>)[矩形文档]。默认值:```typc (:)```。
- *inset*: relative dictionary<box_inset>
框内容的内边距大小。默认值:```typc (:)```。
该参数可以是适用于所有边的单一长度值,也可以是分别设置各边长度的字典。传入字典时,可包含以下键(优先级从高到低):top、right、bottom、left(分别控制单元格对应边),x、y(控制水平、垂直内嵌边距),rest(覆盖字典中其他项未设置的所有内嵌边距)。所有键均为可选;省略的键会沿用之前设置的值,若从未设置则使用默认值。
该参数的#link(<relative>)[相对长度]基于不含#link(<box_outset>)[外扩边距]的盒子尺寸计算。请注意:相对内嵌边距与外扩边距不同于相对#link(<box_width>)[宽]#link(<box_height>)[高],后者是相对于容器计算的。
注意:当盒子内包含文本时,其实际尺寸由当前#link(<text_top-edge>)[文本边界]决定。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#rect(inset: 0pt)[Tight]
```
],[
#set align(horizon)
#image("images/6028.png",width: auto)]
)
- *outset*: relative dictionary<box_outset>
在不影响布局的情况下,盒子尺寸需要扩大多少。默认值:```typc (:)```。
该值可以是适用于所有边的单一长度,也可以是存放各边长度的字典。此参数的[相对长度]是相对于不包含外延距离的盒子尺寸。更多详情可参考上文#link(<box_inset>)[内边距]的文档。
该功能可用于避免内边距影响文本行布局。下方示例的通用版本,请参阅#link(<raw_block>)[原始文本的块参数]文档。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
An inline
#box(
fill: luma(235),
inset: (x: 3pt, y: 0pt),
outset: (y: 3pt),
radius: 2pt,
)[rectangle].
```
],[
#set align(horizon)
#image("images/6029.png",width: auto)]
)
- *clip*: bool
是否裁剪容器内的内容。默认值:```typc false```。
当容器中的内容超出容器本身大小时,裁剪功能十分有用,所有超出容器边界的内容都会被隐藏。
#table(
columns: 1fr,
align: left,
stroke: none,
[
```typst
#box(
width: 50pt,
height: 50pt,
clip: true,
image("images/tiger.png"", width: 100pt, height: 100pt)
)
```
],
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6030.png",width: 80%)
]
)
]
)
- *body*: none content
盒子里的内容。
== 列分隔符(colbreak)<colbreak>
强制分栏。
在单栏布局或页面的最后一栏中使用该功能时,其效果等同于分页符。否则,分栏符之后的内容将放置在下一栏。
=== 示例(Example)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set page(columns: 2)
Preliminary findings from our
ongoing research project have
revealed a hitherto unknown
phenomenon of extraordinary
significance.
#colbreak()
Through rigorous experimentation
and analysis, we have discovered
a hitherto uncharacterized process
that defies our current
understanding of the fundamental
laws of nature.
```
],[
#set align(horizon)
#image("images/6031.png",width: auto)]
)
=== 参数(Parameters)
```typc
colbreak(weak: bool) → content
```
- *weak*: bool
如果为真,当当前列已经为空时,将跳过分栏符。默认值:```typc false```。
== 列数(columns)<columns>
将一个区域分隔为多个等宽列。
column函数可将任意容器内部拆分为多列。目前该函数不会自动均衡各列高度,各列高度将取容器高度或页面剩余高度。均衡列高的功能已列入后续开发计划。
在跨多列排布内容时,可使用#link(<colbreak>)[colbreak]强制将后续内容换到下一列。
=== 示例(Example)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#columns(2, gutter: 8pt)[
This text is in the
first column.
#colbreak()
This text is in the
second column.
]
```
],[
#set align(horizon)
#image("images/6032.png",width: auto)]
)
=== 页面级列(Page-level columns)<columns_Page-level-columns>
如果需要在整个文档中插入列,请使用页面功能的#link(<page_columns>)[列参数]。这将直接在页面级别创建列,而不是将所有内容包裹在布局容器中。因此,#link(<pagebreak>)[分页符]、#link(<footnote>)[脚注]和#link(<Paragraph_line>)[行号]等功能将继续按预期正常工作。有关更多信息,请同时阅读#link(<page_guide>)[页面设置指南的相关部分]。
=== 突破列布局(Breaking out of columns)<columns_Breaking-out-of-columns>
如需临时脱离分栏布局(例如论文标题),可使用父级作用域浮动定位:
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set page(columns: 2, height: 150pt)
#place(
top + center,
scope: "parent",
float: true,
text(1.4em, weight: "bold")[
My document
],
)
#lorem(40)
```
],[
#set align(horizon)
#image("images/6033.png",width: auto)]
)
=== 参数(Parameters)
```typst
columns(
count: int,
gutter: relative,
body: content,
) → content
```
- *count*: int
列数。默认值:```typc 2```。
- *gutter*: relative
各列之间的沟槽间距大小。默认值:```typc 4%+0pt```。
- *body*: content
需要分栏排版的内容。
== 方向(direction)<direction>
内容可排布的四个方向。
可选值如下:
-- ltr:从左到右。
-- rtl:从右到左。
-- ttb:从上到下。
-- btt:从下到上。
这些值在全局范围内以及方向类型的作用域内均可用,因此你可以编写下面两种形式中的任意一种:
#table(
columns: (1.5fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#stack(dir: rtl)[A][B][C]
#stack(dir: direction.rtl)[A][B][C]
```
],[
#set align(horizon)
#image("images/6034.png",width: auto)]
)
=== 定义(Definitions)
- *from*
从起点返回一个方向。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#direction.from(left) \
#direction.from(right) \
#direction.from(top) \
#direction.from(bottom)
```
],[
#set align(horizon)
#image("images/6035.png",width: auto)]
)
```typst
direction.from(side: alignment) → direction
```
- *to*
从终点返回一个方向。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#direction.to(left) \
#direction.to(right) \
#direction.to(top) \
#direction.to(bottom)
```
],[
#set align(horizon)
#image("images/6036.png",width: auto)]
)
```typst
direction.to(side: alignment) → direction
```
- *axis*
该方向所属的轴,取值为“horizontal”或“vertical”。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#ltr.axis() \
#ttb.axis()
```
],[
#set align(horizon)
#image("images/6037.png",width: auto)]
)
```typst
direction.axis() → str
```
- *sign*
用于计算的对应符号。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#ltr.sign() \
#rtl.sign() \
#ttb.sign() \
#btt.sign()
```
],[
#set align(horizon)
#image("images/6038.png",width: auto)]
)
```typst
direction.sign() → int
```
- *start*<direction_start>
作为对齐基准,此方向的起始点。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#ltr.start() \
#rtl.start() \
#ttb.start() \
#btt.start()
```
],[
#set align(horizon)
#image("images/6039.png",width: auto)]
)
```typst
self.start() → alignment
```
- *end*<direction_end>
该方向的终点,作为对齐基准。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#ltr.end() \
#rtl.end() \
#ttb.end() \
#btt.end()
```
],[
#set align(horizon)
#image("images/6040.png",width: auto)]
)
```typst
self.end() → alignment
```
- *inv*<direction_inv>
相反方向。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#ltr.inv() \
#rtl.inv() \
#ttb.inv() \
#btt.inv()
```
],[
#set align(horizon)
#image("images/6041.png",width: auto)]
)
```typst
self.inv() → direction
```
== 小数(fraction)<fraction>
定义布局中剩余空间的分配方式。
每个按比例分配尺寸的元素,将根据自身比例值与所有比例值总和的比值来获取空间。
更多详情可参见 h、v 函数以及 grid 函数。
=== 示例(Example)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
Left #h(1fr) Left-ish #h(2fr) Right
```
],[
#set align(horizon)
#image("images/6042.png",width: auto)]
)
== 网格(grid)<grid>
以网格形式排布内容。
网格元素可让你将内容按网格排布。你可以定义行数与列数,同时设置行列之间的间距大小。行列支持多种尺寸模式,可用来创建复杂布局。
网格元素与表格元素虽然功能很相似,但二者适用场景不同,语义含义也不一样。网格元素用于展示和页面布局;而#link(<table>)[表格]元素,广义上来说,用于展示多条相关数据。在其中一种元素上设置并启用的规则不会影响另一种元素。请查阅#link(<grid_Accessibility>)[无障碍章节],了解屏幕阅读器等辅助技术(AT)会如何向用户呈现网格与表格。
=== 确定轨道尺寸(Sizing the tracks)<grid_Sizing-the-tracks>
网格的尺寸由参数中指定的轨道大小决定。包含多项尺寸参数:#link(<grid_columns>)[列]、#link(<grid_rows>)[行]与#link(<grid_gutter>)[间距]。由于各项尺寸参数可接收的值相同,我们在此统一说明。每个尺寸参数接收一组独立的轨道尺寸数组。轨道尺寸可为以下类型之一:
#set par(hanging-indent: 1.3cm)
-- #rtext()[auto]:轨道尺寸将自适应容纳其内容,最大不超过剩余空间。若存在多个#rtext()[auto]轨道宽度,且它们所需空间总和超出可用空间,则这些#rtext()[auto]轨道会平分可用空间。
-- 固定长度或相对长度(例如 #rtext()[10pt] 或 #rtext()[20% - 1cm]):轨道将严格采用该尺寸。
-- 分数长度(例如 #rtext()[1fr]):在所有其他轨道完成尺寸计算后,剩余空间将按照分数比例分配给分数轨道。例如,若有两条分数轨道,每条的值均为#rtext()[1fr],则二者将各占用一半剩余空间。
#set par(hanging-indent: 0cm)
如需指定单条轨道,可省略数组,直接使用单个数值。如需指定多条自动轨道,请输入轨道数量而非数组。例如,```typc columns: 3``` 等价于```typc columns: (auto,auto,auto)```。
=== 示例(Examples)
下面的示例展示了不同的轨道尺寸设置选项。同时演示了如何使用 grid.cell 让单个单元格跨越两条网格轨道。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
// 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],
grid.cell(
colspan: 2,
image("tiger.jpg", width: 100%),
),
)
```
],[
#set align(horizon)
#image("images/6043.png",width: auto)]
)
你也可以将字符串或内容数组#link(<arguments_Spreading>)[展开]到网格中,填充网格单元格。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#grid(
columns: 5,
gutter: 5pt,
..range(25).map(str)
)
```
],[
#set align(horizon)
#image("images/6044.png",width: auto)]
)
=== 设置网格样式(Styling the grid)<grid_Styling-the-grid>
网格元素与表格元素的工作原理类似。如需实操讲解,请参阅#link(<table>)[表格指南];若只想快速了解概览,请继续阅读。
可通过不同参数自定义网格的外观,以下是其中最重要的几项:
#set par(hanging-indent: 1.3cm)
-- #link(<grid_align>)[align] 用于修改单元格的对齐方式
-- #link(<grid_inset>)[inset] 可选择为单元格添加内边距
-- #link(<grid_fill>)[fill] 用于设置单元格的背景色
-- #link(<grid_stroke>)[stroke] 可选择启用带指定描边样式的网格线
#set par(hanging-indent: 0cm)
为满足不同需求,可以通过多种方式对它们进行设置。
如果需要为单个单元格覆盖上述选项,可以使用 #link(<grid_cell>)[grid.cell] 元素。同样,你可以通过 #link(<grid_hline>)[grid.hline] 和 #link(<grid_vline>)[grid.vline] 元素单独覆盖网格线。
若要为网格配置整体样式,你可以改用以下任意一种方式设置选项:
#set par(hanging-indent: 1.3cm)
-- 单个值,作用于全部单元格。
-- 与每列对应的数值数组。当列数多于数组元素数量时,数组会循环复用。
-- 形如 `(x, y) => value` 的函数。该函数接收单元格的列索引与行索引(索引均从0开始),并返回应用到该单元格的对应值。
#set par(hanging-indent: 0cm)
#table(
columns: 1fr,
align: left,
stroke: none,
[
```typst
#grid(
columns: 5,
// By a single value
align: center,
// By a single but more complicated value
inset: (x: 2pt, y: 3pt),
// By an array of values (cycling)
fill: (rgb("#239dad50"), none),
// By a function that returns a value
stroke: (x, y) => if calc.rem(x + y, 3) == 0 { 0.5pt },
..range(5 * 3).map(n => numbering("A", n + 1))
)
```
],
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6045.png",width: 80%)
]
)
]
)
除此之外,你还可以为网格(grid)以及网格单元格(grid.cell)设置样式规则。特别地,网格单元格(grid.cell)的x、y字段可用于where选择器,从而能够对特定列、特定行或是单独位置的单元格设置样式。
*描边样式优先级(Stroke styling precedence)*
如上所述,设置网格单元格描边有三种方式:通过 grid.cell 的 stroke 字段、使用 grid.hline 和 grid.vline,或是设置网格的 stroke 字段。当存在多项冲突的设置时,hline 和 vline 设置优先级最高,其次是单元格设置,最后是网格设置。
此外,重复显示的网格表头或表尾的描边,优先级高于普通单元格描边。
=== 无障碍性(Accessibility)<grid_Accessibility>
网格不具备任何特殊语义。辅助技术(AT)无法在网格内按单元格进行二维导航。如果你需要展示表格数据,请改用table元素。
辅助技术会按照语义顺序读取网格单元格。通常,该顺序就是你传入网格的顺序。但是,如果你通过grid.cell的x、y参数手动设置单元格位置,单元格将按行读取,在从左向右排版的文档中为从左到右。单元格会在首次遍历到其位置时被读取。
=== 参数(Parameters)
```typc
grid(
columns: auto int relative fraction array,
rows: auto int relative fraction array,
gutter: auto int relative fraction array,
column-gutter: auto int relative fraction array,
row-gutter: auto int relative fraction array,
inset: relative array dictionary function,
align: auto array function alignment,
fill: none color gradient tiling array function,
stroke: none length color gradient stroke tiling array dictionary function,
children: content,
) → content
```
- *columns*: auto int relative fraction array<grid_columns>
列的尺寸。默认值:```typc ()```。
可以指定一个轨道尺寸数组,或者传入一个整数,以此创建拥有对应数量#text(fill: red,"auto")尺寸列的网格。请注意,与行和间距不同,仅提供单个轨道尺寸将只会生成一列。
更多详情请参阅上文的#link(<grid_Sizing-the-tracks>)[轨道尺寸]部分。
- *rows*: auto int relative fraction array<grid_rows>
行尺寸。默认值:```typc ()```。
如果单元格数量超出定义行所能容纳的数量,则重复最后一行,直至所有单元格都排布完毕。
更多详情请参阅上文的#link(<grid_Sizing-the-tracks>)[轨道尺寸]部分。
- *gutter*: auto int relative fraction array<grid_gutter>
行与列之间的间距。这是将列间距和行间距设为相同值的简写方式。默认值:```typc ()```。
如果间距数量大于已定义的尺寸,将重复使用最后一个间距。
更多详情请参见上方的轨道尺寸章节。
- *column-gutter*: auto int relative fraction array<grid_column-gutter>
列之间的间距。默认值:```typc ()```。
- *row-gutter*: auto int relative fraction array<grid_row-gutter>
行与行之间的间距。默认值:```typc ()```。
- *inset*: relative array dictionary function<grid_inset>
单元格内容的内边距大小。默认值:```typc (:)```。
如需为所有单元格设置统一内边距,可以使用单个长度值作用于全部四边,或是使用包含各边长度的字典。更多详情请参阅#link(<box_inset>)[容器]文档。
如需为不同单元格设置不同内边距,你可以采用以下方式:
#set par(hanging-indent: 1cm)
-- 为全部单元格使用同一个内边距\
-- 使用与每一列对应的内边距数组\
-- 使用函数,将单元格位置映射为对应的内边距
#set par(hanging-indent: 0cm)
更多详情请参考上方#link(<grid_Styling-the-grid>)[样式]部分。
此外,你可以在 #link(<table_inset>)[table.inset] 参数处查看示例。
- *align*: auto array function alignment<grid_align>
如何对齐单元格内容。默认值:```typc auto```。
若设置为auto,则采用外部对齐方式。
你可以通过以下任意一种方式指定对齐方式:
#set par(hanging-indent: 1cm)
-- 为所有单元格设置统一的对齐方式\
-- 使用与每一列对应的对齐方式数组\
-- 使用一个函数,将单元格位置映射为其对齐方式
详细信息请参阅上方的#link(<grid_Styling-the-grid>)[样式]部分。
此外,你可以在 #link(<table_align>)[table.align] 参数处查看示例。
- *fill*: none color gradient tiling array function<grid_fill>
如何填充单元格。默认值:```typc none```。
#set par(hanging-indent: 1.3cm)
可以采用以下方式:
-- 所有单元格使用单一颜色
-- 与每一列对应的颜色数组
-- 将单元格位置映射为对应颜色的函数
#set par(hanging-indent: 0cm)
值得重点说明的是,数组和函数可用于创建条纹网格。更多详情请参阅上方的#link(<grid_Styling-the-grid>)[样式]设置部分。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#grid(
fill: (x, y) =>
if calc.even(x + y) { luma(230) }
else { white },
align: center + horizon,
columns: 4,
inset: 2pt,
[X], [O], [X], [O],
[O], [X], [O], [X],
[X], [O], [X], [O],
[O], [X], [O], [X],
)
```
],[
#set align(horizon)
#image("images/6046.png",width: auto)]
)
- *stroke*: none length color gradient stroke tiling array dictionary function<grid_stroke>
如何设置单元格描边。默认值:```typc (:)```。
网格默认没有描边,可通过将此选项设置为目标描边样式来开启。
如果需要绘制能够跨过分隔槽(#link(<grid_gutter>)[gutter]选项生成的单元格间距)的线条,或是覆盖多个特定单元格之间的描边,可在定义网格单元格的同时,设置#link(<grid_hline>)[grid.hline]和#link(<grid_vline>)[grid.vline]中的一项或多项。
如需为所有单元格设置相同描边,可对全部四边使用同一种描边,也可以使用描边字典单独定义每一条边。更多信息请参阅矩形相关文档。
#set par(hanging-indent: 1.5cm)
如需为不同单元格设置各不相同的描边,你可以:\
-- 对所有单元格使用同一种描边\
-- 使用与每一列对应的描边数组\
-- 使用函数,将单元格位置映射为对应的描边
#set par(hanging-indent: 0cm)
更多详情请参考上方的#link(<grid_Styling-the-grid>)[样式]设置章节。
#table(
columns: 1fr,
align: left,
stroke: none,
[
```typst
#set page(width: 420pt)
#set text(number-type: "old-style")
#show grid.cell.where(y: 0): set text(size: 1.3em)
#grid(
columns: (1fr, 2fr, 2fr),
row-gutter: 1.5em,
inset: (left: 0.5em),
stroke: (x, y) => if x > 0 { (left: 0.5pt + gray) },
align: horizon,
[Winter \ 2007 \ Season],
[Aaron Copland \ *The Tender Land* \ January 2007],
[Eric Satie \ *Gymnopedie 1, 2* \ February 2007],
[],
[Jan 12 \ *Middlebury College \ Center for the Arts* \ 20:00],
[Feb 2 \ *Johnson State College Dibden Center for the Arts* \ 19:30],
[],
[Skip a week \ #text(0.8em)[_Prepare your exams!_]],
[Feb 9 \ *Castleton State College \ Fine Arts Center* \ 19:30],
[],
[Jan 26, 27 \ *Lyndon State College Alexander Twilight Theater* \ 20:00],
[
Feb 17 --- #smallcaps[Anniversary] \
*Middlebury College \ Center for the Arts* \
19:00 #text(0.7em)[(for a special guest)]
],
)
```
],
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6047.png",width: 100%)
]
)
]
)
#table(
columns: 1fr,
align: left,
stroke: none,
[
```typst
#set page(height: 13em, width: 26em)
#let cv(..jobs) = grid(
columns: 2,
inset: 5pt,
stroke: (x, y) => if x == 0 and y > 0 {
(right: (
paint: luma(180),
thickness: 1.5pt,
dash: "dotted",
))
},
grid.header(grid.cell(colspan: 2)[
*Professional Experience*
#box(width: 1fr, line(length: 100%, stroke: luma(180)))
]),
..{
let last = none
for job in jobs.pos() {
(
if job.year != last [*#job.year*],
[
*#job.company* - #job.role _(#job.timeframe)_ \
#job.details
]
)
last = job.year
}
}
)
#cv(
(
year: 2012,
company: [Pear Seed & Co.],
role: [Lead Engineer],
timeframe: [Jul - Dec],
details: [
- Raised engineers from 3x to 10x
- Did a great job
],
),
(
year: 2012,
company: [Mega Corp.],
role: [VP of Sales],
timeframe: [Mar - Jun],
details: [- Closed tons of customers],
),
(
year: 2013,
company: [Tiny Co.],
role: [CEO],
timeframe: [Jan - Dec],
details: [- Delivered 4x more shareholder value],
),
(
year: 2014,
company: [Glorbocorp Ltd],
role: [CTO],
timeframe: [Jan - Mar],
details: [- Drove containerization forward],
),
)
```
],
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6048.png",width: 100%)\
#image("images/6049.png",width: 100%)
]
)
]
)
- *children*: content
网格单元格中的内容,以及通过 #link(<grid_hline>)[grid.hline] 和 #link(<grid_vline>)[grid.vline] 元素指定的所有额外网格线。
单元格以行优先顺序填充。
=== 定义(Definitions)
- *cell*<grid_cell>
网格中的单元格。你可以在网格的参数列表里使用该函数,以覆盖单个单元格的网格样式属性,或将其手动放置在网格内。你也可以在显示规则中使用该函数,一次性为多个单元格应用指定样式。
例如,您可以为单个单元格覆盖位置和笔画:
#table(
columns: 1fr,
align: left,
stroke: none,
[
```typst
#set text(15pt, font: "Noto Sans Symbols 2")
#show regex("[♚-♟︎]"): set text(fill: rgb("21212A"))
#show regex("[♔-♙]"): set text(fill: rgb("111015"))
#grid(
fill: (x, y) => rgb(
if calc.odd(x + y) { "7F8396" }
else { "EFF0F3" }
),
columns: (1em,) * 8,
rows: 1em,
align: center + horizon,
[♖], [♘], [♗], [♕], [♔], [♗], [♘], [♖],
[♙], [♙], [♙], [♙], [], [♙], [♙], [♙],
grid.cell(
x: 4, y: 3,
stroke: blue.transparentize(60%)
)[♙],
..(grid.cell(y: 6)[♟],) * 8,
..([♜], [♞], [♝], [♛], [♚], [♝], [♞], [♜])
.map(grid.cell.with(y: 7)),
)
```
],
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6050.png",width: 80%)
]
)
]
)
你也可以对 grid.cell 设置显示规则,一次性为所有单元格设置样式。例如,你可以根据单元格的位置来应用样式。更多相关内容请参考 #link(<table_cell>)[table.cell] 元素的示例。
```typc
grid.cell(
body: content,
x: auto int,
y: auto int,
colspan: int,
rowspan: int,
inset: auto relative dictionary,
align: auto alignment,
fill: none auto color gradient tiling,
stroke: none length color gradient stroke tiling dictionary,
breakable: auto bool,
) → content
```
-- *body*: content
单元格内容。
-- *x*: auto int<grid_cell_x>
单元格所在列(从零开始索引)。此字段可用于显示规则中,根据单元格所在列设置单元格样式。默认值:```typc auto```。
你可以重写该字段,指定单元格需要放置在哪一列。如果未选择行(y),单元格将被放置在该列可用的第一行(从第0行开始);若无可用行,则新建一行。但如果同时指定了x和y,单元格将被放置在该精确位置。若该位置已被占用,会抛出错误(因此,通常建议先定义自定义位置的单元格,再定义自动位置的单元格)。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#let circ(c) = circle(
fill: c,
width: 5mm,
)
#grid(
columns: 4,
rows: 7mm,
stroke: .5pt + blue,
align: center + horizon,
inset: 1mm,
grid.cell(x: 2, y: 2, circ(aqua)),
circ(yellow),
grid.cell(x: 3, circ(green)),
circ(black),
)
```
],[
#set align(horizon)
#image("images/6051.png",width: auto)]
)
-- *y*: auto int<grid_cell_y>
单元格所在行(从零开始索引)。此字段可用于显示规则,根据单元格所在行设置单元格样式。默认值:```typc auto```。
你可以重写此字段,指定单元格必须放置在哪一行。如果未选择列(x),单元格将放置在所选定行中可用的第一列(从0号列开始)。若选定行中的所有列均已被占用,则会抛出错误。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#let tri(c) = polygon.regular(
fill: c,
size: 5mm,
vertices: 3,
)
#grid(
columns: 2,
stroke: blue,
inset: 1mm,
tri(black),
grid.cell(y: 1, tri(teal)),
grid.cell(y: 1, tri(red)),
grid.cell(y: 2, tri(orange))
)
```
],[
#set align(horizon)
#image("images/6052.png",width: auto)]
)
-- *colspan*: int<grid_cell_colspan>
此单元格跨越的列数。默认值:```typc 1```。
-- *rowspan*: int<grid_cell_rowspan>
该单元格所跨的行数。默认值:```typc 1```。
-- *inset*: auto relative dictionary<grid_cell_inset>
单元格的#link(<grid_inset>)[内嵌]覆盖。默认值:```typc auto```。
-- *align*: auto alignment<grid_cell_align>
单元格的对齐覆盖设置。默认值:```typc auto```。
-- *fill*: none auto color gradient tiling<grid_cell_fill>
单元格的填充覆盖。默认值:```typc auto```。
-- *stroke*: none length color gradient stroke tiling dictionary<grid_cell_stroked>
单元格描边覆盖。默认值:```typc (:)```。
-- *breakable*: auto bool<grid_cell_breakable>
该单元格所跨的行是否可以分布在不同页面。取值为auto时,仅跨固定尺寸行的单元格不可分页,而至少跨一行自动尺寸行的单元格支持分页。默认值:```typc auto```。
- *hline*<grid_hline>
网格中的一条水平线。
它会覆盖所有单元格描边,包括通过网格的stroke字段指定的描边。可以跨越由网格的column-gutter选项生成的单元格间距。
该函数的示例可参见#link(<table_hline>)[table.hline]元素。
```typc
grid.hline(
y: auto int,
start: int,
end: none int,
stroke: none length color gradient stroke tiling dictionary,
position: alignment,
) → content
```
-- *y*: auto int<grid_hline_y>
水平线放置在该行的上方(从零开始索引)。如果position字段设置为bottom,则该线条会放置在指定索引行的下方(详见#link(<grid_hline_position>)[grid.hline.position])。
设置为auto时,线条将置于网格子元素里,位于该线条之前、最后一个自动定位单元格(即没有坐标覆盖的单元格)的下一行。若线条前方不存在这类单元格,则线条放置在网格最顶部(第0行)。注意:若为此选项填写网格的总行数,这条水平线会覆盖网格的下边框;而取值为0时,则会覆盖上边框。默认值:```typc auto```。
-- *start*: int<grid_hline_start>
水平线起始的列(从零开始索引,包含该列)。默认值:```typc 0```。
-- *end*: none int<grid_hline_end>
水平线终止所在列(从零开始索引,不包含该列)。也就是说,水平线将绘制到 end - 1 列并贯穿该列。默认值:```typc none```。
若取值为 none 或等于总列数,则水平线会一直延伸至网格末尾。
-- *stroke*: none length color gradient stroke tiling dictionary<grid_hline_stroke>
线条的描边。默认值:```typc 1pt+black```。
指定为无会移除先前在该线条范围内绘制的所有线条,包括水平横线或其下方单元格描边。
-- *position*: alignment<grid_hline_position>
给定线条所在行(y),确定线条的放置位置:可选顶部(绘制在该行上方)或底部(绘制在该行下方)。默认值:```typc top```。
该设置仅在启用行间距时生效(其他情况下不建议使用,可直接将y字段值加1来实现),因为启用行间距后,某一行下方的位置与下一行上方的位置会因两者之间的间隔而有所差异。
- *vline*<grid_vline>
网格中的竖线。
它会覆盖所有单元格描边,包括通过网格的stroke字段指定的描边。该竖线可以穿过由网格row-gutter选项生成的单元格间距区域。
```typc
grid.hline(
x: auto int,
start: int,
end: none int,
stroke: none length color gradient stroke tiling dictionary,
position: alignment,
) → content
```
-- *x*: auto int<grid_vline_x>
竖线放置在该列之前(采用从零开始的索引)。如果position字段设置为end,则该线条会放置在指定索引列的后方(详见#link(<grid_vline_position>)[grid.vline.position])。默认值:```typc auto```。
设置为#text(fill: red,"auto")时,线条将放置在网格子元素中、线条前方最后一个自动定位单元格(即未手动指定坐标的单元格)的后一列。若线条前方不存在这类单元格,则线条置于网格首列(第0列)之前。请注意:当该选项的值恰好等于网格总列数时,这条竖线会覆盖网格的末端边框(从左到右布局下为右侧,从右到左布局下为左侧);而取值为0时,会覆盖起始边框(从左到右布局下为左侧,从右到左布局下为右侧)。
-- *start*: int<grid_vline_start>
竖线起始所在行(从零开始索引,包含该行)。默认值:```typc 0```。
-- *end*: none int<grid_vline_end>
竖线终止所在的行(从零开始索引,不包含该行)。因此,竖线会绘制至 end - 1 行并贯穿该行。默认值:```typc none```。
取值为 none 或等于总行数时,竖线将一直延伸到网格底部。
-- *stroke*: none length color gradient stroke tiling dictionary<grid_vline_stroke>
线条的描边。默认值:```typc 1pt+black```。
指定为无会清除此前在该线条范围内绘制的所有线条,包括垂直线或其下方的单元格描边。
-- *position*: alignment<grid_vline_position>
线条的放置位置,由其所在列(x)决定——可以选择在该列之前开始绘制,或是在该列之后结束绘制。默认值:```typc start```。
也支持使用 left 和 right 值,但不推荐,因为这会导致网格在从左到右和从右到左的文档中出现不一致。
该设置仅在启用列间距时生效(其他情况下不应使用,建议直接将x字段加1即可),因为启用列间距后,受两列之间空隙的影响,一列结束后的位置与下一列开始前的位置不再相同。
- *header*
可重复的网格表头。
如果repeat设置为true,表头将跨页面重复显示。示例可参考#link(<table_header>)[table.header]元素以及#link(<grid_stroke>)[grid.stroke]参数。
```typc
grid.header(
repeat: bool,
level: int,
children:..content,
) → content
```
-- *repeat*: bool
此表头是否需要跨页重复显示。默认值:```typc true```。
-- *level*: int
标题的层级,不得为零。默认值:```typc 1```。
该设置支持一次性重复多个标题。不同层级的标题可一起重复,前提是层级呈升序排列。
值得注意的是,当更低层级的标题开始重复时,所有更高层级或同等层级的标题将停止重复(它们会被新标题“替换”)。
-- *children*: content
表头内的单元格与线条。
- *footer*
可重复网格页脚。
与grid.header元素类似,该页脚可以在网格的每一页重复显示。
页脚之后不能放置任何其他网格单元格。
```typc
grid.footer(
repeat: bool,
children:..content,
) → content
```
-- *repeat*: bool
是否在所有页面重复显示该页脚。默认值:```typc true```。
-- *children*: content
页脚内的单元格和线条。
== 隐藏(hide)<hide>
隐藏内容但不改变布局。
hide函数可以隐藏内容,布局仍能“识别”到该内容。这适合创建和某些内容大小完全一致的空白区域。
=== 示例(Example)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
Hello Jane \
#hide[Hello] Joe
```
],[
#set align(horizon)
#image("images/6053.png",width: auto)]
)
=== 修订(Redaction)<hide_Redaction>
此函数在编辑内容时可能也很有用,因为其参数既不会直观显示,也无法被辅助技术访问。话虽如此,隐藏的内容仍可能留下一些痕迹(例如PDF文档大纲中的书签标题)。
请注意,根据具体情况,有可能根据布局中的内容大小对其进行逆向工程。因此,我们不建议使用此功能来隐藏高度敏感的信息。
=== 参数(Parameters)
```typc
hide(body: content) → content
```
- *body*: content
需要隐藏的内容。
== 布局(layout)<layout>
提供对当前外部容器(若无容器则为页面)尺寸(宽度和高度)的访问能力。
接收一个函数,该函数接收单个参数,此参数是包含 width 和 height 键的字典,二者均为#link(<length>)[长度]类型。该函数自带#link(<Context>)[上下文],即无需搭配 context 关键字使用。这也是下方示例中可以调用 #link(<measure>)[measure] 的原因。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#let text = lorem(30)
#layout(size => [
#let (height,) = measure(
width: size.width,
text,
)
This text is #height high with
the current page width: \
#text
])
```
],[
#set align(horizon)
#image("images/6054.png",width: auto)]
)
请注意,布局函数会将其内容强制放入一个块级容器中,因此无法在该容器内部设置相对于页面或分页符的位置。
如果布局调用置于一个宽#rtext()[800pt]、高#rtext()[400pt]的盒子内,那么指定函数会收到参数(宽度:#rtext()[800pt],高度:#rtext()[400pt])。如果直接放在页面中,它获取的是页面尺寸减去页边距后的数值。该特性大多配合尺寸测量使用。
若要获取页面的剩余高度而非页面总尺寸,可以将布局调用包裹在 ```typc block(height: 1fr)``` 中。其原理是该块会自动拉伸以填满剩余空间(更多细节可参阅#link(<fraction>)[分数]单位文档)。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set page(height: 150pt)
#lorem(20)
#block(height: 1fr, layout(size => [
Remaining height: #size.height
]))
```
],[
#set align(horizon)
#image("images/6055.png",width: auto)]
)
你也可以使用该函数将比例换算为固定长度。当你在构建自定义布局抽象层时,这个功能会很有用。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#layout(size => {
let half = 50% * size.width
[Half a page is #half wide.]
})
```
],[
#set align(horizon)
#image("images/6056.png",width: auto)]
)
请注意:如果对应的页面尺寸设置为#rtext()[auto],布局所提供的宽度或高度将为无限值。
=== 参数(Parameters)
```typc
layout(func: function) → content
```
- *func*:function
接收外部容器尺寸并执行调用的函数。该函数的返回值会显示在文档中。
容器尺寸以字典形式提供,包含 width(宽度)和 height(高度)两个键,二者均为长度类型。
每当 layout 返回的内容在文档中渲染一次,该函数就会被调用一次。利用这一特性,可以生成依赖容器尺寸的内容。
== 长度(length)<length>
尺寸或距离,可附带上下文单位进行表示。
Typst支持以下长度单位:
-- 磅:72pt
-- 毫米:254mm
-- 厘米:2.54cm
-- 英寸:1in
-- 相对于字体大小:2.5em
你可以对长度乘以或除以整数与浮点数。
=== 示例(Example)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#rect(width: 20pt)
#rect(width: 2em)
#rect(width: 1in)
#(3em + 5pt).em \
#(20pt).em \
#(40em + 2pt).abs \
#(5em).abs
```
],[
#set align(horizon)
#image("images/6057.png",width: auto)]
)
=== 域(Fields)<length_fields>
-- abs:仅包含当前长度绝对部分的长度(即排除em分量)。
-- em:此长度中的em单位数量,为浮点数。
=== 定义(Definitions)
- *pt*<length_pt>
将此长度转换为磅值。
若该长度包含非零em单位(例如 5em + 2pt,而非仅 2pt),则会报错。可使用 abs 字段(例如 (5em + 2pt).abs.pt())忽略长度中的em部分(仅转换其绝对分量)。
```typc
self.pt() → float
```
- *mm*<length_mm>
将此长度转换为毫米。
若该长度包含非零em单位,则会报错。更多详情请参见pt方法。
```typc
self.mm() → float
```
- *cm*<length_cm>
将此长度转换为厘米。
如果该长度包含非零em单位,则会报错。更多详情请参阅pt方法。
```typc
self.cm() → float
```
- *inches*<length_inches>
将此长度转换为英寸。
如果此长度包含非零 em 单位,则执行失败并报错。更多详情请参见 #link(<length_pt>)[pt] 方法。
- *to-absolute*<length_to-absolute>
将该长度解析为绝对长度。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set text(size: 12pt)
#context [
#(6pt).to-absolute() \
#(6pt + 10em).to-absolute() \
#(10em).to-absolute()
]
#set text(size: 6pt)
#context [
#(6pt).to-absolute() \
#(6pt + 10em).to-absolute() \
#(10em).to-absolute()
]
```
],[
#set align(horizon)
#image("images/6058.png",width: auto)]
)
```typc
self.to-absolute() → length
```
== 测量(measure)<measure>
测量内容完成布局后的尺寸。
measure函数用于获取内容完成布局后的尺寸。默认情况下会假定空间无限大,因此测量得到的尺寸不一定和内容最终的实际尺寸一致。如果你希望基于当前布局尺寸进行测量,可以将measure与#link(<layout>)[layout]配合使用。
=== 示例(Example)
同样的内容,尺寸会随其所处的上下文环境而变化。在下面的示例中,当我们增大字体大小时,#link(<content>)[content]区域自然会变大。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#let content = [Hello!]
#content
#set text(14pt)
#content
```
],[
#set align(horizon)
#image("images/6059.png",width: auto)]
)
因此,只有在具备上下文的情况下,你才能进行测量。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#let thing(body) = context {
let size = measure(body)
[Width of "#body" is #size.width]
}
#thing[Hey] \
#thing[Welcome]
```
],[
#set align(horizon)
#image("images/6060.png",width: auto)]
)
measure函数会返回一个包含width和height项的字典,两项均为#link(<length>)[长度]类型。
=== 参数(Parameters)
```typc
measure(
width: auto length,
height: auto length,
content: content,
) → dictionary
```
- *width*: auto length
用于布局内容的可用宽度。默认值:```typc auto```。
将此项设为auto代表可用宽度为无限大。
注意:使用该函数的宽度和高度参数,与测量包含内容的定尺寸区#link(<block>)[块]有所不同。在下面的示例中,前者获取的是内部内容的尺寸,而非区块本身的尺寸。
#table(
columns: 1fr,
align: left,
stroke: none,
[
```typst
#context measure(lorem(100), width: 400pt)
#context measure(block(lorem(100), width: 400pt))
```
],
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6061.png",width: 80%)
]
)
]
)
- *height*: auto length
可用于布局内容的可用高度。默认值:```typc auto```。
将此项设置为auto表示可用高度为无限。
- *content*: content
待测量大小的内容。
== 移动(move)<move>
移动内容而不影响布局。
move函数支持移动内容,同时布局仍“认为”该内容位于原始位置。容器的尺寸计算方式保持不变,如同内容未被移动。
=== 示例(Example)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#rect(inset: 0pt, fill: gray, move(
dx: 4pt, dy: 6pt,
rect(
inset: 8pt,
fill: white,
stroke: black,
[Abra cadabra]
)
))
```
],[
#set align(horizon)
#image("images/6062.png",width: auto)]
)
=== 无障碍性(Accessibility)
移动效果对辅助技术(AT)是透明的。无论视觉上如何移动,辅助技术都会按照内容在源文件中的显示顺序读取内容。如果在导出PDF时需要让辅助技术完全忽略某部分内容,可以考虑使用pdf.artifact。
=== 参数(Parameters)
```typc
move(
dx: relative,
dy: relative,
body: content,
) → content
```
- *dx*: relative
内容的水平位移。默认值:```typc 0%+0pt```。
- *dy*: relative
内容的垂直位移。默认值:```typc 0%+0pt```。
- *body*: content
待移动的内容。
== 边距(pad)<pad>
为内容四周添加间距。
可以单独设置每一侧的间距,也可以通过指定位置参数一次性设置全部四个侧边的间距。
=== 示例(Example)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set align(center)
#pad(x: 16pt, image("typing.jpg"))
_Typing speeds can be
measured in words per minute._
```
],[
#set align(horizon)
#image("images/6063.png",width: auto)]
)
=== 参数(Parameters)
```typc
pad(
left: relative,
top: relative,
right: relative,
bottom: relative,
x: relative,
y: relative,
rest: relative,
body: content,
) → content
```
- *left*: relative<pad_left>
左侧的内边距。默认值:```typc 0%+0pt```。
- *top*: relative<pad_top>
顶部的内边距。默认值:```typc 0%+0pt```。
- *right*: relative<pad_right>
右侧的内边距。默认值:```typc 0%+0pt```。
- *bottom*: relative<pad_bottom>
底部的内边距。默认值:```typc 0%+0pt```。
- *x*: relative<pad_x>
一种将左侧和右侧设为相同值的简写方式。默认值:```typc 0%+0pt```。
- *y*: relative<pad_y>
可同时将上边距和下边距设为相同值的简写方式。默认值:```typc 0%+0pt```。
- *rest*: relative<pad_rest>
一种简写方式,可将四条边设置为相同的值。默认值:```typc 0%+0pt```。
- *body*: content<pad_body>
需要在两侧填充的内容。
== 页面(page)<page>
将其子元素布局到一页或多页上。
虽然该函数主要在集合规则中用于设置页面属性,但也可以用来将参数内容显式渲染到一组独立页面中。
页面的宽度或高度可以设置为auto。在这种情况下,页面会沿对应轴自动延展以适配内容。
页面设置指南讲解了如何使用本函数及相关函数来配置文档,并附带大量示例。
=== 示例(Example)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set page("us-letter")
There you go, US friends!
```
],[
#set align(horizon)
#image("images/6064.png",width: auto)]
)
=== 无障碍(Accessibility)
屏幕阅读器等辅助技术(AT)无法读取页面页眉、页脚、前景和背景中的内容,辅助技术仅会读取页面主体部分。请勿将文档其他位置未包含的重要信息放置在这些区域。
=== 样式(Styling)<page_Styling>
请注意,页面元素无法通过显示规则设定目标;编写 show page: .. 不会产生任何效果。若要在每一页重复显示内容,你可以改用 set 规则配置页眉、页脚、背景和前景属性。
=== 参数(Parameters)
```typc
page(
paper: str,
width: auto length,
height: auto length,
flipped: bool,
margin: auto relative dictionary,
bleed: relative dictionary,
binding: auto alignment,
columns: int,
fill: none auto color gradient tiling,
numbering: none str function,
supplement: none auto content,
number-align: alignment,
header: none auto content,
header-ascent: relative,
footer: none auto content,
footer-descent: relative,
background: none content,
foreground: none content,
body: content,
) → content
```
- *paper*: str<page_paper>
用于设置宽高的标准纸张尺寸。默认值:```typc "a4"```。
这只是设置宽高的简写形式,因此无法在上下文表达式中获取该值。
#table(
columns: (1fr,1.5fr),
align: left,
stroke: none,
table.header([Variant],[Details]),
table.hline(),
["a0"],[尺寸为841.0 × 1189.0毫米的纸张。],
["a1"],[尺寸为594.0 × 841.0毫米的纸张。],
["a2"],[尺寸为420.0 × 594.0毫米的纸张。],
["a3"],[尺寸为297.0 × 420.0毫米的纸张。],
["a4"],[尺寸为210.0 × 297.0毫米的纸张。],
["a5"],[尺寸为148.0 × 210.0毫米的纸张。],
["a6"],[尺寸为105.0 × 148.0毫米的纸张。],
["a7"],[尺寸为74.0 × 105.0毫米的纸张。],
["a8"],[尺寸为52.0 × 74.0毫米的纸张。],
["a9"],[尺寸为37.0 × 52.0毫米的纸张。],
["a10"],[尺寸为26.0 × 37.0毫米的纸张。],
["a11"],[尺寸为18.0 × 26.0毫米的纸张。],
["iso-b1"],[尺寸为707.0 × 1000.0毫米的纸张。],
["iso-b2"],[尺寸为500.0 × 707.0毫米的纸张。],
["iso-b3"],[尺寸为353.0 × 500.0毫米的纸张。],
["iso-b4"],[尺寸为250.0 × 353.0毫米的纸张。],
["iso-b5"],[尺寸为176.0 × 250.0毫米的纸张。],
["iso-b6"],[尺寸为125.0 × 176.0毫米的纸张。],
["iso-b7"],[尺寸为88.0 × 125.0毫米的纸张。],
["iso-b8"],[尺寸为62.0 × 88.0毫米的纸张。],
["iso-c3"],[尺寸为324.0 × 458.0毫米的纸张。],
["iso-c4"],[尺寸为229.0 × 324.0毫米的纸张。],
["iso-c5"],[尺寸为162.0 × 229.0毫米的纸张。],
["iso-c6"],[尺寸为114.0 × 162.0毫米的纸张。],
["iso-c7"],[尺寸为81.0 × 114.0毫米的纸张。],
["iso-c8"],[尺寸为57.0 × 81.0毫米的纸张。],
["din-d3"],[尺寸为272.0 × 385.0毫米的纸张。],
["din-d4"],[尺寸为192.0 × 272.0毫米的纸张。],
["din-d5"],[尺寸为136.0 × 192.0毫米的纸张。],
["din-d6"],[尺寸为96.0 × 136.0毫米的纸张。],
["din-d7"],[尺寸为68.0 × 96.0毫米的纸张。],
["din-d8"],[尺寸为48.0 × 68.0毫米的纸张。],
["sis-g5"],[尺寸为169.0 × 239.0毫米的纸张。],
["sis-e5"],[尺寸为115.0 × 220.0毫米的纸张。],
["ansi-a"],[尺寸为216.0 × 279.0毫米的纸张。],
["ansi-b"],[尺寸为279.0 × 432.0毫米的纸张。],
["ansi-c"],[尺寸为432.0 × 559.0毫米的纸张。],
["ansi-d"],[尺寸为559.0 × 864.0毫米的纸张。],
["ansi-e"],[尺寸为864.0 × 1118.0毫米的纸张。],
["arch-a"],[尺寸为229.0 × 305.0毫米的纸张。],
["arch-b"],[尺寸为305.0 × 457.0毫米的纸张。],
["arch-c"],[尺寸为457.0 × 610.0毫米的纸张。],
["arch-d"],[尺寸为610.0 × 914.0毫米的纸张。],
["arch-e1"],[尺寸为762.0 × 1067.0毫米的纸张。],
["arch-e"],[尺寸为914.0 × 1219.0毫米的纸张。],
["jis-b0"],[尺寸为1030.0 × 1456.0毫米的纸张。],
["jis-b1"],[尺寸为728.0 × 1030.0毫米的纸张。],
["jis-b2"],[尺寸为515.0 × 728.0毫米的纸张。],
["jis-b3"],[尺寸为364.0 × 515.0毫米的纸张。],
["jis-b4"],[尺寸为257.0 × 364.0毫米的纸张。],
["jis-b5"],[尺寸为182.0 × 257.0毫米的纸张。],
["jis-b6"],[尺寸为128.0 × 182.0毫米的纸张。],
["jis-b7"],[尺寸为91.0 × 128.0毫米的纸张。],
["jis-b8"],[尺寸为64.0 × 91.0毫米的纸张。],
["jis-b9"],[尺寸为45.0 × 64.0毫米的纸张。],
["jis-b10"],[尺寸为32.0 × 45.0毫米的纸张。],
["jis-b11"],[尺寸为22.0 × 32.0毫米的纸张。],
["sac-d0"],[尺寸为764.0 × 1064.0毫米的纸张。],
["sac-d1"],[尺寸为532.0 × 760.0毫米的纸张。],
["sac-d2"],[尺寸为380.0 × 528.0毫米的纸张。],
["sac-d3"],[尺寸为264.0 × 376.0毫米的纸张。],
["sac-d4"],[尺寸为188.0 × 260.0毫米的纸张。],
["sac-d5"],[尺寸为130.0 × 184.0毫米的纸张。],
["sac-d6"],[尺寸为92.0 × 126.0毫米的纸张。],
["iso-id-1"],[尺寸为85.6 × 53.98毫米的纸张。],
["iso-id-2"],[尺寸为74.0 × 105.0毫米的纸张。],
["iso-id-3"],[尺寸为88.0 × 125.0毫米的纸张。],
["asia-f4"],[尺寸为210.0 × 330.0毫米的纸张。],
["jp-shiroku-ban-4"],[尺寸为264.0 × 379.0毫米的纸张。],
["jp-shiroku-ban-5"],[尺寸为189.0 × 262.0毫米的纸张。],
["jp-shiroku-ban-6"],[尺寸为127.0 × 188.0毫米的纸张。],
["jp-kiku-4"],[尺寸为227.0 × 306.0毫米的纸张。],
["jp-kiku-5"],[尺寸为151.0 × 227.0毫米的纸张。],
["jp-business-card"],[尺寸为91.0 × 55.0毫米的纸张。],
["cn-business-card"],[尺寸为90.0 × 54.0毫米的纸张。],
["eu-business-card"],[尺寸为85.0 × 55.0毫米的纸张。],
["fr-tellière"],[尺寸为340.0 × 440.0毫米的纸张。],
["fr-couronne-écriture"],[尺寸为360.0 × 460.0毫米的纸张。],
["fr-couronne-édition"],[尺寸为370.0 × 470.0毫米的纸张。],
["fr-raisin"],[尺寸为500.0 × 650.0毫米的纸张。],
["fr-carré"],[尺寸为450.0 × 560.0毫米的纸张。],
["fr-jésus"],[尺寸为560.0 × 760.0毫米的纸张。],
["uk-brief"],[尺寸为406.4 × 342.9毫米的纸张。],
["uk-draft"],[尺寸为254.0 × 406.4毫米的纸张。],
["uk-foolscap"],[尺寸为203.2 × 330.2毫米的纸张。],
["uk-quarto"],[尺寸为203.2 × 254.0毫米的纸张。],
["uk-crown"],[尺寸为508.0 × 381.0毫米的纸张。],
["uk-book-a"],[尺寸为111.0 × 178.0毫米的纸张。],
["uk-book-b"],[尺寸为129.0 × 198.0毫米的纸张。],
["us-letter"],[尺寸为215.9 × 279.4毫米的纸张。],
["us-legal"],[尺寸为215.9 × 355.6毫米的纸张。],
["us-tabloid"],[尺寸为279.4 × 431.8毫米的纸张。],
["us-executive"],[尺寸为184.15 × 266.7毫米的纸张。],
["us-foolscap-folio"],[尺寸为215.9 × 342.9毫米的纸张。],
["us-statement"],[尺寸为139.7 × 215.9毫米的纸张。],
["us-ledger"],[尺寸为431.8 × 279.4毫米的纸张。],
["us-oficio"],[尺寸为215.9 × 340.36毫米的纸张。],
["us-gov-letter"],[尺寸为203.2 × 266.7毫米的纸张。],
["us-gov-legal"],[尺寸为215.9 × 330.2毫米的纸张。],
["us-business-card"],[尺寸为88.9 × 50.8毫米的纸张。],
["us-digest"],[尺寸为139.7 × 215.9毫米的纸张。],
["us-trade"],[尺寸为152.4 × 228.6毫米的纸张。],
["newspaper-compact"],[尺寸为280.0 × 430.0毫米的纸张。],
["newspaper-berliner"],[尺寸为315.0 × 470.0毫米的纸张。],
["newspaper-broadsheet"],[尺寸为381.0 × 578.0毫米的纸张。],
["presentation-16-9"],[尺寸为297.0 × 167.0625毫米的纸张。],
["presentation-4-3"],[尺寸为280.0 × 210.0毫米的纸张。],
)
- *width*: auto length<page_width>
应用所有裁切后最终页面的宽度。默认值:```typc 595.28pt```。
在专业印刷设置中,该尺寸可能小于送入打印机的纸张尺寸。
有关如何设置裁切或溢出区域的详细信息,请参阅溢出参数。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set page(
width: 3cm,
margin: (x: 0cm),
)
#for i in range(3) {
box(square(width: 1cm))
}
```
],[
#set align(center)
#image("images/6065.png",width: auto)]
)
- *height*: auto length<page_height>
应用所有裁切设置后的最终页面区域高度。默认值:```typc 841.89pt```。
如果该项设置为auto,则仅能通过插入分页符或添加其他非空页面集规则手动触发分页。本文档中的大多数示例都将页面高度设为auto,以便页面可动态伸缩来适配内容。
- *flipped*: bool<page_flipped>
页面是否翻转至横向方向。默认值:```typc false```。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set page(
"us-business-card",
flipped: true,
fill: rgb("f2e5dd"),
)
#set align(bottom + end)
#text(14pt)[*Sam H. Richards*] \
_Procurement Manager_
#set text(10pt)
23 W 23rd Street \
New York, NY 10010 \
+1 (212) 555-0155
```
],[
#set align(center)
#image("images/6066.png",width: auto)]
)
- *margin*: auto relative dictionary<page_margin>\ 页面的边距。默认值:```typc auto```。
- #rtext()[auto]:边距自动设置为页面较小边长的2.5/21。对于A4页面,该设置会得到2.5厘米的边距。
- 单一长度值:四周使用相同的边距。
- 字典:使用字典可以分别设置各边边距。字典可包含以下键,优先级依次为:
- 上边(top):上边距。
- 右边(right):右边距。
- 下边(bottom):下边距。
- 左边(left):左边距。
- 内侧(inside):页面内侧边距(装订所在一侧)。
- 外侧(outside):页面外侧边距(与装订侧相对)。
- x:水平边距。
- y:垂直边距。
- 其余(rest):除字典中明确指定尺寸之外所有方向的边距。
所有键均为可选项;省略的键将使用其先前设置的值,如果从未设置过,则使用默认边距。此外,left(左侧)和right(右侧)的值与inside(内侧)和outside(外侧)的值互斥。取值应为相对长度或auto。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set page(
width: 3cm,
height: 4cm,
margin: (x: 8pt, y: 4pt),
)
#rect(
width: 100%,
height: 100%,
fill: aqua,
)
```
],[
#set align(center)
#image("images/6067.png",width: auto)]
)
- *bleed*: relative dictionary<page_bleed>\ 页面的溢出边距。默认值:```typc (:)```。\ 溢出区是指超出页面最终裁剪尺寸的内容区域。它确保即使发生微小的裁剪偏差,最终产品也不会出现未打印的边缘。\ 可接受值:
- 单倍长度:四周溢出量相同。
- 字典:使用字典,可以单独设置溢出边距。字典可能包含以下键,按优先级顺序列出:
- 顶部(top):顶部溢出边距。
- 右侧(right):右侧溢出边距。
- 底部(bottom):底部溢出边距。
- 左侧(left):左侧溢出边缘。
- 内侧(inside):页面内侧(#link(<page_binding>)[装订处])的溢出边距。
- 外侧(outside):页面外侧(与装订处相对)的溢出边距。
- x:水平溢出边距。
- y:垂直溢出边距。
- 其余(rest):除词典明确设定尺寸的边外,其余各边均留有溢出边距。
所有键均为可选键;省略的键将使用其之前设置的值,如果从未设置,则使用0pt。此外,left和right的值与inside和outside的值互斥。这些值应为相对长度。
在PDF导出中,如果溢出区域非零,则会为页面定义一个裁剪框。
#table(
columns: 1fr,
align: left,
stroke: none,
[
```typst
#set page(
width: 8cm,
height: 5cm,
margin: 1cm,
// The bleed is not visible in the preview;
// it exceeds beyond the page.
bleed: 0.5cm,
// Fills the entire bleed area, so there will
// be no white strips after printing and trimming.
background: rect(width: 100%, height: 100%, fill: aqua),
)
#rect(width: 100%, height: 100%, fill: white)
```
],
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6068.png",width: 80%)
]
)
]
)
- *binding*: auto alignment<page_binding>\ 页面将在哪一侧装订。默认值:```typc auto```。
- 自动:文本从左向右排版时等同于左侧;文本从右向左排版时等同于右侧。
- 左侧:在左侧装订。
- 右侧:在右侧装订。
该设置会影响内边距和外边距选项的含义。
- *columns*: int<page_columns>
该页面有多少列。默认值:```typc 1```。
如果需要在页面或其他容器中插入多列,也可以使用#link(<columns>)[分栏函数]。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set page(columns: 2, height: 4.8cm)
Climate change is one of the most
pressing issues of our time, with
the potential to devastate
communities, ecosystems, and
economies around the world. It's
clear that we need to take urgent
action to reduce our carbon
emissions and mitigate the impacts
of a rapidly changing climate.
```
],[
#set align(horizon)
#image("images/6069.png",width: auto)]
)
- *fill*: none auto color gradient tiling<page_fill>
页面的背景填充。默认值:```typc auto```。
将此项设置为非透明颜色时,会告知打印机对整页进行着色。如果考虑大批量印刷,采购预染色纸张、不设置此属性会更加环保且划算。
设为none时,背景变为透明。请注意,PDF页面在查看器中依旧会显示(通常为白色的)背景,但它们实际上是透明的。(打印时,背景不会使用任何色彩。)
默认值auto在输出PDF时等同于none,输出PNG和SVG时等同于白色。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set page(fill: rgb("444352"))
#set text(fill: rgb("fdfdfd"))
*Dark mode enabled.*
```
],[
#set align(horizon)
#image("images/6070.png",width: auto)]
)
- *numbering*: none str function<page_numbering>\ 如何对页面进行编号。你可以参考页面设置指南来自定义页码。默认值:```typc none```。\ 支持编号样式,或是接收一个或两个数值参数的函数:
+ 第一个数值为当前页码。
+ 第二个数值为总页数。在编号样式中,第二个数值可以省略。如果传入函数,在链接或引用场景下该函数接收一个参数;生成可见页码时则接收两个参数。
这些是由页面计数器控制的逻辑页码,因此可能与物理页码不一致。具体来说,它们是 counter(page) 的[当前值]与[最终值]。更多详情请参阅[计数器]文档。
如果指定了显式页脚(顶部对齐编号时则为页眉),则该编号设置会被忽略。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set page(
height: 100pt,
margin: (top: 16pt, bottom: 24pt),
numbering: "1 / 1",
)
#lorem(48)
```
],[
#set align(horizon)
#image("images/6071.png",width: auto)\
#image("images/6072.png",width: auto)
]
)
- *supplement*: none auto content<page_supplement>
页面补充说明。默认值:```typc auto```。
在引用页码时,该内容添加于页码之前。
#table(
columns: 1fr,
align: left,
stroke: none,
[
```typst
#set page(numbering: "1.", supplement: [p.])
= Introduction <intro>
We are on #ref(<intro>, form: "page")!
```
],
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6073.png",width: 80%)
]
)
]
)
- *number-align*: alignment<page_number-align>
页码的对齐方式。默认值:```typc center+bottom```。
若垂直位置为顶部,则页码放置在页眉;若垂直位置为底部,则页码放置在页脚。不允许设置水平对齐。如果指定了显式匹配的页眉或页脚,则页码设置无效。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set page(
margin: (top: 16pt, bottom: 24pt),
numbering: "1",
number-align: right,
)
#lorem(30)
```
],[
#set align(horizon)
#image("images/6074.png",width: auto)]
)
- *header*: none auto content<page_header>\ 页面的页眉。占据每页的上边距区域。默认值:```typc auto```。
- 内容:将指定内容显示为页眉。
- 自动:若已设置编号且编号对齐方式为顶部,则显示页码。
- 无:不显示页眉。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set par(justify: true)
#set page(
margin: (top: 32pt, bottom: 20pt),
header: [
#set text(8pt)
#smallcaps[Typst Academy]
#h(1fr) _Exercise Sheet 3_
],
)
#lorem(19)
```
],[
#set align(horizon)
#image("images/6075.png",width: auto)]
)
- *header-ascent*: relative<page_header-ascent>
页眉向上伸入上边距的距离。该比例相对于上边距的高度。默认值:```typc 30%+0pt```。
- *footer*: none auto content<page_footer>\ 页面的页脚。填充每一页的底部边距。默认值:```typc auto```。
- 内容:将该内容显示为页脚。
- 自动:若已设置编号且编号对齐方式为底部,则显示页码。
- 无:不显示页脚。
仅需要页码时,使用编号属性通常就足够。如果你想创建自定义页脚同时仍然显示页码,可以直接调用页面计数器。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set par(justify: true)
#set page(
height: 100pt,
margin: 20pt,
footer: context [
#set align(right)
#set text(8pt)
#counter(page).display(
"1 of I",
both: true,
)
]
)
#lorem(48)
```
],[
#set align(horizon)
#image("images/6076.png",width: auto)\
#image("images/6077.png",width: auto)
]
)
- *footer-descent*: relative<page_footer-descent>
页脚向下延伸至下边距的距离。该比例相对于下边距的高度。默认值:```typc 30%+0pt```。
#table(
columns: 1fr,
align: left,
stroke: none,
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6078.png",width: 80%)
]
)
]
)
- *background*: none content<page_background>
页面背景中的内容。默认值:```typc none```。
该内容会置于页面主体的后方,可用于放置背景图片或水印。
为方便使用,在背景内容中使用[相对长度]时,会依据包含页面#link(<page_bleed>)[溢出]在内的页面尺寸进行解析。例如,页面宽度为100毫米,溢出位为5毫米,则100%宽度的计算结果为5毫米 + 100毫米 + 5毫米。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set page(background: rotate(24deg,
text(18pt, fill: rgb("FFCBC4"))[
*CONFIDENTIAL*
]
))
= Typst's secret plans
In the year 2023, we plan to take
over the world (of typesetting).
```
],[
#set align(horizon)
#image("images/6079.png",width: auto)]
)
- *foreground*: none content<page_foreground>
页面前景中的内容。默认值:```typc none```。
该内容将覆盖页面主体。
相对长度将依据包含#link(<page_bleed>)[溢出]的页面尺寸进行解析,遵循与#link(<page_background>)[背景]相同的规则。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set page(foreground: text(24pt)[🤓])
Reviewer 2 has marked our paper
"Weak Reject" because they did
not understand our approach...
```
],[
#set align(horizon)
#image("images/6080.png",width: auto)]
)
- *body*: content<page_body>
页面内容。
如果内容无法在单页容纳,将会创建多个页面。正文排版完成后,会新建一个页面,该页面保留调用函数前的页面属性。
== 分页(pagebreak)<pagebreak>
手动分页符。
不得在任何容器内部使用。
=== 示例(Example)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
The next page contains
more details on compound theory.
#pagebreak()
== Compound Theory
In 1984, the first ...
```
],[
#set align(horizon)
#image("images/6081.png",width: auto)
#image("images/6082.png",width: auto)
]
)
即使不设置手动分页符,内容也会根据配置的页面尺寸自动分页。你可以将#link(<page_height>)[页面高度]设置为#text(fill: red,"auto"),让页面动态延展,直至遇到手动分页符。
分页机制会尽量避免在页面顶部或底部出现单行文本(这类单行被称为孤行)。你可以调整 #link(<text_costs>)[text.costs] 参数关闭该功能。
=== 参数(Parameters)
```typc
pagebreak(
weak: bool,
to: none str,
) → content
```
- *weak*: bool
如果为真(#text(fill: red,"true")),当当前页面已经为空时,将跳过分页符。默认值:```typc false```。
- *to*: none str
如果设置该参数,则可确保下一页为偶数页或奇数页;必要时会在中间插入空白页。默认值:```typc none```。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set page(height: 30pt)
First.
#pagebreak(to: "odd")
Third.
```
],[
#set align(horizon)
#image("images/6083.png",width: auto)
#image("images/6084.png",width: auto)
#image("images/6085.png",width: auto)
]
)
#table(
columns: (1fr,1.5fr),
align: left,
stroke: none,
table.header([Variant],[Details]),
table.hline(),
[\"even\"],[下一页将是偶数页。],
[\"odd\"],[下一页将是奇数页。]
)
== 放置(place)<place>
将内容相对于其父容器进行放置。
被放置的内容可以是叠加模式(默认)或浮动模式。叠加模式的内容会按照指定#link(<place_alignment>)[对齐方式]与父容器对齐,并显示在容器内目前已添加的所有其他内容之上。浮动模式的内容放置在容器的顶部或底部,会相应将其他内容向下或向上挤开。两种模式下,都可通过#link(<place_dx>)[dx]、#link(<place_dy>)[dy]偏移量调整内容位置,且不会影响整体布局。
父元素可以是任意容器,例如#link(<block>)[块]、#link(<box>)[盒子]、矩形等。顶层place调用会直接将内容放置在当前页面的文本区域内,可用于页面上的绝对定位:采用上+左#link(<alignment>)[对齐]时,偏移量dx和dy会以文本区域左上角为基准,设定元素左上角的位置。如果需要在包含页边距的完整页面上实现绝对定位,可在#link(<page_foreground>)[page.foreground]或#link(<page_background>)[page.background]中使用place。
=== 示例(Examples)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set page(height: 120pt)
Hello, world!
#rect(
width: 100%,
height: 2cm,
place(horizon + right, square()),
)
#place(
top + left,
dx: -5pt,
square(size: 5pt, fill: red),
)
```
],[
#set align(horizon)
#image("images/6086.png",width: auto)
]
)
=== 对其他元素位置的影响(Effect on the position of other elements)
叠加元素不会在内容流中占用空间,但位置调用会在内容流中插入一个不可见的块级元素。这可能会打断当前段落,进而影响布局。为避免该问题,当在段落中间执行位置调用时,可以将位置调用包裹在一个容器内。随后,对齐方式与偏移量将相对于这个零尺寸容器生效。为确保它不会干扰间距,该容器应使用连字符连接符附着在某个字词上。
例如,下面这段代码定义了一个函数,用于将注释附加到后续单词上:
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#let annotate(..args) = {
box(place(..args))
sym.wj
h(0pt, weak: true)
}
A placed #annotate(square(), dy: 2pt)
square in my text.
```
],[
#set align(horizon)
#image("images/6087.png",width: auto)
]
)
零宽弱间距用于舍弃函数调用与下一个单词之间的空格。
=== 参数(Parameters)
```typc
place(
alignment:auto alignment,
scope: str,
float: bool,
clearance: length,
dx: relative,
dy: relative,
body: content,
) → content
```
- *alignment*: auto alignment<place_alignment>\ 设置内容相对于父容器的放置位置。
- 若float为#rtext()[false],则该值可设置为除auto以外的任意对齐方式。
- 若float为#rtext()[true],则该值必须为#rtext()[auto]、top或bottom。
当float为#rtext()[false]且未指定垂直对齐方式时,内容将放置在垂直轴上的当前位置。
- *scope*: str<place_scope>
某物放置所参照的包含作用域。
父级作用域主要用于图形,正因如此,图形函数设有对应的#link(<figure>)[作用域参数]。不过,它同样可用于更通用的场景,实现跨列排版。典型示例:在双栏文档中#link(<page_guide-Add-columns>)[创建一个通栏的标题区域]。
请注意,父级作用域定位目前仅在浮动(float)开启时支持。该限制未来可能会调整。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set page(height: 150pt, columns: 2)
#place(
top + center,
scope: "parent",
float: true,
rect(width: 80%, fill: aqua),
)
#lorem(25)
```
],[
#set align(horizon)
#image("images/6088.png",width: auto)
]
)
#table(
columns: (0.5fr,1.5fr),
align: left,
stroke: none,
table.header([Variant],[Details]),
table.hline(),
[\"column\"],[放入当前列。],
[\"parent\"],[相对于父元素进行定位,使内容跨所有列显示。]
)
- *float*: bool<place_float>
所放置元素是否为浮动布局。默认值:```typc false```。
浮动元素位于父容器的顶部或底部,会挤占常规流中的内容。它们之间始终按照常规流顺序摆放,同时会置于后续#link(<place_flush>)[place.flush]元素之后的所有内容之前。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#set page(height: 150pt)
#let note(where, body) = place(
center + where,
float: true,
clearance: 6pt,
rect(body),
)
#lorem(10)
#note(bottom)[Bottom 1]
#note(bottom)[Bottom 2]
#lorem(40)
#note(top)[Top]
#lorem(10)
```
],[
#set align(horizon)
#image("images/6089.png",width: auto)
#image("images/6090.png",width: auto)
]
)
- *clearance*: length<place_clearance>
浮动布局中,已放置元素与其他元素之间的间距。
当float为#rtext()[false]时,该属性不生效。默认值:```typc 1.5em```。
- *dx*: relative<place_dx>
所放置内容的水平位移。默认值:```typc 0%+0pt```。
#table(
columns: 1fr,
align: left,
stroke: none,
[
```typst
#set page(height: 100pt)
#for x in range(-8, 8) {
place(center + horizon,
dx: x * 8pt, dy: x * 4pt,
text(
size: calc.root(x + 10, 3) * 6pt,
fill: color.mix((green, 8 - x), (blue, 8 + x)),
)[T]
)
}
```
],
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6091.png",width: 80%)
]
)
]
)
这不会影响流式内容的布局。换句话说,放置的内容会被视作包裹在移动元素内进行处理。
- *dy*: relative<place_dy>
所放置内容的垂直位移。默认值:```typc 0%+0pt```。
这不会影响常规流内内容的布局。换言之,该放置内容的处理方式,如同其被包裹在一个移动元素中。
- *body*: content<place_body>
待放置的内容。
=== 定义(Definition)
- *flush*<place_flush>
请求布局算法先放置待处理的浮动元素,再继续处理后续内容。
这有助于防止浮动图元溢入下一个章节。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#lorem(15)
#figure(
rect(width: 100%, height: 50pt),
placement: auto,
caption: [A rectangle],
)
#place.flush()
This text appears after the figure.
```
],[
#set align(horizon)
#image("images/6092.png",width: auto)
#image("images/6093.png",width: auto)
]
)
== 比率(ratio)<ratio>
一个整体的比率。
比率通常以数字形式表示,后面跟一个百分号。比率通常作为#link(<relative>)[相对长度]的一部分出现,用于指定某个布局元素相对于页面或某个容器的尺寸。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#rect(width: 25%)
```
],[
#set align(horizon)
#image("images/6094.png",width: auto)
]
)
然而,它们也可以描述任何其他相对于某个基点的属性,例如,[水平缩放量]或#link(<Left_Right_lr_size>)[括号相对于其所包含内容的高度]。
=== 脚本(Scripting)<ratio_Scripting>
在您自己的代码中,您可以随意使用比率。您可以将它们与如下所示的各种其他类型相乘:
#table(
columns: (1fr,2fr,1fr),
align: left,
stroke: none,
table.header([*Multiply by*],[*Example*],[*Result*]),
table.hline(),
[#link(<ratio>)[ratio]],[27% \* 10%],[2.7%],
[#link(<length>)[length]],[27% \* 100pt],[27pt],
[#link(<relative>)[relative]],[27% \* (10% + 100pt)],[2.7% + 27pt],
[#link(<angle>)[angle]],[27% \* 100deg],[27deg],
[#link(<Integer>)[int]],[27% \* 2],[54%],
[#link(<float>)[float]],[27% \* 0.37037],[10%],
[#link(<frac>)[fraction]],[27% \* 3fr],[0.81fr],
)
文档中显示的比率会四舍五入到两位有效数字,以提高可读性。
== 相对长度(relative)<relative>
与某个已知长度相关的长度。
这种类型是#link(<length>)[长度]与#link(<ratio>)[比例]的组合。它通过长度与比例的加减运算得出。在需要相对长度的地方,也可以直接使用单纯的长度或比例。
=== 相对于页面(Relative to the page)
一种常见使用场景是将布局元素(例如#link(<block>)[块]、#[矩形]等)的宽度或高度设置为页面宽度的某个百分比。本例中,矩形的宽度被设置为#rtext()[25%],因此它占据页面内部宽度(总宽度减去边距)的四分之一。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#rect(width: 25%)
```
],[
#set align(horizon)
#image("images/6094.png",width: auto)
]
)
在需要相对长度的场景中,纯长度值或比例值始终有效,并且二者可以自由混用:
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#rect(width: 25%+1cm)
```
],[
#set align(horizon)
#image("images/6095.png",width: auto)
]
)
页面#link(<page_background>)[背景]与#link(<page_foreground>)[前景]中的内容,其相对长度将参照包含#link(<page_bleed>)[溢出]位的页面尺寸进行解析。做出该选择是出于使用便利的考虑:要创建适配溢出位的背景,本身就需要将背景延伸至溢出区域内。
如果你想调整元素尺寸,使其占满页面的完整宽度,有几种可行方案(具体选用哪一种很大程度上取决于你的实际使用场景):
#set par(hanging-indent: 2cm)
+ 将页面边距设为0磅(```typ #set page(margin: 0pt)```)
+ 用比例乘以已知的页面总宽度(```typc 21cm × 69%```)
+ 使用内边距抵消边距(```typ #pad(x: -2.5cm, ...)```)
+ 使用页面#link(<page_background>)[背景]或#link(<page_foreground>)[前景]字段,这类字段不会计入边距(注意:该方式会让内容脱离文档流渲染,可使用place控制内容位置)
#set par(hanging-indent: 0cm)
=== 相对于容器(Relative to a container)
当布局元素(例如#link(<rect>)[矩形])嵌套在另一个布局容器(例如#link(<block>)[块])中,而不是页面的直接子元素时,相对宽度将相对于该容器计算:
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#block(
width: 100pt,
fill: aqua,
rect(width: 50%),
)
```
],[
#set align(horizon)
#image("images/6096.png",width: auto)
]
)
=== 脚本(Scripting)
你可以将相对长度与比率、整数以及浮点数相乘。
相对长度包含以下字段:
-- length:它的#link(<length>)[长度]分量。
-- ratio:它的#link(<ratio>)[比率]分量。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#(100% - 50pt).length \
#(100% - 50pt).ratio
```
],[
#set align(horizon)
#image("images/6097.png",width: auto)
]
)
== 重复(repeat)<repeat>
在可用空间内重复内容。
这在实现自定义索引、引用或大纲时非常有用。
正文参数的实例之间可以插入空格,因此请务必相应地调整#link(<repeat_justify>)[justify]参数。
如果可用空间没有界限,则会出现错误,因为这会导致内容无限生成。
=== 示例(Example)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
Sign on the dotted line:
#box(width: 1fr, repeat[.])
#set text(10pt)
#v(8pt, weak: true)
#align(right)[
Berlin, the 22nd of December, 2022
]
```
],[
#set align(horizon)
#image("images/6098.png",width: auto)
]
)
=== 无障碍性(Accessibility)
重复的内容会被自动标记为工件,并在辅助技术(AT)中隐藏。请勿使用此功能创建对文档意义有贡献的内容。
=== 参数(Parameters)
```typc
repeat(
body: content,
gap: length,
justify: bool,
) → content
```
- *body*: content<repeat_body>
需要重复的内容。
- *gap*: length<repeat_gap>
实体各个内容之间的间隙。默认值:```typc 0pt```。
- *justify*: bool<repeat_justify>
是否增加实例之间的间距,以完全填满可用空间。默认值:```typc true```。
== 旋转(rotate)<rotate>
在不影响布局的情况下旋转内容。
按指定角度旋转元素。除非设置```typc reflow: true ```,否则布局表现如同该元素未发生旋转。
=== 示例(Example)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#stack(
dir: ltr,
spacing: 1fr,
..range(16)
.map(i => rotate(24deg * i)[X]),
)
```
],[
#set align(horizon)
#image("images/6099.png",width: auto)
]
)
=== 参数(Parameters)
```typc
rotate(
angle: angle,
origin: alignment,
reflow: bool,
body: content,
) → content
```
- *angle*: angle<rotate_angle>
旋转量。默认值:```typc 0deg```。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#rotate(-1.571rad)[Space!]
```
],[
#set align(horizon)
#image("images/6100.png",width: auto)
]
)
- *origin*: alignment<rotate_origin>
旋转的原点。默认值:```typc center+horizon```。
例如,如果你希望旋转元素的左下角保持与基线对齐,则需要将其设置为下+左。
#table(
columns: 1fr,
align: left,
stroke: none,
[
```typst
#set text(spacing: 8pt)
#let square = square.with(width: 8pt)
#box(square())
#box(rotate(30deg, origin: center, square()))
#box(rotate(30deg, origin: top + left, square()))
#box(rotate(30deg, origin: bottom + right, square()))
```
],
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6101.png",width: 80%)
]
)
]
)
- *reflow*: bool<rotate_reflow>
旋转是否影响布局。默认值:```typc false```。
若设置为#text(fill: red,"false"),旋转后的内容将保留原始内容的边界框。若设置为#text(fill: red,"true"),边界框会将内容的旋转纳入考量,并相应调整布局。
#table(
columns: 1fr,
align: left,
stroke: none,
[
```typst
Hello #rotate(90deg, reflow: true)[World]!
```
],
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6102.png",width: 80%)
]
)
]
)
- *body*: content
要旋转的内容。
== 缩放(scale)<scale>
缩放内容而不影响布局。
可通过在单个轴上指定负缩放值来镜像内容。
=== 示例(Example)
#table(
columns: 1fr,
align: left,
stroke: none,
[
```typst
#set align(center)
#scale(x: -100%)[This is mirrored.]
#scale(x: -100%, reflow: true)[This is mirrored.]
```
],
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6103.png",width: 80%)
]
)
]
)
=== 参数(Parameters)
```typc
scale(
factor: auto length ratio,
x: auto length ratio,
y: auto length ratio,
origin: alignment,
reflow: bool,
body: content,
) → content
```
- *factor*: auto length ratio<scale_factor>
作为位置参数的双轴缩放系数。这只是一种可选的简写形式,用于将x轴与y轴设置为相同数值。默认值:```typc 100%```。
- *x*: auto length ratio<scale_x>
水平缩放系数。默认值:```typc 100%```。
如果该参数为负值,物体会进行水平镜像翻转。
- *y*: auto length ratio<scale_y>
垂直缩放系数。默认值:```typc 100%```。
如果该参数为负值,物体会沿垂直方向镜像翻转。
- *origin*: alignment<scale_origin>
变换的原点。默认值:```typc center+horizon```。
#table(
columns: 1fr,
align: left,
stroke: none,
[
```typst
A#box(scale(75%)[A])A \
B#box(scale(75%, origin: bottom + left)[B])B
```
],
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6104.png",width: 80%)
]
)
]
)
- *reflow*: bool<scale_reflow>
缩放是否影响布局。默认值:```typc false```。
若设置为false,缩放后的内容允许与其他内容重叠。若设置为true,则会计算缩放后内容的新尺寸,并相应调整布局。
#table(
columns: 1fr,
align: left,
stroke: none,
[
```typst
Hello #scale(x: 20%, y: 40%, reflow: true)[World]!
```
],
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6105.png",width: 80%)
]
)
]
)
- *body*: content
待扩容的内容。
== 倾斜(skew)<skew>
使内容发生倾斜。
沿水平方向、垂直方向(或同时沿两个方向)倾斜元素。除非设置reflow: #text(fill: red,"true"),否则布局表现将如同该元素未发生倾斜。
=== 示例(Example)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#skew(ax: -12deg)[
This is some fake italic text.
]
```
],[
#set align(horizon)
#image("images/61006.png",width: auto)
]
)
=== 参数(Parameters)
```typc
skew(
ax: angle,
ay: angle,
origin: alignment,
reflow: bool,
body: content,
) → content
```
- *ax*: angle<skew_ax>
水平偏斜角。默认值:```typc 0deg```。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#skew(ax: 30deg)[Skewed]
```
],[
#set align(horizon)
#image("images/61007.png",width: auto)
]
)
- *ay*: angle<skew_ay>
垂直偏斜角。默认值:```typc 0deg```。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#skew(ay: 30deg)[Skewed]
```
],[
#set align(horizon)
#image("images/61008.png",width: auto)
]
)
- *origin*: alignment<skew_origin>
倾斜变换的原点。默认值:```typc center+horizon```。
运算过程中原点保持固定不变。
#table(
columns: 1fr,
align: left,
stroke: none,
[
```typst
X #box(skew(ax: -30deg, origin: center + horizon)[X]) X \
X #box(skew(ax: -30deg, origin: bottom + left)[X]) X \
X #box(skew(ax: -30deg, origin: top + right)[X]) X
```
],
[
#set quote(block: true)
#show quote: set align(center)
#rect(
fill: luma(240),
width: 100%,
height: auto,
quote[
#image("images/6109.png",width: 80%)
]
)
]
)
- *reflow*: bool<skew_reflow>
倾斜变换是否会影响布局。默认值:```typc false```。
若设为false,经过倾斜处理的内容将保留原始内容的边界框。若设为#text(fill: red,"true"),边界框会将内容的变换纳入考量,并相应调整布局。
#table(
columns: 1fr,
align: left,
stroke: none,
fill: (x,y)=>
if (y==0) {white}
else if (y==1) {luma(220)}
,
[
```typst
Hello #skew(ay: 30deg, reflow: true, "World")!
```
],
[
#image("images/6110.png",width: 80%)
]
)
- *body*: content
待倾斜的内容。
== 水平间距(h)<spacing_h>
在段落中插入水平间距。
该间距可以是绝对值、相对值或比例值。若为比例值,该行剩余空间会按照各比例间距的占比分配给所有比例间距。
=== 示例(Example)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
First #h(1cm) Second \
First #h(30%) Second
```
],[
#set align(horizon)
#image("images/61011.png",width: auto)
]
)
=== 分数间距(Fractional spacing)<spacing_h-Fractional>
采用分数间距时,你可以在同一行内对齐元素,而无需强制分段(#link(<align>)[align] would 则会强制分段)。每个设置了分数尺寸的元素,会根据自身分数占所有分数总和的比例分配空间。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
First #h(1fr) Second \
First #h(1fr) Second #h(1fr) Third \
First #h(2fr) Second #h(1fr) Third
```
],[
#set align(horizon)
#image("images/61012.png",width: auto)
]
)
=== 数学间距(Mathematical Spacing)<spacing_h-Mathematical>
在#link(<math>)[数学公式]中,你还可以使用以下常量在元素之间添加间距:thin(1/6 字符宽度)、med(2/9 字符宽度)、thick(5/18 字符宽度)、quad(1 字符宽度)、wide(2 字符宽度)。
=== 参数(Parameters)
```typc
h(
amount: relative fraction,
weak: bool,
) → content
```
- *amount*: relative fraction
插入多少间距。
- *weak*: bool
如果为#rtext()[true],段落开头或结尾处的间距会合并。此外,多个相邻的弱间距中,除最大的那个之外其余全部合并。默认值:```typc false```。
标记内的弱间距还会移除所有相邻的标记空格,无论插入的间距大小。若要在弱间距旁强制保留空格,可以显式编写 #" "(普通空格)或 ~(不间断空格)。后者可用于实现这样一种结构:始终通过一个不间断空格连接前一个单词,不受前方是否存在标记空格的影响。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#h(1cm, weak: true)
We identified a group of _weak_
specimens that fail to manifest
in most cases. However, when
#h(8pt, weak: true) supported
#h(8pt, weak: true) on both sides,
they do show up.
Further #h(0pt, weak: true) more,
even the smallest of them swallow
adjacent markup spaces.
```
],[
#set align(horizon)
#image("images/61013.png",width: auto)
]
)
== 垂直间距(v)<spacing_v>
在块元素流中插入垂直间距。
该间距可以是绝对值、相对值或比例值。若是比例值,页面上的剩余空间会按照各比例间距的相对占比分配给所有比例间距。
=== 示例(Example)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#grid(
rows: 3cm,
columns: 6,
gutter: 1fr,
[A #parbreak() B],
[A #v(0pt) B],
[A #v(10pt) B],
[A #v(0pt, weak: true) B],
[A #v(40%, weak: true) B],
[A #v(1fr) B],
)
```
],[
#set align(horizon)
#image("images/61014.png",width: auto)
]
)
=== 参数(Parameters)
```typc
v(
amount: relative fraction,
weak: bool,
) → content
```
- *amount*: relative fraction
插入多少间距。
- *weak*: bool
若为#rtext()[true],则流的起始或末尾处的间距会合并。此外,多个相邻的弱间距中,除最大的那个外其余都会合并。弱间距始终会合并相邻的段落间距,即便该段落间距更大。默认值:```typc false```。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
The following theorem is
foundational to the field:
#v(4pt, weak: true)
$ x^2 + y^2 = r^2 $
#v(4pt, weak: true)
The proof is simple:
```
],[
#set align(horizon)
#image("images/61015.png",width: auto)
]
)
== 堆叠(stack)<stack>
沿水平或垂直方向排布内容与间距。
堆叠组件会沿着一条轴线放置一组项目,项目之间可设置可选间距。
=== 示例(Example)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typst
#stack(
dir: ttb,
rect(width: 40pt),
rect(width: 120pt),
rect(width: 90pt),
)
```
],[
#set align(horizon)
#image("images/61016.png",width: auto)
]
)
=== 无障碍性(Accessibility)
堆栈不带有任何特殊语义。辅助技术(AT)会按照传入此函数的顺序读取堆栈中的内容。
=== 参数(Parameters)
```typc
stack(
dir: direction,
spacing: none relative fraction,
children:..relative fraction content,
) → content
```
- *dir*: direction\ 项目堆叠的方向。可选值如下:
#set par(hanging-indent: 1.3cm)
-- ltr:从左到右。
-- rtl:从右到左。
-- ttb:从上到下。
-- btt:从下到上。
#set par(hanging-indent: 0cm)
你可以使用 start 和 end 方法,分别获取该方向作为对齐方式时的起点与终点。也可以使用 axis 方法判断一个方向属于```typc "horizontal"```还是```typc "vertical"```。inv 方法返回该方向的反向方向。
例如,```typc ttb.start()``` 为顶部,```typc ttb.end()``` 为底部,```typc ttb.axis()``` 为```typc "vertical"```,```typc ttb.inv()``` 等价于 btt。
- *spacing*: none relative fraction
未指定明确间距时,项目之间插入的间距。默认值:```typc none```。
- *children*:..relative fraction content
让子内容沿着轴线堆叠。
#let rtext(body)={
text(fill:maroon,body)
}
= 布局库(Layout)
以不同的方式在页面上排列元素。
通过组合布局函数,您可以创建复杂且自动化的布局。
== 对齐(align)<align>
水平和垂直对齐内容。
=== 示例(Example)
我们先从将内容水平居中开始:
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#set page(height: 120pt)
#set align(center)
Centered text, a sight to see \
In perfect balance, visually \
Not left nor right, it stands alone \
A work of art, a visual throne
```
],[
#set align(horizon)
#image("images/6001.png",width: auto)
]
)
要将对象垂直居中,请使用水平对齐:
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#set page(height: 120pt)
#set align(horizon)
Vertically centered, \
the stage had entered, \
a new paragraph.
```
],[
#set align(horizon)
#image("images/6002.png",width: auto)
]
)
=== 合并比对结果(Combining alignments)<align_Combining-alignments>
你可以使用 + 运算符组合两种对齐方式。我们改用函数形式而非set规则,让该设置仅作用于一段内容:
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#set page(height: 120pt)
Though left in the beginning ...
#align(right + bottom)[
... they were right in the end, \
and with addition had gotten, \
the paragraph to the bottom!
]
```
],[
#set align(horizon)
#image("images/6003.png",width: auto)
]
)
=== 嵌套对齐(Nested alignment)<align_Nested-alignment>
您可以为布局容器及其内部元素使用不同的对齐方式。通过这种方式,您可以创建复杂的布局:
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
#align(center, block[
#set align(left)
Though centered together \
alone \
we \
are \
left.
])
```
],[
#set align(horizon)
#image("images/6004.png",width: auto)