How to force displaystyle in both the numerator and denominator of all fractions?

I used show math.frac: math.display to set a fraction to the display style, but it doesn’t work on

$(sum_(i=1)^n m_i arrow(x)_i) / (sum_(i=1)^n m_i)$

I have to do this

$display(sum_(i=1)^n m_i arrow(x)_i) / display(sum_(i=1)^n m_i)$

to make the sum symbols in display style. But it’s too annoying to add display() when I write many fractions. Is there a more convenient way to achieve this?

You can automate wrapping the numerator and denominator in math.display(/**/) with a show rule:

#show math.frac: math.display
#show math.frac: it => {
  if it.has("label") and it.label == <already-processed> {return it}
  
  [#math.frac(
    math.display(it.num),
    math.display(it.denom)
  )<already-processed>]
}

The label is to stop it from recursing infinitely and hitting the show rule depth limit.

I believe it is visually identical:

2 Likes

I find a problem. The show rule will apply to the fractions at the upper and lower index positions like x^(1/2) and make the fraction too big. How can I avoid this while using this rule.

I think adding a state variable which controls whether the show rule is applied or not would be the way to do it. Then another show rule (or rules) would set the state to true or false.

I gave a crack at it but got lost in show rules and didn’t make any progress. I’ll leave it here in the off chance that it helps someone else, but know that it does not work.

Summary
#let tests = [
  $(sum_(i=1)^n m_i arrow(x)_i) / (sum_(i=1)^n m_i)$
  
  $(sum_(i=1)^n m_i arrow(x)_i^(a/b)) / (sum_(i=1)^n m_i)$
]

#show math.frac: math.display
#let use-display = state("use-display", true)

#show math.frac: it => {
  if it.has("label") and it.label == <already-processed> {return it}
  
  context if use-display.get() [#math.frac(
    math.display(it.num),
    math.display(it.denom)
  )<already-processed>] else [#it<already-processed>]
}

#show math.attach: it => {
  if it.has("label") and it.label == <already-processed> {return it}
  
  [#math.attach(
    it.base,
    t: {
      use-display.update(false)
      // set math.frac(style: "horizontal")
      it.t
      use-display.update(true)
    },
    b: it.b,
    tl: it.tl,
    bl: it.bl,
    tr: it.tr,
    br: it.br
  )<already-processed>]
}

#tests