How to load variable with links to a table?

I have the following typst table code:

#figure(
table(
  columns: (auto, 1fr, auto),
  align: left,
  [ID], [Title], [Book],
    [#link(<K-1>)[1]], [Title1], [Book1],
  [#link(<K-2>)[2]], [Title2], [Book2],
), caption: [Books]
)

When I move the two book entries to a variable and use …data the links are not working anymore.



#let data = ("#link(<K-1>)[1]]", "Title1", "Book1", "#link(<K-2>)[2]]", "Title2", "Book2")

#figure(
table(
  columns: (auto, 1fr, auto),
  align: left,
  [ID], [Title], [Book],
    ..data
), caption: [Books]
)

Is it possible to get the links working?

Hey @Kapati, welcome to the forum! I’ve moved your question to the dedicated Questions category. In addition, I’ve changed your question post’s title to better fit our guidelines: How to post in the Questions category

For future posts, make sure your title is a question you’d ask to a friend about Typst. :wink:

In the process of moving the data to a separate variable, it seems you accidentally changed the [markup blocks] into "strings". "strings" have no formatting, such as links or bold text, at all. Rather, you should remain using [markup blocks], which support all of Typst’s markup syntax:

#let data = ([#link(<K-1>)[1]]], [Title1], [Book1], [#link(<K-2>)[2]]], [Title2], [Book2])

#figure(
table(
  columns: (auto, 1fr, auto),
  align: left,
  [ID], [Title], [Book],
    ..data
), caption: [Books]
)

Now, your example is very small, and, in particular, you didn’t mention why you moved the data to a separate variable. This is a crucial bit of information. Therefore, I am going to infer that you might be loading this data from an external non-Typst file, such as a CSV file, which of course only contains strings since it’s not a Typst file. If that is the case, you can use eval(data, mode: markup) to ask Typst to interpret each string as if it were Typst markup code:

#let data = csv("file.csv").flatten() // flatten arrays of arrays into a single array of cells

#figure(
table(
  columns: (auto, 1fr, auto),
  align: left,
  [ID], [Title], [Book],
    ..data.map(cell => eval(cell, mode: markup))
    // NOTE: equivalent to
    // ..data.map(eval.with(mode: markup))
), caption: [Books]
)
2 Likes

Hi @Kapati, thanks for your question! If you feel the response you got has sufficiently answered your question, be sure to give it a checkmark :ballot_box_with_check:. This will help others find the solution in the future. If something is missing, please let us know what so we can resolve your issue. Thanks!