Can I use variables in .bib?

I major in mathematics and I have to deal with bibliographies with math equations in their titles. For convenience I would like to use variables in bibliography. The following simple way doesn’t work:

// in testbib.bib
@article{Test2025,
  author = {Author},
  title = {$Area(x^2+y^2<1)=pi$},
  year = 2025, 
}

// in testtyp.typ

#let Area = math.op("Area")

@Test2025 $ Area(x^2+y^2<1)=pi$ 

#bibliography("testbib.bib")

And the output looks like

Although I can manually set show rules or edit the entries of .bib file, there are lots of bibliographies to deal with and the same problem occurs to other custom math operators.

Anyone has any suggestions about this? Thanks.

Hello @Ariel and welcome!
I moved your post to the Questions category :smiley:. Please edit your topic’s title to be a question, and add relevant tags.

Two things for bib variables:

  • You need to escape the delimiters, e.g., `, (or any other for that matter, as long as you change your detection in Typst
  • You can use eval

image

test.bib

@article{Test2025,
  author = {Author},
  title = {\$Area(x^2+y^2<1)=pi\$},
  year = 2025, 
}

test.typ

Notice I scope the show rule to avoid it causing issues elsewhere.

@Test2025
#let Area = "Area"
#{
    show regex(`\$(.*)\.text): it => {
      eval(it.text.slice(1, -1), mode: "math", scope: (
        Area: $Area$,
      ))
    }
    bibliography("test.bib")
}

This works for me. Thank you very much.

1 Like