Search code examples
typst

In Typst, is it possible to check if this is the "main" file?


In a Typst project of multiple files, it can be easiest to only compile the file presently in focus.

While working on macro and template files, it would be nice to show some test/debug calls in the resulting document, but only if those files are being compiled directly, i.e. not #include'ed by other files.

This is similar to the python feature if __name__ == "__main__".


Solution

  • Looks like the intended solution is to use #import instead. The point of importing is specifically to access the functions and variables of a file without rendering the content. For example:

    // macros.typ
    #let diameter(r) = {2*r}
    DEBUG: Radius 2 is diameter #diameter(2)
    

    and then

    // main.typ
    #import "macros.typ": *
    This is the main text.