How to change the margin depending on the page number being even or odd?

Hello
For a project where i create a student song book, i need to apply different type of margin based on the fact that the page number is odd or even.
At the moment i manually set the margin by passing parameters (here a dictionary) by value.
Due to the constraint on the page number i’m limited to use context function like this :

// in utils.typ
#let set_margin_type() = context {
  let page_num = here().position().at("page")//counter(page).get().first()
  let margin = value_odd_even(page_num, constant.odd_margin , constant.even_margin)
}

is there a way to correctly do it such that i can do something like this :

// in format.typ
#let formatable_song(
  margin_type,
  title,
  body_list,
  body_list_format,
  header_tune : none,
  header_tune_author : none,
  header_lyrics : none,
  header_lyrics_pseudo : none,
  header_comments : none,
) = { 
  set page(margin : MARGIN_DICT, footer: utils.get_footer_alignemnt())
}
  

For better context here’s my project, the file utils.typ, format.typ and main.typ are probably the most usefull:
https://typst.app/project/rxJdkxQZb6FrIuv59NVJxS

Thank you in advance for your help

You can’t have arbitrary margins per page since Typst’s layout currently requires that all continuous containers have the same size (continuous: not divided by a page break) but if you just need to switch back-and-forth to account for binding, there’s a tool specifically for that: margin dicts can contain inside and outside keys that automatically flip each page.

I think that would be sufficient for you; if not let me know what’s missing :)

2 Likes