Advanced arguments

Spreading arguments from list

Spreading operator allows you to "unpack" the list of values into arguments of function:

#let func(a, b, c, d, e) = [#a #b #c #d #e]
#func(..(([hi],) * 5))
Rendered image

This may be super useful in tables:

#let a = ("hi", "b", "c")

#table(columns: 3,
  [test], [x], [hello],
  ..a
)
Rendered image

Key arguments

The same idea works with key arguments:

#let text-params = (fill: blue, size: 0.8em)

Some #text(..text-params)[text].
Rendered image

Managing arbitrary arguments

Typst allows taking as many arbitrary positional and key arguments as you want.

In that case function is given special arguments object that stores in it positional and named arguments.

Link to reference

#let f(..args) = [
  #args.pos()\
  #args.named()
]

#f(1, "a", width: 50%, block: false)
Rendered image

You can combine them with other arguments. Spreading operator will "eat" all remaining arguments:

#let format(title, ..authors) = {
  let by = authors
    .pos()
    .join(", ", last: " and ")

  [*#title* \ _Written by #by;_]
}

#format("ArtosFlow", "Jane", "Joe")
Rendered image

Optional argument

Currently the only way in Typst to create optional positional arguments is using arguments object:

TODO