How to render math-symbols in a csv file as math in a table?

I have a csv file where the lines have the following form:
$a_1$, 1,3,4,5, 123, 4358034, and when I try to render them in a table with the code

#let my-output = csv("output.csv")
#table(
    columns: (auto,)*7,
    ..my-output.flatten()
  ) 

the $a_1$ renders as a string, not content.

I tried using eval, but ..my-output.flatten().map(eval) results in an error.

You could destructure the array in the following way:

#let (first, ..rest) = my-output.flatten()
#table(
  columns: (auto,) * 7,
  eval(first), ..rest
)

This only evaluates the first $a_1$, but all lines in my csv have this format, not just the first line. But you gave me the idea to do the following:

#let my-output = csv("output.csv")
#let newlist = ()
#for (first, ..rest) in my-output {
  newlist.push(eval(first))
  newlist.push(rest)
}

#table(
    columns: (auto,)*7,
    ..newlist.flatten()
  ) 

This works, but I feel like it is badly written code. Can it be improved?

Well, I used the data you have provided.

Look at this one:

#let my-output = csv(bytes(
  ```csv
  $x = 1$,$y^2$,$z$, 1, 2, 3, a, b, c,*A*,*B*,*C*,_D_,_E_,_F_
  ```.text,
))

#table(
  columns: 3,
  ..my-output.flatten().map(eval.with(mode: "markup"))
)


Edit: Cleaned up code from @Andrew 's suggestions :pray:

2 Likes

columns are auto-sized by default, just use columns: 7. grid.columns

1 Like

Yes, thank you!

I didn’t think before that even though map(eval), did result in an error map(x => eval(x, mode: "markup") does not.

1 Like

Depends on your input.

#let file = ```csv
$x = 1$,$y^2$,$z$,1,2,3,a,b,c,*A*,*B*,*C*,_D_,_E_,_F_
```.text
#let my-output = csv(bytes(file))

#let markup = table(
  columns: 3,
  ..my-output.flatten().map(eval.with(mode: "markup"))
)

#let file = ```csv
x = 1,y^2,z,1,2,3,a,b,c,bold(A),bold(B),bold(C),upright(D),upright(E),upright(F)
```.text
#let my-output = csv(bytes(file))

#let math = table(
  columns: 3,
  ..my-output.flatten().map(eval.with(mode: "math"))
)

#let file = ```csv
$x = 1$,$y^2$,$z$,1,2,3,[a],[b],[c],[*A*],[*B*],[*C*],emph[D],emph[E],emph[F]
```.text
#let my-output = csv(bytes(file))

#let code = table(
  columns: 3,
  ..my-output.flatten().map(eval).map(x => [#x])
)

#table(
  columns: 3,
  align: center + horizon,
  table.header(..([Markup], [Math], [Code]).map(strong)),
  markup, math, code,
)

2 Likes

That is helpful for the understanding. Thanks!

1 Like