From 295eb9fc75ac6ee4f81bd7fbbbae53f74c09506d Mon Sep 17 00:00:00 2001 From: postaid Date: Tue, 12 Nov 2024 12:29:21 +0500 Subject: [PATCH] fix "text" macro process inside math context --- .../tests/parse.test.ts | 82 ++++++------------- .../grammars/latex.pegjs | 8 +- 2 files changed, 33 insertions(+), 57 deletions(-) diff --git a/packages/unified-latex-util-parse/tests/parse.test.ts b/packages/unified-latex-util-parse/tests/parse.test.ts index a345fa6..32f55c1 100644 --- a/packages/unified-latex-util-parse/tests/parse.test.ts +++ b/packages/unified-latex-util-parse/tests/parse.test.ts @@ -27,16 +27,6 @@ describe("unified-latex-util-parse", () => { return root.content; } - function textToStringNodes(text: string) { - return Array.from(text).map((s) => { - if (/\s/.test(s)) { - return AstBuilder.SP; - } else { - return AstBuilder.s(s); - } - }); - } - it("trims whitespace/parbreaks in math environments", () => { // Display math let targetAst = strToNodes("\\[\\]"); @@ -103,57 +93,37 @@ describe("unified-latex-util-parse", () => { ], }]); - ast = strToNodes("$x_{y_{\\text{hello there $p_q_r$}}}$"); + ast = strToNodes("$O_O\\text{T_T$U_U$}$"); expect(ast).toEqual([{ type: "inlinemath", - content:[ - AstBuilder.s('x'), + content: [ + AstBuilder.s("O"), AstBuilder.m("_", AstBuilder.args([ + AstBuilder.s("O"), + ]), { escapeToken: "" }), + AstBuilder.m("text", AstBuilder.args([ AstBuilder.arg([ - AstBuilder.s("y"), - AstBuilder.m("_", AstBuilder.args([ - AstBuilder.arg([ - AstBuilder.m("text", AstBuilder.args([ - AstBuilder.arg([ - ...textToStringNodes("hello there "), - { - type: "inlinemath", - content: [ - AstBuilder.s("p"), - AstBuilder.m("_", AstBuilder.args([ - AstBuilder.arg([ - AstBuilder.s("q"), - ], { - openMark: '{', - closeMark: '}', - }), - ]), { escapeToken: "" }), - AstBuilder.m("_", AstBuilder.args([ - AstBuilder.arg([ - AstBuilder.s("r"), - ], { - openMark: '{', - closeMark: '}', - }), - ]), { escapeToken: "" }), - ] - }, - ], { - openMark: '{', - closeMark: '}', - }) - ])), - ], { - openMark: '{', - closeMark: '}', - }) - ]), { escapeToken: "" }), + AstBuilder.s("T_T"), + { + type: "inlinemath", + content: [ + AstBuilder.s("U"), + AstBuilder.m("_", AstBuilder.args([ + AstBuilder.arg([ + AstBuilder.s("U"), + ], { + openMark: "{", + closeMark: "}", + }), + ]), { escapeToken: "" }), + ], + }, ], { - openMark: '{', - closeMark: '}', - }) - ]), { escapeToken: "" }), - ] + openMark: "{", + closeMark: "}", + }), + ])), + ], }]); }); diff --git a/packages/unified-latex-util-pegjs/grammars/latex.pegjs b/packages/unified-latex-util-pegjs/grammars/latex.pegjs index 0c33cb5..2b3f2cb 100644 --- a/packages/unified-latex-util-pegjs/grammars/latex.pegjs +++ b/packages/unified-latex-util-pegjs/grammars/latex.pegjs @@ -67,6 +67,7 @@ parbreak "parbreak" math_token "math token" = special_macro + / whitespace* x:text_macro whitespace* y:group whitespace* { return [x, y].flatMap((x) => x); } / macro / full_comment / whitespace* x:math_group whitespace* { return x; } @@ -290,6 +291,11 @@ macro "macro" return createNode("macro", { content: m }); } +text_macro "text macro" + = m:(escape n:"text" { return n; }) { + return createNode("macro", { content: m }); + } + group "group" = begin_group x:(!end_group c:token { return c; })* end_group { return createNode("group", { content: x.flatMap((x) => x) }); @@ -345,7 +351,7 @@ math_environment "math environment" }); } -// group that assumes you're in math mode. If you use "\text{}" this isn't a good idea.... +// group that assumes you're in math mode. math_group "math group" = begin_group x:(!end_group c:math_token { return c; })* end_group { return createNode("group", { content: x.flatMap((x) => x) });