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.
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.
Although not literally the original question, I stumbled on this thread and figured that in my case, main.typ has a title set, but the subdocuments do not, so that I can can use this condition to give the sub-documents the treatment they need:
#let sub-document(it,) = {
context(if document.title == none {
set page(
height: auto,
width: auto,
margin: 0pt,
)
// settings for sub-document
it
} else {
it
})
}
...
#show: sub-document