From a80ddb81b30cb8b546b0ec92b3209daad4f238e5 Mon Sep 17 00:00:00 2001 From: Erik Demaine Date: Tue, 1 Nov 2022 17:53:24 -0400 Subject: [PATCH] fix: space should prevent optional argument to \\ Fix `src/functions/cr.js`'s definition of `\\` to manually look for an optional argument via `future()` instead of `numOptionalArgs`, so that it does *not* skip over spaces. This matches the existing behavior of `\\` in `src/environments/array.js` and AMSMath's behavior of `\math@cr` via `\new@ifnextchar`. Fixes #3745 --- src/functions/cr.js | 6 +++--- test/katex-spec.js | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/functions/cr.js b/src/functions/cr.js index 3fc0acde70..bf25f26d57 100644 --- a/src/functions/cr.js +++ b/src/functions/cr.js @@ -13,13 +13,13 @@ defineFunction({ names: ["\\\\"], props: { numArgs: 0, - numOptionalArgs: 1, - argTypes: ["size"], + numOptionalArgs: 0, allowedInText: true, }, handler({parser}, args, optArgs) { - const size = optArgs[0]; + const size = parser.gullet.future().text === "[" ? + parser.parseSizeGroup(true) : null; const newLine = !parser.settings.displayMode || !parser.settings.useStrictBehavior( "newLineInDisplayMode", "In LaTeX, \\\\ or \\newline " + diff --git a/test/katex-spec.js b/test/katex-spec.js index 985bf64734..a2b088b323 100644 --- a/test/katex-spec.js +++ b/test/katex-spec.js @@ -1309,6 +1309,11 @@ describe("A begin/end parser", function() { expect`\begin{matrix}a&b\cr[c]&d\end{matrix}`.toParse(); }); + it("should not treat [ after space as optional argument to \\\\", function() { + expect`\begin{matrix}a&b\\ [c]&d\end{matrix}`.toParse(); + expect`a\\ [b]`.toParse(); + }); + it("should eat a final newline", function() { const m3 = getParsed`\begin{matrix}a&b\\ c&d \\ \end{matrix}`[0]; expect(m3.body).toHaveLength(2);