I want to have a conditional watermark based on a command line parameter but am having trouble with the watermark variable being out of scope in the #set page call. Is there way to do this?
You can “factor out” the variable definition, because each block in Typst joins and returns each line in it. For example, #let x = { [a]; [b]; [c] } (where ; is equivalent to writing them in separate lines) will be the same as #let x = [abc]. With that in mind, we can write this:
#let watermark = if sys.inputs.watermark == "true" {
// 'rotate(...)' is returned by this block
rotate(24deg, text(185pt, fill: rgb("FF0000"))[*SAMPLE*])
} else {
// 'none' is returned by this block
none
}
#set page(
paper: "a4",
numbering: "1",
flipped: true,
margin: 1cm,
foreground: watermark
)