typst

How to treat string as content in Typst?


In normal text/content, Typst will automatically convert certain characters to their typographically correct counterparts (e.g. apostrophes, quotation marks). I would like to get the same behavior for text represented as strings (for example from reading in a csv/toml file). Is this possible?

This is what I tried, without success. The goal would be to automatically get typographically correct apostrophes from the character string, similar to the first line

// what I want
China's Flag

// what I have
#let s = "China's Flag"
#s
#[#s]

Output:

enter image description here


Solution

  • Typst performs certain glyph substitutions in markup mode that won't occur when you pass it a literal string. What you are asking is for Typst to eval your string using it's syntax parsing logic, instead of handling the string literally:

    #let complex = "
    - 1
    - 2
    #text(red)[Some typst syntax, too!]
    "
    = Prints as literal string characters
    #complex
    
    = Passes through Typst's interpreter
    #eval(complex, mode: "markup")
    

    enter image description here

    Keep in mind: if you have extremely long typst strings you want to eval, include may be a better choice than eval. It lets you write up entire .typ files and pastes the properly rendered content wherever you included it.