I want to create a function (in an extra typst file), which creates an table including first level headings using a loop. The following is what i have so far, but it only returns the titles of the heading and doesn’t put it into separate table cells. I also would like to omit some first level headings, for example the first one, which is the heading of the table of contents.
#let title_headings() = {
table(
row-gutter: 1cm,
columns: (4fr, 1fr),
fill: (x, y) =>
if x == 1 {
color.rgb("#aa0000")
},
align: center + horizon,
context{
for element in query(heading.where(level: 1)) {
table.cell(text(size: 14pt, element.body))
table.cell(text(size: 26pt, fill: white, "1"))
}
}
)
}
This function is included in a grid a template file like this. The template is then used in the main document
A context expression is a single bit of content, which cannot be inspected anymore, the table gets this single bit of content and treats it as a single cell (See Why is the value I receive from context always content?). This means that if you need more than one cell out of the context expression you need to move your context further up, I’ve done that here by lifting it outside the table. Additionally looping over cells and just placing them next to each other will also just create a single long bit of content. The solution is to create an array of individual cells, which are then all applied as individual cells to table by the use of the spread operator ... Because the inner loop returned not one, but two cells, we flatten the inner arrays, otherwise we’d spread pairs of cells into the table, not cells itself.
I think you have used “anymore” here in such a way that makes it confusing. context is always opaque and never can be inspected. So “anymore” here refers to “inside context” and “outside context”: you can inspect whatever is written in the context clause/block, but when you go outside (hehe), the context’s content can’t be inspected anymore (it becomes opaque).
Hi @flixz02, thanks for your question! Could you maybe try to revise your post’s title so others can find it easier in the future? The question guidelines recommend a complete question:
Good titles are questions you would ask your friend about Typst.
I thought about editing the title myself, but I didn’t want to miss your intent – after all the problem revolved around context in addition to tables. I would also encourage you to add these labels (context, tables) while you’re doing so.