Can I access the dimensions of a page?

Hi Everyone,

I have made the bookletic package that splits the page into two smaller ones for ordering as a booklet. And right now I have the overall page size hardcoded so that I can divide it and set the size for the smaller pages but am trying to find a way to use the built in page sizes. Is there a way to ask the user of a function for a page size and use that within a function to get the actual dimensions of a type of paper?

2 Likes

Hello!
You can access page size using the layout function.
From the official documentation:

#layout(size => {
  let half = 50% * size.width
  [Half a page is #half wide.]
})
2 Likes

To supplement the previous answer: While layout generally gives the dimensions of the currently surrounding container, page.width and page.height give the page height/width specifically. They are only available within context.

#set page(width: 500pt)
#context page.width

However, page.width will always have the value of width, not of height when flipped: true. Moreover, as of Typst 0.11.1 the retrieval does not work when using an explicit #page(width: ..)[..]. This will be fixed in Typst 0.12.

If it’s for a smaller piece of layout, the layout approach would be more robust in this regard. However, it comes at the cost of all the content within it being wrapped in a block-level container, so you can’t e.g. have pagebreaks within it and it can negatively affect layout of things like footnotes.

2 Likes

I have tried the context approach but have not been able to assign the value to a variable for use throughout the rest of the code. When I assign it like this:
let variable = context page.width

The variable ends up having the context function itself assigned rather than the actual value. Am I missing something or is this a bug?

See here:

1 Like

Thank you! That works! This is the structure that ended up working for others who might need it:

#let sig (/*arguments*/) => {
//Initialize stuff
  context {
    let signature-paper-height = page.height
    //Rest of package that uses value here
  }
}
1 Like