Reading rich text from external source?

Hello, I’m new to typst.

I’ve written a template that reads values from a CSV file, and displays them as cards (one card per CSV row). Now, I would really like for some of the columns to contain formatted text (ideally typst itself, but markdown or anything else will do). Is there a way to do it?

I tried the eval function, but it only accepts a string literal, not a variable. What I really would like is a function that accepts a string input, and produces content as output.

Thanks
Al

Welcome! Did you make sure eval’s mode was set correctly? I have no issues using it:

#let data = csv(bytes(```
_Grandma_,cookies,1995,"#text(fill: red)[9646616]"
*Auntie*,apple pies,1996,9646616
```.text))

#let card(entity, item, year, count) = block(
  stroke: (left: 1.5pt + blue),
  fill: aqua.lighten(50%),
  inset: 1em,
)[
  #year: #eval(entity, mode: "markup") made #eval(count, mode: "markup") #item
]

#data.map(it => card(..it)).join()
1 Like

ah, my bad indeed! The problem was that I was declaring a function using the function declaration syntax, and then passing it to eval’s scope:

let VP(pts) = ...

#eval(rules, mode: "markup", scope: (VP: VP))

But this breaks with

error: unexpected argument
    ┌─ templates/card.typ:153:54
    │
153 │               #text(fill: black-bar, size: 6pt)[#eval(rules, mode: "markup", scope: (VP: VP))]
    │                                                       ^^^^^

Instead, I have to declare the function as a lambda! Then it works!

Thanks for the minimal working example, and for helping me find the real culprit.

Al

Hi, I’m glad you found a solution that works for you, but declaring the function and passing it as a parameter to scope shouldn’t cause issues:

#let data = csv(bytes(```
_Grandma_,cookies,1995,"#text(fill: red)[9646616]"
*Auntie*,apple pies,1996,#greenify(9646616)
```.text))

#let greenify(it) = text(fill: green, str(it))

#let card(entity, item, year, count) = block(
  stroke: (left: 1.5pt + blue),
  fill: aqua.lighten(50%),
  inset: 1em,
)[
  #year: #eval(entity, mode: "markup") made #eval(count, mode: "markup", scope: (greenify: greenify)) #item
]

#data.map(it => card(..it)).join()

It’s hard to tell what exactly is going wrong, but it could be that you are passing too many arguments into the function, for example