fraudTech commited on
Commit
174de03
·
1 Parent(s): a3ca936

Fixed latex rendering for \begin{aligned} (and similar display mat

Browse files
Files changed (2) hide show
  1. web/src/App.tsx +17 -3
  2. web/src/math-preprocessing.ts +21 -0
web/src/App.tsx CHANGED
@@ -474,9 +474,15 @@ const mdOnlyComponents = {
474
  code({ className, children, ...props }: any) {
475
  const codeTheme = React.useContext(CodeThemeContext);
476
  const match = /language-(\w+)/.exec(className || '');
 
 
 
 
 
 
477
  return match ? (
478
  <SyntaxHighlighter style={codeTheme as any} language={match[1]} PreTag="div" {...props}>
479
- {String(children).replace(/\n$/, '')}
480
  </SyntaxHighlighter>
481
  ) : (
482
  <code className={className} {...props}>{children}</code>
@@ -1386,9 +1392,13 @@ function App() {
1386
  components={{
1387
  code({ className, children, ...props }: any) {
1388
  const match = /language-(\w+)/.exec(className || '');
 
 
 
 
1389
  return match ? (
1390
  <SyntaxHighlighter style={codeTheme as any} language={match[1]} PreTag="div" {...props}>
1391
- {String(children).replace(/\n$/, '')}
1392
  </SyntaxHighlighter>
1393
  ) : (
1394
  <code className={className} {...props}>{children}</code>
@@ -1474,9 +1484,13 @@ function App() {
1474
  components={{
1475
  code({ className, children, ...props }: any) {
1476
  const match = /language-(\w+)/.exec(className || '');
 
 
 
 
1477
  return match ? (
1478
  <SyntaxHighlighter style={codeTheme as any} language={match[1]} PreTag="div" {...props}>
1479
- {String(children).replace(/\n$/, '')}
1480
  </SyntaxHighlighter>
1481
  ) : (
1482
  <code className={className} {...props}>{children}</code>
 
474
  code({ className, children, ...props }: any) {
475
  const codeTheme = React.useContext(CodeThemeContext);
476
  const match = /language-(\w+)/.exec(className || '');
477
+ const rawContent = String(children).replace(/\n$/, '');
478
+ // If the code block contains a LaTeX display math environment, render as KaTeX.
479
+ // This handles cases where the LLM wraps \begin{aligned} etc. in code fences.
480
+ if (/\\begin\{(?:aligned|align\*?|equation\*?|gather\*?|multline\*?|array|matrix|pmatrix|bmatrix|vmatrix|Vmatrix|cases|split|eqnarray\*?)\}/.test(rawContent)) {
481
+ return <KatexDisplay math={rawContent} />;
482
+ }
483
  return match ? (
484
  <SyntaxHighlighter style={codeTheme as any} language={match[1]} PreTag="div" {...props}>
485
+ {rawContent}
486
  </SyntaxHighlighter>
487
  ) : (
488
  <code className={className} {...props}>{children}</code>
 
1392
  components={{
1393
  code({ className, children, ...props }: any) {
1394
  const match = /language-(\w+)/.exec(className || '');
1395
+ const rawContent = String(children).replace(/\n$/, '');
1396
+ if (/\\begin\{(?:aligned|align\*?|equation\*?|gather\*?|multline\*?|array|matrix|pmatrix|bmatrix|vmatrix|Vmatrix|cases|split|eqnarray\*?)\}/.test(rawContent)) {
1397
+ return <KatexDisplay math={rawContent} />;
1398
+ }
1399
  return match ? (
1400
  <SyntaxHighlighter style={codeTheme as any} language={match[1]} PreTag="div" {...props}>
1401
+ {rawContent}
1402
  </SyntaxHighlighter>
1403
  ) : (
1404
  <code className={className} {...props}>{children}</code>
 
1484
  components={{
1485
  code({ className, children, ...props }: any) {
1486
  const match = /language-(\w+)/.exec(className || '');
1487
+ const rawContent = String(children).replace(/\n$/, '');
1488
+ if (/\\begin\{(?:aligned|align\*?|equation\*?|gather\*?|multline\*?|array|matrix|pmatrix|bmatrix|vmatrix|Vmatrix|cases|split|eqnarray\*?)\}/.test(rawContent)) {
1489
+ return <KatexDisplay math={rawContent} />;
1490
+ }
1491
  return match ? (
1492
  <SyntaxHighlighter style={codeTheme as any} language={match[1]} PreTag="div" {...props}>
1493
+ {rawContent}
1494
  </SyntaxHighlighter>
1495
  ) : (
1496
  <code className={className} {...props}>{children}</code>
web/src/math-preprocessing.ts CHANGED
@@ -38,6 +38,27 @@ export function preprocessMathContent(content: string): string {
38
  (_match: string, inner: string) => `\n$$\n${inner.trim()}\n$$\n`
39
  );
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  // Step -1: Convert lone-$-on-its-own-line delimiters to $$ display math.
42
  // remark-math cannot parse multiline inline $...$. This pattern arises when the
43
  // AI writes inline math split across lines, or when \(...\) is later converted to $...$
 
38
  (_match: string, inner: string) => `\n$$\n${inner.trim()}\n$$\n`
39
  );
40
 
41
+ // Step -2b: Convert plain (no-language) code fences whose content is a LaTeX display
42
+ // math environment to $$ blocks. The LLM sometimes wraps \begin{aligned} / \begin{equation}
43
+ // etc. in bare ``` fences (no language tag), which remark-gfm renders as <code> — bypassing
44
+ // all math rendering. Only ``` fences with nothing (or only whitespace) after the opening
45
+ // backticks are matched here; language-tagged fences are left untouched so that legitimate
46
+ // code examples are preserved.
47
+ processed = processed.replace(
48
+ /^```[ \t]*\r?\n([\s\S]*?)^```[ \t]*$/gm,
49
+ (_match: string, inner: string) => {
50
+ const trimmed = inner.trim();
51
+ const hasMathEnv = DISPLAY_MATH_ENVS.some(env => {
52
+ const envName = env.replace(/\\\*/g, '*');
53
+ return trimmed.includes(`\\begin{${envName}}`);
54
+ });
55
+ if (hasMathEnv) {
56
+ return `\n$$\n${trimmed}\n$$\n`;
57
+ }
58
+ return _match;
59
+ }
60
+ );
61
+
62
  // Step -1: Convert lone-$-on-its-own-line delimiters to $$ display math.
63
  // remark-math cannot parse multiline inline $...$. This pattern arises when the
64
  // AI writes inline math split across lines, or when \(...\) is later converted to $...$