How to redefine the default frac behavior while avoiding circular references

I want to add spaces before and after the numerator and denominator to make the fraction bar longer. Below is the script I tried writing, but apparently there’s an error because of a circular reference. How to overwrite the defition of frac?

Script

#let AddSpacefrac(num, den) = math.frac(
[#h(1em /6) #num #h(1em /6)],
[#h(1em /6) #den #h(1em /6)],
)

#show math.frac: frac => {
AddSpacefrac(frac.num, frac.denom)
}

Error message

maximum show rule depth exceeded

Hint: check whether the show rule matches its own output```

One solution is a workaround for math.equation suggested by @frozolotl in this issue: https://github.com/typst/typst/discussions/2242

The modified show rule is

#show math.frac: it => {
  if it.has("label") and it.label == <stop-frac-recursion> {
    it
  } else {
    [#AddSpacefrac(it.num, it.denom) <stop-frac-recursion> ]
  }
}

Not sure if there is a better solution than this workaround.

1 Like

It worked! Thank you so much!!!