diff --git a/crates/typst-layout/src/math/root.rs b/crates/typst-layout/src/math/root.rs index a6b5c03d0..c7f41488e 100644 --- a/crates/typst-layout/src/math/root.rs +++ b/crates/typst-layout/src/math/root.rs @@ -85,14 +85,15 @@ pub fn layout_root( ascent.set_max(shift_up + index.ascent()); } - let radicand_x = sqrt_offset + sqrt.width(); + let sqrt_x = sqrt_offset.max(Abs::zero()); + let radicand_x = sqrt_x + sqrt.width(); let radicand_y = ascent - radicand.ascent(); let width = radicand_x + radicand.width(); let size = Size::new(width, ascent + descent); // The extra "- thickness" comes from the fact that the sqrt is placed // in `push_frame` with respect to its top, not its baseline. - let sqrt_pos = Point::new(sqrt_offset, radicand_y - gap - thickness); + let sqrt_pos = Point::new(sqrt_x, radicand_y - gap - thickness); let line_pos = Point::new(radicand_x, radicand_y - gap - (thickness / 2.0)); let radicand_pos = Point::new(radicand_x, radicand_y); @@ -100,7 +101,8 @@ pub fn layout_root( frame.set_baseline(ascent); if let Some(index) = index { - let index_pos = Point::new(kern_before, ascent - index.ascent() - shift_up); + let index_x = -sqrt_offset.min(Abs::zero()) + kern_before; + let index_pos = Point::new(index_x, ascent - index.ascent() - shift_up); frame.push_frame(index_pos, index); } diff --git a/tests/ref/math-root-frame-size-index.png b/tests/ref/math-root-frame-size-index.png new file mode 100644 index 000000000..41d4df2e9 Binary files /dev/null and b/tests/ref/math-root-frame-size-index.png differ diff --git a/tests/suite/math/root.typ b/tests/suite/math/root.typ index df339fa80..b70b4b9bf 100644 --- a/tests/suite/math/root.typ +++ b/tests/suite/math/root.typ @@ -44,3 +44,9 @@ $ root(2, x) quad $ √2^3 = sqrt(2^3) $ $ √(x+y) quad ∛x quad ∜x $ $ (√2+3) = (sqrt(2)+3) $ + +--- math-root-frame-size-index --- +// Test size of final frame when there is an index. +$ a root(, 3) & a root(., 3) \ + a sqrt(3) & a root(2, 3) \ + a root(#h(-1em), 3) & a root(123, 3) $