Typst 将数组展开为水平列

问题描述 投票:0回答:1

我整个晚上都在尝试用数组填充单行表,但到目前为止我还无法使其与 for 一起工作,任何人都可以给我一个提示,我认为这是一个非常简单的问题...

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)
)

所以我可以找到任何方法来解开 cols 变量并在这种情况下显示 5 个水平单元格....

有什么线索吗?

typst
1个回答
0
投票

您可以在数组上使用

*
运算符:
columns: (auto, ) * cols.len()

我还简化了您的代码以使用常见的

typst
约定:

#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
  )

© www.soinside.com 2019 - 2024. All rights reserved.