How to center-align equations taking the label into consideration? Is this a bug in the Typst compiler?

Often the equation peeks into the label, even though on the left side is still place, because the center-alignment does not take the label into consideration, see e.g.

I have to manually fix it with a #h(-2em) such that it now looks like this:

How can I enable it without (with many equations) performance draining measuring? Is this even a bug in the Typst compiler?

1 Like

It looks like this is done intentionally? The code does calculate the full_number_width and will translate the the equation under certain conditions, but does not translate with the default setup.

equation.translate(Point::with_x(match (equation_align, number_align.x) {
    (Start, Start) => full_number_width,
    (End, End) => -full_number_width,
    _ => Abs::zero(),
}));
#set math.equation(numbering: "(1)")

= Center aligned (default)
$ #{ "A" * 22 } $
#set math.equation(number-align: left)
$ #{ "A" * 22 } $

= Left aligned
#show math.equation: set align(left)
#set math.equation(number-align: right)
$ #{ "A" * 22 } $
#set math.equation(number-align: left)
$ #{ "A" * 22 } $


I think the algorithm you proposed is also problematic, because the widths of numbers are not fixed.
In the following example, the current algorithm put the two equations aligned, but your algorithm would shift the second equation to the left.

      x = a        (9)
      x = b       (10)

As for your original example, I suggest you split the equation into multiple lines with \ (and &).

1 Like

Interesting. Hm, I guess then I’ll probably stay with my imperfect manual way of doing it. Maybe it would be best to let math.equation have the option “alignment correction” that is on default 0em and has the options auto or something of type length and makes the center alignment as if the line begins left of the length that is provided while auto gives the length with the actual label, but if I want to circumvent the problem you describe, but also not have it 0.

What do you think of this idea?

I haven’t formed a clear opinion yet… Maybe shifting a center-aligned equation only when it would collide with the number if not shifted? Or adding math.equation.{body,number} sub-elements?

If you want to discuss it seriously, you could create an issue or forward the problem to Discord | #Math | Typst.