How to expose layout information as metadata?

Hi,
I am trying to expose the width of the current layout as metadata using the following code:

#layout(size => {size.width.mm()})
#metadata(layout(size => {size.width.mm()}))<layout>

The first line prints out 160 as expected, but when I run typst query test.typ "<layout>" to query the metadata from the CLI, I get

[{
  "func":"metadata",
  "value":{"func":"layout","func":"(..) => .."},
  "label":"<layout>"
}]

So the metadata value seems to be some kind of layout function object. I tried to call it using (), but then typst complained that it is content and can not be called. What am I doing wrong? How can that width value 160 be made the metadata value?

this is similar to how context produces opaque content blocks:

in this case, what you wanna do is produce the labeled metadata from within the layout callback, which is where you actually have access to the value:

#layout(size => [#metadata(size.width.mm())<layout>])
1 Like

Aaaah okay. So layout behaves just like context in the way that everything that needs its information must happen inside of it. Alright, this solves my problem. Thank you very much :)

1 Like