diff --git a/crates/typst/src/math/matrix.rs b/crates/typst/src/math/matrix.rs index 34e0611a6..95164e822 100644 --- a/crates/typst/src/math/matrix.rs +++ b/crates/typst/src/math/matrix.rs @@ -458,6 +458,12 @@ fn layout_mat_body( gap: Axes>, span: Span, ) -> SourceResult { + let ncols = rows.first().map_or(0, |row| row.len()); + let nrows = rows.len(); + if ncols == 0 || nrows == 0 { + return Ok(Frame::soft(Size::zero())); + } + let gap = gap.zip_map(ctx.regions.base(), Rel::relative_to); let half_gap = gap * 0.5; @@ -483,12 +489,6 @@ fn layout_mat_body( _ => (AugmentOffsets::default(), AugmentOffsets::default(), default_stroke), }; - let ncols = rows.first().map_or(0, |row| row.len()); - let nrows = rows.len(); - if ncols == 0 || nrows == 0 { - return Ok(Frame::soft(Size::zero())); - } - // Before the full matrix body can be laid out, the // individual cells must first be independently laid out // so we can ensure alignment across rows and columns.