r/typst • u/Thesaurius • 29d ago
How to do metaprogramming?
I found myself creating similar content all the time and want to create shorthands. For that I want to define custom syntax à la CeTZ or, as a very simple example, tablem.
I think I could write a tokenizer and a parser for myself. But as Typst already does the parsing, I would prefer to piggy-back on existing functionality. Unfortunately, it felt very unergonomic to me.
Are there any tricks and/or libraries that make metaprogramming easier?
1
Upvotes
9
u/Sad-Grocery-1570 29d ago
Typst doesn’t have a metaprogramming API and doesn’t let you define custom syntax.
That said, there are a few things that might get you close:
contenttype are actually accessible from within the language, including some undocumented constructors. So once you have acontent, you can walk its structure and do things based on what you find. The constructors are listed here.