Repository: KaTeX/KaTeX (js)

Issue:
Error when rendering empty matrix
**Describe the bug:**
KaTeX errors when asked to render an empty matrix.

**To Reproduce:**
Attempt to render
```
\begin{bmatrix}
\end{bmatrix}
```
We get the error
```
o.body[0] is undefined
```

**Environment (please complete the following information):**
 - KaTeX Version: 0.13.10
 - Device: Desktop
 - OS: Linux
 - Browser: Firefox
 - Version: 88.0.1

**Additional context:**
Demo link:

https://katex.org/?data=%7B%22displayMode%22%3Atrue%2C%22leqno%22%3Afalse%2C%22fleqn%22%3Afalse%2C%22throwOnError%22%3Atrue%2C%22errorColor%22%3A%22%23cc0000%22%2C%22strict%22%3A%22warn%22%2C%22output%22%3A%22htmlAndMathml%22%2C%22trust%22%3Afalse%2C%22macros%22%3A%7B%22%5C%5Cf%22%3A%22%231f(%232)%22%7D%2C%22code%22%3A%22%5C%5Cbegin%7Bbmatrix%7D%5Cn%5C%5Cend%7Bbmatrix%7D%22%7D


Pull request description:
fix: matrix environment with zero or inconsistent columns
Previously, the matrix family of environments assumed that the number of columns was given by the number of columns in the first row.
* This didn't work when there were zero rows (#3017)
* This didn't handle the case where future rows have more columns than the first (which is visible
when using an alignment other than `c`, such as `\begin{matrix*}[r]`).

This PR fixes both issues, and thereby fixes #3017.

Patch:
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 ? {


Test patch:
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() {