How to write Typst markup in bibliography?

It appears that Typst commands are ignored in Hayagriva content:

bibfile.yml:

breathing:
  type: article
  title: Breathing in O#sub[2] releases CO2
  author:
  - Smith, Joe
  - Smith, Jane
  date: 2025
  parent:
    type: periodical
    title: Journal of Lungs
    volume: 1

document.typ:

@breathing
#bibliography("bibfile.yml")

yields:

But show rules will still apply, so I can do:

#show "CO2": [CO#sub[2]]
@breathing
#bibliography("bibfile.yml")

which will yield:

The issue is that now I need to check every citation for new show rules that I should add to my Typst source. In practice, I’m going to miss some of them and end up with poorly formatted references. Including all show rules needed for all references in my (potentially large) bibliography seems hacky and requires syncing data in two different files, never a pleasant prospect.

Is there a way to record this formatting information within the Hayagriva (or BibTeX) file?

Hey @Mathieu, welcome to the forum! I’ve changed your post’s title to a question in order to better fit our guidelines, feel free to adjust it further if needed: How to post in the Questions category

You can try using $math mode content$ in your bibliography file as that is usually preserved by Hayagriva. Otherwise, you’ll need show rules. Per-citation show rules are not currently possible, although there are some ongoing community efforts to make this possible.

1 Like

You could use eval and show rules to achieve this. For example,

bibfile.yml

title: Breathing in myeval(O#sub[2]) releases CO2

document.typ

@breathing

#show bibliography: it => {
  let reg = regex("myeval\((.*)\)")
  show reg: r => {
    eval(r.text.replace(reg, m => {
      m.captures.first()
    }), mode: "markup")
  }
  it
}

#bibliography("bibfile.yml")

yields this:

1 Like

I have recently come across this issue and aarnent’s solution is the best that I have found yet, but it does have two easily fixable problems.

First, it fails when ‘myeval’ is upcased to ‘Myval’ by the title casing mechanisms.

Second, it fails when there is more than one markup string in the bibliography entry. The regex starts with the beginning of the first and does not stop until the end of the last, attempting to transform the whole of the captured text.

The correction to these is to use the line

  let reg = regex("Myeval\((.*?)\)")

(note the added ?, which makes the match non-greedy) and to use Myeval instead of myeval.

The inability to properly specify an article title like “Revenge for Honour: Date, Authorship and Sources" is a real issue in many fields. (Or a publisher field of sine nomine or …

A better solution, in my opinion, is to accept basic LaTeX formatting commands in bib(la)tex files and convert them on-the-fly for internal use. Like it or not, such formatting is embedded in bibliographies and other processing software.

Typst markup should be accepted directly in Hayagriva yaml files.

Following up on my observation above about upcasing, the problem goes both ways. Consider this entry:

Voltaire1759a:
  type: book
  title: Candide, ou l'Optimisme. Traduit de l'Allemand de {MMyeval(#super[R])}. le Docteur RALPH
  author: Voltaire
  date: 1759
  publisher:
    name: Myeval(#emph[s.n.]), likely John Nourse
    location: Myeval(#emph[s.l.]), likely London, England
  page-total: 237
  url:
    value: https://archive.org/details/bib_fict_4655354
    date: 2024-06-27
  note: Wade 2, Morize 59x. This volume has a paragraph (p.242) that is missing in other editions, and also has a spurious sequel as a "seconde partie"

Here, Myeval in the title has to be protected from case changing.

So, this is a fragile but workable solution that is only partially automatable and requires manual intervention to verify the correct behavior.

There was a similar thread earlier. Check out that one also.