@devries-cameron20 / t3code Mirror · read-only / commits / c4e74fe767
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 18:30 UTC

Typeset equations that fail only because of an unescaped percent (#29511)

Co-authored-by: meutex <342316034+meutex@users.noreply.github.com> Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Elliot Drel <elliotdrel@gmail.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Meutex committed Nov 13, 2026 at 04:35 UTC c4e74fe7674f2f0e375f0d42d6125db407aa43f0
3 files changed +89 -11
apps/web/src/components/chat/MathTypeset.logic.test.ts new
+30
@@ -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 +});
apps/web/src/components/chat/MathTypeset.logic.ts new
+39
@@ -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
+20 -11
@@ -3,6 +3,7 @@ import "katex/dist/katex.min.css";
3 import type { ReactNode } from "react";
4
5 import { LRUCache } from "../../lib/lruCache";
6 +import { escapeLiteralPercents } from "./MathTypeset.logic";
7
8 // Virtualized threads remount messages while scrolling; cached markup keeps
9 // that from typesetting the same formulas again. "" records a parse error.
@@ -12,20 +13,28 @@ function typeset(tex: string, display: boolean): string {
13 const key = `${display ? "display" : "inline"}:${tex}`;
14 const cached = typesetCache.get(key);
15 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 + };
26 let html = "";
27 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);
29 } 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 + }
38 }
39 typesetCache.set(key, html, (key.length + html.length) * 2);
40 return html;