How to pass an array of to #grid?

I have this bit of code for displaying multiple images in a figure :

#let img_dir = "../imgs/"

#let figure_two_images(path1, path2, caption) = figure(
  grid(
    columns: 2, gutter: 2mm,
    image(img_dir + path1),
    image(img_dir + path2),
  ),
  caption: caption
)

Would it be possible to extend this to an array with an arbitrary number of paths ? Equivalent to

#grid(
  columns: 2, gutter: 2mm,
  image(img_dir + path1),
  image(img_dir + path2),
  image(img_dir + path3),
  etc.
)

My try was the following:

#let paths = (
  "path/to/img/1.png",
  "path/to/img/2.png"
)
#grid(
  columns: 2,
  for path in paths {
    image(img_dir + path, width:30%)
  }
)

but the loop returns content, displaying it in a single column.

1 Like

Solution:

#let img_dir = "./"

#let figure_two_images(caption, ..pathes) = figure(
  grid(
    columns: 2,
    gutter: 2mm,
    ..pathes.pos().map(path => image(img_dir + path)),
  ),
  caption: caption,
)

#figure_two_images("Images", "1.png", "2.png", "3.png")

The key concept is using .. to define a function with arbitrary parameters and to spread an array.

For more details, check out my other post and the related Typst documentation page.

1 Like

You were nearly there!

#grid(
  columns: 2,
  ..for path in paths {
    (image(img_dir + path, width:30%),)
  }
)
3 Likes

So this was all that was missing… Thank you!

Thank you for the links, they are indeed what I failed to find!