Skip to content

Commit

Permalink
fix "text" macro process inside math context
Browse files Browse the repository at this point in the history
  • Loading branch information
postaid committed Nov 12, 2024
1 parent 535eebf commit 295eb9f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 57 deletions.
82 changes: 26 additions & 56 deletions packages/unified-latex-util-parse/tests/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("\\[\\]");
Expand Down Expand Up @@ -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: "}",
}),
])),
],
}]);

});
Expand Down
8 changes: 7 additions & 1 deletion packages/unified-latex-util-pegjs/grammars/latex.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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) });
Expand Down Expand Up @@ -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) });
Expand Down

0 comments on commit 295eb9f

Please sign in to comment.