Typst unwrap array as horizontal colums

196 views Asked by At

I've been all evening trying to populate a single row table with an array, but so far I haven't been able to make it work with a for, could anyone give me a tip, I suppose it is a very easy issue...

let info = (
        phone: "+34 6",
        email: "carle",
        homepage: "ev",
        github: "carl",
        linkedin: "carle",
        nationality: "es",
    )
let cols = ()

for (key, value) in info {
    if value != "" {
        // Adds hyperlinks
        if key == "email" {
            cols.push(text(value))
        } else if key == "phone" {
            cols.push(text(value))
        } else if key == "linkedin" {
            cols.push(text(value))
        } else if key == "github" {
            cols.push(text(value))
        } else if key == "gitlab" {
            cols.push(text(value))
        } else if key == "homepage" {
            cols.push(text(value))
        }
    }
}

table(
    columns: (for  in cols { (auto,) }),
    inset: 0pt,
    stroke: none,
    column-gutter: 1fr,
    align: horizon,
        // for  in cols {
        //     (cols.at(0))
        // }
        //ToDo: FixMe!
        cols.at(0), cols.at(1), cols.at(2), cols.at(3), cols.at(4)
        //range(cols).map(col => auto)
)

So I can find any way to unwrap the cols variable and show 5 horizontal cells in this case....

Any clue?

1

There are 1 answers

1
ntjess On BEST ANSWER

You can use the * operator on an array: columns: (auto, ) * cols.len()

I've also simplified your code to use common typst conventions:

#let info = (
  phone: "+34 6",
  email: "carle",
  homepage: "ev",
  github: "carl",
  linkedin: "carle",
  nationality: "es",
)
#let allowed-keys = ("email", "phone", "linkedin", "github", "gitlab", "homepage")

#let cols = allowed-keys.map(
  key => info.at(key, default: none)
).filter(value => value != none)

#table(
  columns: (auto, ) * cols.len(),
  inset: 0pt,
  stroke: none,
  column-gutter: 1fr,
  align: horizon,
      ..cols
  )

enter image description here