提示:本教程存在过时问题,更详细教程请阅读 小蓝书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 中文文档

stroke

Defines how to draw a line.

A stroke has a paint (a solid color or gradient), a thickness, a line cap, a line join, a miter limit, and a dash pattern. All of these values are optional and have sensible defaults.

Example

#set line(length: 100%)
#stack(
  spacing: 1em,
  line(stroke: 2pt + red),
  line(stroke: (paint: blue, thickness: 4pt, cap: "round")),
  line(stroke: (paint: blue, thickness: 1pt, dash: "dashed")),
  line(stroke: 2pt + gradient.linear(..color.map.rainbow)),
)
Preview

Simple strokes

You can create a simple solid stroke from a color, a thickness, or a combination of the two. Specifically, wherever a stroke is expected you can pass any of the following values:

For full control, you can also provide a dictionary or a stroke object to any function that expects a stroke. The dictionary's keys may include any of the parameters for the constructor function, shown below.

Fields

On a stroke object, you can access any of the fields listed in the constructor function. For example, (2pt + blue).thickness is 2pt. Meanwhile, stroke(red).cap is auto because it's unspecified. Fields set to auto are inherited.

构造函数
如果类型具有构造函数,可以像函数一样调用它来创建一个该类型的值。

Converts a value to a stroke or constructs a stroke with the given parameters.

Note that in most cases you do not need to convert values to strokes in order to use them, as they will be converted automatically. However, this constructor can be useful to ensure a value has all the fields of a stroke.

#let my-func(x) = {
    x = stroke(x) // Convert to a stroke
    [Stroke has thickness #x.thickness.]
}
#my-func(3pt) \
#my-func(red) \
#my-func(stroke(cap: "round", thickness: 1pt))
Preview

paint
auto color gradient pattern
必需参数位置参数
位置参数按顺序传入,不带名称。

The color or gradient to use for the stroke.

If set to auto, the value is inherited, defaulting to black.

thickness
auto length
必需参数位置参数
位置参数按顺序传入,不带名称。

The stroke's thickness.

If set to auto, the value is inherited, defaulting to 1pt.

cap
auto str
必需参数位置参数
位置参数按顺序传入,不带名称。

How the ends of the stroke are rendered.

If set to auto, the value is inherited, defaulting to "butt".

join
auto str
必需参数位置参数
位置参数按顺序传入,不带名称。

How sharp turns are rendered.

If set to auto, the value is inherited, defaulting to "miter".

dash
none auto str array dictionary
必需参数位置参数
位置参数按顺序传入,不带名称。

The dash pattern to use. This can be:

If set to auto, the value is inherited, defaulting to none.

查看示例
#set line(length: 100%, stroke: 2pt)
#stack(
  spacing: 1em,
  line(stroke: (dash: "dashed")),
  line(stroke: (dash: (10pt, 5pt, "dot", 5pt))),
  line(stroke: (dash: (array: (10pt, 5pt, "dot", 5pt), phase: 10pt))),
)
Preview

miter-limit
auto float
必需参数位置参数
位置参数按顺序传入,不带名称。

Number at which protruding sharp bends are rendered with a bevel instead or a miter join. The higher the number, the sharper an angle can be before it is bevelled. Only applicable if join is "miter".

Specifically, the miter limit is the maximum ratio between the corner's protrusion length and the stroke's thickness.

If set to auto, the value is inherited, defaulting to 4.0.

查看示例
#let points = ((15pt, 0pt), (0pt, 30pt), (30pt, 30pt), (10pt, 20pt))
#set path(stroke: 6pt + blue)
#stack(
    dir: ltr,
    spacing: 1cm,
    path(stroke: (miter-limit: 1), ..points),
    path(stroke: (miter-limit: 4), ..points),
    path(stroke: (miter-limit: 5), ..points),
)
Preview