Hi all,
I am trying to implement a function that adapts the page margins given the paper format provided by the user in the template. Initially, I use the following implementation:
#let paper-format = "a5"
#let adapt-margins(margins) = context {
let paper-width = page.width
let paper-height = page.height
let a4-width = 210mm
let a4-height = 297mm
let width = (paper-width - a4-width) / 2
let height = (paper-height - a4-height) / 2
(
left: width + margins.left,
right: width + margins.right,
top: height + margins.top,
bottom: height + margins.bottom,
)
}
In the template:
#let my-template(paper: "a4", args..., body) = context {
// It is an example
let my-margins = (left: 1cm, right: 1cm, top: 1cm, bottom: 1cm)
show: page.with(paper: paper)
let margins = adapt-margins(my-margins)
set page(margin: margins)
}
The previous function doesn’t work because margins is a context and not a dictionary. If I code the function directly in the template definition, that is:
#let my-template(paper: "a4", args..., body) = context {
// It is an example
let my-margins = (left: 1cm, right: 1cm, top: 1cm, bottom: 1cm)
show: page.with(paper: paper)
let paper-width = page.width
let paper-height = page.height
let a4-width = 210mm
let a4-height = 297mm
let width = (paper-width - a4-width) / 2
let height = (paper-height - a4-height) / 2
set page(
margin: (
left: width + margins.left,
right: width + margins.right,
top: height + margins.top,
bottom: height + margins.bottom,
)
)
}
it works, but an extra page is added, which is quite logical.
Is there a way to do this without redefining all the paper widths and heights (which is my current solution)?
Thank you