fix: Correctly parse \ followed by whitespace (#2877)

* fix: Correctly parse \ followed by whitespace

LaTeX parses `\` followed by whitespace including up to one newline
as equivalent to `\ `.  (With multiple newlines, you get paragraph
breaks.)

Fix #2860.

* Improve comments

* Avoid second RegExp match in control words

* Document capturing groups

Co-authored-by: Ron Kok <ronkok@comcast.net>
This commit is contained in:
Erik Demaine
2021-05-05 21:54:41 -04:00
committed by GitHub
parent e3b54c73ea
commit c85250d14e
2 changed files with 22 additions and 15 deletions

View File

@@ -678,7 +678,7 @@ describe("A text parser", function() {
const noBraceTextExpression = r`\text x`;
const nestedTextExpression =
r`\text{a {b} \blue{c} \textcolor{#fff}{x} \llap{x}}`;
const spaceTextExpression = r`\text{ a \ }`;
const spaceTextExpression = r`\text{ a \ }`;
const leadingSpaceTextExpression = r`\text {moo}`;
const badTextExpression = r`\text{a b%}`;
const badFunctionExpression = r`\text{\sqrt{x}}`;
@@ -722,12 +722,17 @@ describe("A text parser", function() {
const parse = getParsed(spaceTextExpression)[0];
const group = parse.body;
expect(group.length).toEqual(4);
expect(group[0].type).toEqual("spacing");
expect(group[1].type).toEqual("textord");
expect(group[2].type).toEqual("spacing");
expect(group[3].type).toEqual("spacing");
});
it("should handle backslash followed by newline", () => {
expect("\\text{\\ \t\r \n \t\r }").toParseLike("\\text{\\ }");
});
it("should accept math mode tokens after its argument", function() {
expect(mathTokenAfterText).toParse();
});