fix: matrix environment with zero or inconsistent columns (#3018)

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, and it 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]`).

Fixes #3017

Co-authored-by: ylemkimon <y@ylem.kim>
This commit is contained in:
Erik Demaine
2021-05-14 17:11:44 -04:00
committed by GitHub
parent 0e9acce9be
commit f779bac684
2 changed files with 10 additions and 1 deletions

View File

@@ -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 ? {

View File

@@ -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() {