typst / typst

A new markup-based typesetting system that is powerful and easy to learn.
https://typst.app
Apache License 2.0
32.29k stars 864 forks source link

Setting page size misbehaves in if-else statements #2354

Closed lumpiluk closed 10 months ago

lumpiluk commented 10 months ago

Description

Trying to set the page size conditionally based on a template parameter (here simply tested with true) does not work with if-else statements. The following code is ignored and A4 pages are used instead:

#if true [
  #set page(width: 594mm, height: 167mm)
] else [
  #set page(paper: "presentation-16-9")
]

Using either of the set commands on their own works as expected.

Workaround:

#let page-params = if true {(width: 594mm, height: 167mm)} else {(paper: "presentation-16-9")}
#set page(
  ..page-params
) 

Reproduction URL

https://typst.app/project/r7y4QAHQO0oqSgoeI1pHCV

Operating system

Web app, Linux

Typst version

laurmaedje commented 10 months ago

Set is scoped to the end of the containing block, in this case the surrounding [. You can write #set page(width: 594mm, height: 167mm) if true though to conditionally set. Alternatively, you can do more arbitrary things with a show rule:

#show: it => {
  if true {
    set page(width: 594mm, height: 167mm)
    it
  } else {
    set page(paper: "presentation-16-9")
    it
  }
}

Everything here will be bound to `it`.

The latter is obviously not particular comfortable to write, which is the reason why set .. if .. exists. But that one is quite limited, so this is all not yet ideal. That said, it is working as expected.

lumpiluk commented 10 months ago

Ah, sorry, that makes sense. I completely forgot about the scoping. Thank you!