Spaces:
Sleeping
Sleeping
fix latex rendering in prompt
Browse files- web/src/App.tsx +23 -10
web/src/App.tsx
CHANGED
|
@@ -367,16 +367,6 @@ const normalizeUserLatex = (content: string): string => {
|
|
| 367 |
normalized = normalized.replace(/\\\(([\s\S]*?)\\\)/g, (_m, expr) => `$${expr.trim()}$`);
|
| 368 |
normalized = normalized.replace(/\\\[([\s\S]*?)\\\]/g, (_m, expr) => `$$${expr.trim()}$$`);
|
| 369 |
|
| 370 |
-
// If delimiters already exist, do not inject additional $ wrappers.
|
| 371 |
-
// Only perform a minimal repair for unmatched $$ blocks.
|
| 372 |
-
if (normalized.includes('$')) {
|
| 373 |
-
const displayDelimiters = normalized.match(/\$\$/g);
|
| 374 |
-
if (displayDelimiters && displayDelimiters.length % 2 === 1) {
|
| 375 |
-
normalized = `${normalized}\n$$`;
|
| 376 |
-
}
|
| 377 |
-
return normalized;
|
| 378 |
-
}
|
| 379 |
-
|
| 380 |
// Wrap common block environments if user forgot $$ delimiters.
|
| 381 |
normalized = normalized.replace(
|
| 382 |
/\\begin\{(aligned|align\*?|equation\*?|gather\*?|multline\*?|array|matrix|pmatrix|bmatrix|vmatrix|Vmatrix|cases|split|eqnarray\*?)\}[\s\S]*?\\end\{\1\}/g,
|
|
@@ -389,6 +379,29 @@ const normalizeUserLatex = (content: string): string => {
|
|
| 389 |
(_m, expr) => `$${expr.trim()}$`
|
| 390 |
);
|
| 391 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
// Wrap common inline commands if user forgot $ delimiters.
|
| 393 |
normalized = normalized.replace(
|
| 394 |
/\\(?:frac|sqrt|sum|int|prod|lim|alpha|beta|gamma|delta|theta|lambda|pi|sigma|mu|partial|cdot|times|leq|geq|neq|infty|forall|exists|sin|cos|tan|log|ln|mathbb|mathbf|mathcal|mathrm|to)(?:\s*\{[^{}]*\}){0,3}(?:\s*\^[^\s{}]+)?(?:\s*_[^\s{}]+)?/g,
|
|
|
|
| 367 |
normalized = normalized.replace(/\\\(([\s\S]*?)\\\)/g, (_m, expr) => `$${expr.trim()}$`);
|
| 368 |
normalized = normalized.replace(/\\\[([\s\S]*?)\\\]/g, (_m, expr) => `$$${expr.trim()}$$`);
|
| 369 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
// Wrap common block environments if user forgot $$ delimiters.
|
| 371 |
normalized = normalized.replace(
|
| 372 |
/\\begin\{(aligned|align\*?|equation\*?|gather\*?|multline\*?|array|matrix|pmatrix|bmatrix|vmatrix|Vmatrix|cases|split|eqnarray\*?)\}[\s\S]*?\\end\{\1\}/g,
|
|
|
|
| 379 |
(_m, expr) => `$${expr.trim()}$`
|
| 380 |
);
|
| 381 |
|
| 382 |
+
// If any rule above introduced delimiters, stop here to avoid double-wrapping
|
| 383 |
+
// in subsequent regex passes.
|
| 384 |
+
if (normalized.includes('$')) {
|
| 385 |
+
// Repair malformed "$$(...latex...)" into "$...latex...$" for user input.
|
| 386 |
+
// This can appear from odd delimiter interactions in partially-normalized text.
|
| 387 |
+
normalized = normalized.replace(
|
| 388 |
+
/\$\$\((\\[A-Za-z][^)\n]*)\)/g,
|
| 389 |
+
(_m, expr) => `$${expr.trim()}$`
|
| 390 |
+
);
|
| 391 |
+
|
| 392 |
+
// Repair malformed "$(...latex...)" into "$...latex...$".
|
| 393 |
+
normalized = normalized.replace(
|
| 394 |
+
/\$\((\\[A-Za-z][^)\n]*)\)/g,
|
| 395 |
+
(_m, expr) => `$${expr.trim()}$`
|
| 396 |
+
);
|
| 397 |
+
|
| 398 |
+
const displayDelimiters = normalized.match(/\$\$/g);
|
| 399 |
+
if (displayDelimiters && displayDelimiters.length % 2 === 1) {
|
| 400 |
+
normalized = `${normalized}\n$$`;
|
| 401 |
+
}
|
| 402 |
+
return normalized;
|
| 403 |
+
}
|
| 404 |
+
|
| 405 |
// Wrap common inline commands if user forgot $ delimiters.
|
| 406 |
normalized = normalized.replace(
|
| 407 |
/\\(?:frac|sqrt|sum|int|prod|lim|alpha|beta|gamma|delta|theta|lambda|pi|sigma|mu|partial|cdot|times|leq|geq|neq|infty|forall|exists|sin|cos|tan|log|ln|mathbb|mathbf|mathcal|mathrm|to)(?:\s*\{[^{}]*\}){0,3}(?:\s*\^[^\s{}]+)?(?:\s*_[^\s{}]+)?/g,
|