fraudTech commited on
Commit
044e522
·
1 Parent(s): 8351b7d

fixed \begin{cases} rendering in LaTeX output

Browse files
Files changed (1) hide show
  1. web/src/math-preprocessing.ts +10 -4
web/src/math-preprocessing.ts CHANGED
@@ -117,11 +117,17 @@ export function preprocessMathContent(content: string): string {
117
 
118
  // Step 0.5: Merge adjacent $$ blocks that are only separated by optional whitespace.
119
  // This occurs when the LLM writes $$expr =$$\begin{cases}...\end{cases} — Step 0 wraps
120
- // the \begin block, producing $$expr =$$$$\n\begin{cases}...\n$$. Those two consecutive
121
  // $$ blocks should be one combined display math expression.
122
- processed = processed.replace(/\$\$([\s\S]*?)\$\$[ \t]*\$\$\n?([\s\S]*?)\$\$/g, (_match, a, b) => {
123
- return `$$\n${a.trim()} ${b.trim()}\n$$`;
124
- });
 
 
 
 
 
 
125
 
126
  // Step 1.5: Re-run the lone-$-on-its-own-line fix AFTER delimiter normalization.
127
  // \(\ncontent\n\) becomes $\ncontent\n$ after Step 1, so we need another pass.
 
117
 
118
  // Step 0.5: Merge adjacent $$ blocks that are only separated by optional whitespace.
119
  // This occurs when the LLM writes $$expr =$$\begin{cases}...\end{cases} — Step 0 wraps
120
+ // the \begin block, producing $$expr =$$\n$$\n\begin{cases}...\n$$. Those two consecutive
121
  // $$ blocks should be one combined display math expression.
122
+ // NOTE: uses [ \t]*\n?[ \t]* (at most ONE newline with surrounding horizontal whitespace)
123
+ // because Step 0 inserts exactly \n before the second $$. Using \s* would incorrectly merge
124
+ // separate equations that happen to be on adjacent lines ($$a=b$$\n\n$$c=d$$).
125
+ // Run twice to collapse 3+ consecutive $$ blocks (e.g. $$a=$$\n$$b=$$\n$$c$$).
126
+ for (let _mergePass = 0; _mergePass < 2; _mergePass++) {
127
+ processed = processed.replace(/\$\$([\s\S]*?)\$\$[ \t]*\n?[ \t]*\$\$([\s\S]*?)\$\$/g, (_match, a, b) => {
128
+ return `$$\n${a.trim()} ${b.trim()}\n$$`;
129
+ });
130
+ }
131
 
132
  // Step 1.5: Re-run the lone-$-on-its-own-line fix AFTER delimiter normalization.
133
  // \(\ncontent\n\) becomes $\ncontent\n$ after Step 1, so we need another pass.