Skip to content

Commit c4e74fe

Browse files
meutexmaria-rcksElliotDreljuliusmarminge
authored
Typeset equations that fail only because of an unescaped percent (pingdotgg#29511)
Co-authored-by: meutex <[email protected]> Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Elliot Drel <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent e253b88 commit c4e74fe

3 files changed

Lines changed: 89 additions & 11 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import katex from "katex";
2+
import { describe, expect, it } from "vite-plus/test";
3+
4+
import { escapeLiteralPercents } from "./MathTypeset.logic";
5+
6+
describe("escapeLiteralPercents", () => {
7+
it.each([
8+
["\\text{3.18% yield}", "\\text{3.18\\% yield}"],
9+
["\\text{100%% yield}", "\\text{100\\%\\% yield}"],
10+
["6\\% + 2\\%", "6\\% + 2\\%"],
11+
["x\\\\%", "x\\\\\\%"],
12+
["x % note\n+ 1", "x % note\n+ 1"],
13+
["% note\n\\text{2%}", "% note\n\\text{2\\%}"],
14+
["x &= 1 % +9%\n\\text{2%}", "x &= 1 % +9%\n\\text{2\\%}"],
15+
["\\verb|50%|+\\text{2%}", "\\verb|50%|+\\text{2\\%}"],
16+
["\\verb*%a%b%+c%", "\\verb*%a%b\\%+c\\%"],
17+
["\\verb|50%+\\text{2%}", "\\verb|50%+\\text{2%}"],
18+
])("repairs %j", (tex, expected) => {
19+
expect(escapeLiteralPercents(tex)).toBe(expected);
20+
});
21+
22+
it("lets the reported multiline equation typeset", () => {
23+
const tex =
24+
"R_e=\\underbrace{\\frac{1.0176}{32}}_{\\text{3.18% dividend yield}}\n+\\underbrace{6\\%}_{\\text{growth}}\n=\\boxed{9.18\\%}";
25+
expect(() => katex.renderToString(tex, { throwOnError: true })).toThrow();
26+
expect(() =>
27+
katex.renderToString(escapeLiteralPercents(tex), { throwOnError: true }),
28+
).not.toThrow();
29+
});
30+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Escapes each `%` written right after other text, as in `\text{3.18% yield}`.
3+
* A `%` at a line start or after whitespace stays a comment. Skips escapes and
4+
* `\verb` like KaTeX's lexer, in one linear pass.
5+
*/
6+
export function escapeLiteralPercents(tex: string): string {
7+
let repaired = "";
8+
let copied = 0;
9+
let index = 0;
10+
while (index < tex.length) {
11+
const char = tex[index];
12+
if (char === "\\") {
13+
if (tex.startsWith("\\verb", index)) {
14+
const starred = tex[index + 5] === "*";
15+
const delimiter = tex[index + (starred ? 6 : 5)];
16+
if (delimiter !== undefined && (starred || !/[*a-zA-Z]/.test(delimiter))) {
17+
const bodyStart = index + (starred ? 7 : 6);
18+
let close = bodyStart;
19+
while (close < tex.length && ![delimiter, "\n", "\r"].includes(tex[close]!)) close++;
20+
// KaTeX rejects a \verb that does not close on its own line.
21+
if (tex[close] !== delimiter) return tex;
22+
index = close + 1;
23+
continue;
24+
}
25+
}
26+
// A control symbol such as `\%` or `\\` is two characters.
27+
index += 2;
28+
} else if (char === "%" && index > 0 && !/\s/.test(tex[index - 1]!)) {
29+
repaired += `${tex.slice(copied, index)}\\%`;
30+
copied = ++index;
31+
} else if (char === "%") {
32+
const newline = tex.indexOf("\n", index);
33+
index = newline === -1 ? tex.length : newline + 1;
34+
} else {
35+
index++;
36+
}
37+
}
38+
return copied === 0 ? tex : repaired + tex.slice(copied);
39+
}

‎apps/web/src/components/chat/MathTypeset.tsx‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import "katex/dist/katex.min.css";
33
import type { ReactNode } from "react";
44

55
import { LRUCache } from "../../lib/lruCache";
6+
import { escapeLiteralPercents } from "./MathTypeset.logic";
67

78
// Virtualized threads remount messages while scrolling; cached markup keeps
89
// that from typesetting the same formulas again. "" records a parse error.
@@ -12,20 +13,28 @@ function typeset(tex: string, display: boolean): string {
1213
const key = `${display ? "display" : "inline"}:${tex}`;
1314
const cached = typesetCache.get(key);
1415
if (cached !== null) return cached;
16+
const options = {
17+
displayMode: display,
18+
output: "htmlAndMathml" as const,
19+
throwOnError: true,
20+
// No \href, \url, \htmlClass, or other commands that emit active HTML.
21+
trust: false,
22+
strict: "ignore" as const,
23+
maxSize: 20,
24+
maxExpand: 1000,
25+
};
1526
let html = "";
1627
try {
17-
html = katex.renderToString(tex, {
18-
displayMode: display,
19-
output: "htmlAndMathml",
20-
throwOnError: true,
21-
// No \href, \url, \htmlClass, or other commands that emit active HTML.
22-
trust: false,
23-
strict: "ignore",
24-
maxSize: 20,
25-
maxExpand: 1000,
26-
});
28+
html = katex.renderToString(tex, options);
2729
} catch {
28-
// Unfinished or unsupported TeX stays readable as its source.
30+
const repaired = escapeLiteralPercents(tex);
31+
if (repaired !== tex) {
32+
try {
33+
html = katex.renderToString(repaired, options);
34+
} catch {
35+
// Unfinished or unsupported TeX stays readable as its source.
36+
}
37+
}
2938
}
3039
typesetCache.set(key, html, (key.length + html.length) * 2);
3140
return html;

0 commit comments

Comments
 (0)