How to align baselines of display equations in tables?

So there is a “perfect” solution, in the sense of perfectly aligned based on the idea of if you can’t beat them join them. So join the equations with an alignment point $&$ into one big equation, but in each table cell, hide the other parts of the equation… The code is in the same project Typst in a different file (concat and hide.typ).

code
#import "@preview/mannot:0.3.0": markul

#{
  [ Concatenate a number of equations with ```typst $&$``` but show only\ the nth equation in each table cell. ]
  // Concatenate a number of equations with &, but show only the nth
  let concat-and-hide(..args, nth: none) = {
    math.equation(args.pos().enumerate().map(((i, elt)) => context {
      let sz = measure(elt)
      if i == nth {
        elt
      } else {
        std.hide(elt) + h(-sz.width)
      }
    }).join($&$), block: true)
  }

  let equationrow(..args) = {
    let eqs = args.pos()
    for (index, eq) in eqs.enumerate() {
      (concat-and-hide(..eqs, nth: index), )
    }
    ()
  }
  
  show table.cell: it => {
    show math.equation.where(block: true): set align(it.align) if it.align != auto
    it
  }
  show math.equation: block.with(stroke: 0.1pt + red)

  let markul = markul.with(outset: (bottom: 0.2pt, right: 20em), stroke: 0.2pt)
  table(
    columns: 4,
    ..equationrow(
      $ markul(x) $,
      $ x^2 $,
      $ integral d x $,
      $ sum_(q=integral_A^B) x^q $,
    )
  )
}
1 Like