#How can I extract values from the `layout` function (as it returns Content type)?

6 messages · Page 1 of 1 (latest)

crimson rampart
#

I have a situation where I need to determine the height of the table rows (based on the data - the height of each row can vary based on the data). And the heights need to be extracted in an array for further processing. I have used the layout function and have the values but am unable to extract them into an array, as the layout function returns data of type content.

A naive approach would be to call another function to store these values but Typst pure functions means this is not so straightforward (at least to me). Any tips or guidance will be really helpful. 🙏

Here is the sample illustrative code (attached as a typ file). But below is the illustrative code that shows the problem.

// Page settings
#set page(
  paper: "a4",
  margin: (x: 45pt, top: 88.66pt, bottom: 78.66pt),
  flipped: true,
)
#set text(font: "Arial", size: 12pt)

#let func-A() = {
  let myheights = ()
  let another_f(arr, val) = {
    arr.push(val)
  }
  for i in range(4) {
    // Need to ignore this output
    let _ = another_f(myheights, i)
  }
  myheights
  // type(x)
}
= Typst Pure Function - This will not work
#func-A()
green kettle
#

you cannot access the values provided by layout outside it at all, because the values haven't been calculated yet after it returns

#

instead, you should make any function calls depending on layout inside it

#

everything you need to display content based on its information goes there, as it returns content to be displayed

#

(same goes for context {...})