Hello!
Please make sure your code is reproducible before posting, it helps to reduce the code to a minimal working example. You can take a look at How to post in the Questions category for other tips!
For your problem, I wouldn’t actually have a “global variable” and add the total price of each line when constructing the rows. You can do it twice at nearly no cost, this would be more “elegant” than having a global variable.
To actually answer your question, you can create a state
to keep track of the total price.
Here are the relevant lines below. I showcase two ways of computing the total price: sumtotalprice
or totalprice-state.get()
.
#let totalprice-state = state("totalprice", 0.0)
#let totalprice(line) = {
if line.type == lineTypes.special {
return line.details.amount * line.details.pricePerUnit
}
return 0
}
#let sumtotalprice(lines) = {
if type(lines) == "array" {
return lines.map(line => totalprice(line)).sum()
}
}
// ...
str(totalprice(line)) + totalprice-state.update(it => it + totalprice(line))
// ...
[], [], [], "Total:", text(size: 15pt)[#sumtotalprice(lines) vs #context totalprice-state.get()]