fraudTech commited on
Commit
02151b4
·
1 Parent(s): 044e522

fix \begin{bmatrix}

Browse files
Files changed (1) hide show
  1. web/src/math-preprocessing.ts +12 -0
web/src/math-preprocessing.ts CHANGED
@@ -97,6 +97,18 @@ export function preprocessMathContent(content: string): string {
97
  }).join('');
98
  }
99
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  // Step 0: Wrap undelimited \begin{env}...\end{env} blocks in $$ delimiters.
101
  // Only wraps blocks that are NOT already inside existing $$ delimiters.
102
  // Runs AFTER Step 1 so that \[ \begin{aligned}...\end{aligned} \] is already $$-wrapped
 
97
  }).join('');
98
  }
99
 
100
+ // Step 0a: Promote inline $\begin{display-env}...\end{display-env}$ to $$...$$
101
+ // When the LLM wraps a display-only environment in single-$ delimiters, KaTeX renders
102
+ // it in inline mode and shows a red error (bmatrix, aligned, etc. require display mode).
103
+ // This step converts those to $$ display blocks BEFORE Step 0 handles bare environments.
104
+ {
105
+ const inlineDisplayPattern = new RegExp(
106
+ `(?<!\\$)\\$(\\\\begin\\{(${DISPLAY_MATH_ENVS.join('|')})\\}[\\s\\S]*?\\\\end\\{\\2\\})\\$(?!\\$)`,
107
+ 'g'
108
+ );
109
+ processed = processed.replace(inlineDisplayPattern, (_match, inner) => `$$\n${inner.trim()}\n$$`);
110
+ }
111
+
112
  // Step 0: Wrap undelimited \begin{env}...\end{env} blocks in $$ delimiters.
113
  // Only wraps blocks that are NOT already inside existing $$ delimiters.
114
  // Runs AFTER Step 1 so that \[ \begin{aligned}...\end{aligned} \] is already $$-wrapped