How to display table.cell with condition?

Hi,

I have this kind of data I want to display them in a table :

Num  | Rev  | Comment
1    |  .1  | One comment
1    |  .1  | Second comment
...
1    |  .2  |  Another comment
2    |  .1  | comment
...

And I want to display them this way in a table (without repeating the same previous data):

1    |  .1  | One comment
     |      | Second comment
...
     |  .2  |  Another comment
2    | .1   |  comment
...

I try to use a if but, try a lot a way to use it… but it is not working…

#let data = ((num:"1", rev: ".1", comment: "One comment"),(num:"1", rev: ".2", comment: "second comment")...)
#let num = 0
#let rev = 0
#table(
  columns: (1fr, 1fr, 1fr),
  ..for elem in qms {
    table.cell(if num != elem.num {num = elem.num; num} else { }),
    table.cell(elem.rev),
    table.cell(elem.comment)
  }
)

Is it possible to display conditional content in cell with script directly in typst template ?

Your example almost works – you just need to add () to create the array in the loop:

#let data = (
  (num:"1", rev: ".1", comment: "One comment"),
  (num:"1", rev: ".2", comment: "second comment"),
  (num:"2", rev: ".1", comment: "Blah blah"),
)
#let num = 0
#let rev = 0
#table(
  columns: (1fr, 1fr, 1fr),
  ..for elem in data {
    (
      table.cell(if num != elem.num {num = elem.num; num} else { }),
      table.cell(elem.rev),
      table.cell(elem.comment),
    )
  }
)

image

  • Note: you can also leave off the table.cells, they are implied, and you could move the temporary variables inside a local block:
    #let data = (
      (num:"1", rev: ".1", comment: "One comment"),
      (num:"1", rev: ".2", comment: "second comment"),
      (num:"2", rev: ".1", comment: "Blah blah"),
    )
    #table(
      columns: (1fr, 1fr, 1fr),
      ..{
        let num = 0
        let rev = 0
        for elem in data {
          (
            if num != elem.num { num = elem.num; num } else { },
            elem.rev,
            elem.comment,
          )
        }
      }
    )
    
2 Likes

Thank you very much.
As I was using table with a lot of data without using …for… I forgot that I need this array in this case.