How to check if current file is main?

Suppose I have a project, containing 2 files: main.typ and chapter1.typ. Main file main.typ includes chapter via #include "chapter1.typ". However, I want to be able to compile chapter1.typ separately with different set-rules. So, I need something like

# set ... if // is_main() ???

The question is how to implement such is_main function or something like that. I wasn’t able to find a simple way to achieve this, is there any?

Hi @legokol .
You may use a solution based on values avalaible in memory as string and passed to the compiler as a key=value formatted data by the --input option.

For example:

// this is a no_main.typ file
#{
  let is_main = sys.inputs.at( "main", default: "true")
  if is_main == "false" {
    [no main file]
  } else {
    [yes this is a main file]
  }
}

To obtain a no main typesetting, compile it with the command:

> typst compile --input main=false no_main.typ

Otherwise compile with the command:

> typst compile no_main.typ

At this point you need only to substitute the code in the if istruction to set up your document as desired.

If you desire a preview based on the no main configuration switch the if true/false cases.
R.

Hello. This is currently not directly possible without some hackery: Entry point (main file) detection within Typst document for nested projects · Issue #4502 · typst/typst · GitHub.

1 Like

Hey @legokol, welcome to the forum! I’ve changed your question post’s title to better fit our guidelines: How to post in the Questions category
For future posts, make sure your title is a question you’d ask to a friend about Typst. :wink:

1 Like