Skip to content

Instantly share code, notes, and snippets.

@avonmoll
Last active May 19, 2024 15:38
Show Gist options
  • Save avonmoll/36ec6071b1e79de3c7bdc909f46a63ec to your computer and use it in GitHub Desktop.
Save avonmoll/36ec6071b1e79de3c7bdc909f46a63ec to your computer and use it in GitHub Desktop.
typst IEEE-style equation references

Problem: the default typst references for math.equation labels appears as "Equation 1". One may provide a supplement argument when referencing as in: <someEquation>[Eq.] which renders "Eq. 1" in the text. However, this is insufficient for meeting the IEEE specification for equation references which should be rendered "(1)".

Solution: I modified the thmref function provided by typst-theorems (see here) in order to provide the level of customization necessary.

#let ieeeEqfmt = (nums) => [(#numbering("1.1", ..nums))]
#let eqref(
label,
fmt: ieeeEqfmt,
//fmt: nums => [(#numbering("1.1", ..nums))],
style: x => x,
makelink: true,
) = {
locate(loc => {
let elements = query(label, loc)
let locationreps = elements.map(x => repr(x.location().position())).join(", ")
assert(elements.len() > 0, message: "label <" + str(label) + "> does not exist in the document: referenced at " + repr(loc.position()))
assert(elements.len() == 1, message: "label <" + str(label) + "> occurs multiple times in the document: found at " + locationreps)
let target = elements.first().location()
let number = counter(math.equation).at(target)
if makelink {
return [
#show link: style
#link(target, fmt(number))
]
}
return fmt(number)
})
}
Display the source blob
Display the rendered blob
Raw
#set math.equation(numbering: "(1)")
#set heading(numbering: "I")
#let reffmt = it => box(stroke: rgb("#ff0000") + 0.5pt, outset: 1pt)[#smallcaps[#it]]
#show ref: reffmt
#let linkfmt = it => [#underline(text(blue)[#it])]
#import "eqref.typ": *
#let eqref = eqref.with(style: reffmt) // set defaults
#let customEqfmt = (nums) => [#box[Eq. (#numbering("1.1", ..nums))]]
#let customEqref = eqref.with(fmt: customEqfmt, style: emph) // alternate options
//#show link: it => text(blue)[#it] // this also will apply to the eqrefs currently
= First Section <first>
The Hamilton-Jacobi-Isaacs hyberbolic partial differential equation:
#let xx = [#math.upright(math.bold("x"))]
#let uu = [#math.upright(math.bold("u"))]
#let vv = [#math.upright(math.bold("v"))]
$ min_(uu in U(xx)) max_(vv in V(xx)) {l(xx, uu, vv, t) + (diff V)/(diff t) + sum_(i=1)^(n) (diff V)/(diff x_i) f_i(xx, uu, vv, t)} = 0 $ <HJI>
In general, #eqref(<HJI>) is difficult to solve.
Also this user-customized equation reference style: #customEqref(<HJI>).
= Second Section
See @first for some background.
Testing a regular hyperlink: #[#show link: linkfmt; #link("https://avonmoll.github.io")].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment