How to make the equivalent of latex \pmod?

How can I make the equivalent of latex’s \pmod? Specifically, how can a function choose a spacing based on whether it’s in display mode?

I want to more or less do something like

#let pmod(m) = $#if is_display {quad} else {med} (mod med #m)$

Here is a solution based on state.

#let pmod-spacing = state("pmod-spacing", 2em/9)

#show math.equation.where(block: true): it => {
  pmod-spacing.update(1em)
  it
}
#show math.equation.where(block: false): it => {
  pmod-spacing.update(2em/9)
  it
}

#let pmod(m) = context h(pmod-spacing.get()) + $(mod med #m)$
Block:
$ n pmod(m) $

Inline:
$n pmod(m)$

Manual display does not works, however: $display(n pmod(m))$.

Disclaminer: I do not know if it’s the correct way to use context

1 Like