How to `include` a data structure?

I’m passing a glossary (for glossarium) to a template (via the parameter glossary). The glossary is basically an array of arrays. If I pass it verbatim as in the following example, it works fine:

glossary: (
  (
    key: "Vulnerability",
    description: "A Vulnerability is a flaw in a computer system that weakens the overall security of the system.",
  ),
  (
    key: "Patch",
    description: "A patch is data that is intended to be used to modify an existing software resource such as a program or a file, often to fix bugs and security vulnerabilities.",
  ),
)

But as glossaries may become large, I would like to put it in a separate file. If I try to include that file as follows, things don’t work any more (i.e. Typst doesn’t recognize the file content as an array of arrays):

glossary: include "myglossary.typ"

myglossary.typ contains:

 (
  (
    key: "Vulnerability",
    description: "A Vulnerability is a flaw in a computer system that weakens the overall security of the system.",
  ),
  (
    key: "Patch",
    description: "A patch is data that is intended to be used to modify an existing software resource such as a program or a file, often to fix bugs and security vulnerabilities.",
  ),
)

I assumed include would just replace the file content 1:1. But obviously it doesn’t . So, how can I achieve this?

Please follow the forum guidelines How to post in the Questions category and update your post.

The problem is that using include returns content, and also that your file currently contains content (not data).

The easiest fix is to store the data in a variable inside myglossary.typ and import it (not include):

// myglossary.typ
#let glossary = (
  (
    key: "Vulnerability",
    description: "A Vulnerability is a flaw in a computer system that weakens the overall security of the system.",
  ),
  // ...
)

and

// main.typ
#import "myglossary.typ": glossary
#myfunc(glossary: glossary)

If you really way to use “inline imports” for some reason, you can use eval to turn content back into data:

// myglossary.typ
#(  // Note the "#"!
  (
    key: "Vulnerability",
    description: "A Vulnerability is a flaw in a computer system that weakens the overall security of the system.",
  ),
  // ...
)

and

// main.typ
#myfunc(glossary: eval(
  (include "myglossary_inline.typ").text
))
2 Likes

Ah thank you very much, that helps!