What are the rules for identifiers in Typst?

The scripting part of Typst allows users to define their own functions and variables. From my tests it seems that these identifiers are allowed to contain Unicode characters (including underscores and dashes).

Example:

#let ÿøúr-nãmê = "Typst"
Hello, #ÿøúr-nãmê!

Using a plain underscore character “_” for identifiers does not fully work, though. You can define a variable like #let _ = [Foo] but referencing it seems to cause errors.

What are the exact rules for identifiers in Typst? And what are best practices, e.g. for shared templates?

According to the lexer inside of Typst’s source code, an identifier is valid if it follows the Unicode standard uax31 and, additionally, Typst allows _ as a starting and continuing character and - as a continuing character.

As for how users should name their variables and functions, the standard seems to be kebab-case. Referencing a variable with just an underscore won’t work since that is probably reserved for explicitly nameless variables.

3 Likes