How to interpret Hayagriva data as content instead of strings?

Typst reads bibliography data - like the title - from a Hayagriva file. Is there any way I could make Typst interpret that data as content so that I could, for example, use smallcaps in the title read from the file?

I’d like to be able to do something like this:

wold:
  type: article
  author:
    - Doe, J.
  title: The Title with #smallcaps[smallcaps]

Is this possible now or even remotely possible at some point?

For the code block, you should use yml or yaml code identifier to get appropriate syntax highlighting.


You can use math mode inside some fields:

#let bib = ```yaml
wold:
  type: article
  author:
    - Doe, J.
  title: The Title with $x^2$
```.text

#bibliography(bytes(bib), full: true)

image

But there is no way to use code mode. You can try changing CSL file by applying same styling for specific part of entry, or create an issue about it.

Note that your example bibliography astually has a yaml comment instead of the text #smallcaps[smallcaps] in it. Your bibliography needs to at least read

wold:
  type: article
  author:
    - Doe, J.
  title: "The Title with #smallcaps[smallcaps]"

Now the question is: can we make Typst interpret any Typst markup in the title? The answer is: basically yes

#let bib = ```yaml
wold:
  type: article
  author:
    - Doe, J.
  title: "The Title with #smallcaps[smallcaps]"
```.text

#show bibliography: it => {
  show text: it => {
    // we must not attempt to evaluate text that is not valid markup
    // thankfully most text _is_, but unbalanced closing brackets don't work
    if it.text == "]" { return it }

    // evaluate any formatting
    let result = eval(it.text, mode: "markup")
    // if nothing was formatted, return the original content
    // to avoid infinite recursion
    if result.func() == text { return it }

    // emit the formatted content
    result
  }
  it
}

#bibliography(bytes(bib), full: true)

The show rule here uses eval() to format any text in the bibliography. This may result in false positives! It’s now necessary for you to ensure any bibliography content is valid Typst markup!

Since formatted text is also text, the show rule here needs to guard against recursion. I’m doing this by checking whether the formatting result is anything other than a simple text.

Hope this helps! :slight_smile:

2 Likes

There is:

#let bib = ```yaml
wold:
  type: article
  author:
    - Doe, J.
  title: "The Title with $#smallcaps(text(font: \"Libertinus Serif\")[SmallCaps])$"
```.text

#bibliography(bytes(bib), full: true)

"The Title with SmallCaps" with correct formatting

It can probably be improved, but that’s a start.

1 Like

Okay, that’s just cheating, you know what I meant. Besides, I’m not sure which stuff will behave differently when called from within math mode. Like the fact that you have to wrap text into function to get non-math font. At least that.

It’s not “cheating”, your statement read as it being impossible to use anything from code mode in the current state, so I pointed out that it is actually possible. But I’m not claiming that you deliberately omitted this information, as you could simply not have known, which is OK. What matters is that what I provided is one possible solution - there are certainly others!

It’s not perfect, but it’s doing what they asked for, so :person_shrugging:

1 Like

Great, thanks for all the solutions, these definitely help! It would be cool to natively process bibliography entries with Typst but as always, I guess it is not as trivial as I might think.