diff --git a/src/environments/array.js b/src/environments/array.js index 6474b0b4..3feb589e 100644 --- a/src/environments/array.js +++ b/src/environments/array.js @@ -820,7 +820,8 @@ defineEnvironment({ const res: ParseNode<"array"> = parseArray(context.parser, payload, dCellStyle(context.envName)); // Populate cols with the correct number of column alignment specs. - res.cols = new Array(res.body[0].length).fill( + const numCols = Math.max(0, ...res.body.map((row) => row.length)); + res.cols = new Array(numCols).fill( {type: "align", align: colAlign} ); return delimiters ? { diff --git a/test/katex-spec.js b/test/katex-spec.js index 79127164..c926229b 100644 --- a/test/katex-spec.js +++ b/test/katex-spec.js @@ -1244,6 +1244,7 @@ describe("A begin/end parser", function() { it("should parse and build an empty environment", function() { expect`\begin{aligned}\end{aligned}`.toBuild(); + expect`\begin{matrix}\end{matrix}`.toBuild(); }); it("should parse an environment with hlines", function() { @@ -1310,6 +1311,13 @@ describe("A begin/end parser", function() { expect("\\begin{matrix*} a & -1 \\\\ -1 & d \\end{matrix*}").toBuild(); expect("\\begin{matrix*}[] a & -1 \\\\ -1 & d \\end{matrix*}").not.toParse(); }); + + it("should allow blank columns", () => { + const parsed = getParsed`\begin{matrix*}[r] a \\ -1 & d \end{matrix*}`; + expect(parsed[0].cols).toEqual( + [{type: 'align', align: 'r'}, + {type: 'align', align: 'r'}]); + }); }); describe("A sqrt parser", function() {