Tuesday, April 29, 2025

equation-reference-in-Typst

equation-reference-in-Typst

📌 Typst Trick: Label and Reference a Specific Numbered Equation

When writing documents with Typst, you may encounter the following issues:

  • All equations are numbered by default.

  • The reference format isn’t what you expect — for example, you get Equation 1, but you want (1).

Here’s how to fix that.


❌ The Problem

This problem occurs if you use the following naive approach to equation numbering:

#set math.equation(numbering: "(1)")

Consider @eq1  
$ a^2 + b^2 = c^2 $ <eq1>  
and  
$ E = mc^2 $

You’ll get the following result:

Typst equation reference

As you can see:

  • Both equations are numbered.

  • The reference appears as Equation 1, not (1).


✅ The Solution

To number only one equation and reference it as (1), define a small helper function eqnum that locally sets the numbering style. Then, override how equation references are displayed using #show ref.

#let eqnum(equation) = [
  #set math.equation(numbering: "(1)")
  #equation
]

#show ref: it => {
  let eq = math.equation
  let el = it.element
  if el != none and el.func() == eq {
    // Override equation references.
    link(el.location(), numbering(
      el.numbering,
      ..counter(eq).at(el.location())
    ))
  } else {
    // Other references as usual.
    it
  }
}

Consider @eq1  
#eqnum[$ a^2 + b^2 = c^2 $ <eq1>]  
and  
$ E = mc^2 $

As a result:

  • The first equation is labeled and referenced as (1).

  • The second equation appears without a number.

Here’s the expected output:

Typst equation reference

✨ Summary

This trick lets you precisely control which equations are numbered and how they’re referenced in Typst, without changing the global equation settings.

Popular Posts