Spaces:
Sleeping
Sleeping
fix: KaTeX only parses inside math delimiters. Anything like \text{Var} or \lambda outside $...$ stays as plain text
Browse files
web/src/math-preprocessing.ts
CHANGED
|
@@ -96,13 +96,16 @@ function wrapBareLatexOutsideMath(content: string): string {
|
|
| 96 |
}
|
| 97 |
|
| 98 |
const latexHeavyPattern = /(?:\\[a-zA-Z]+(?:\{[^}]*\}|[\^_]\{?[^\s}]*\}?)*[\s=+\-*/,]*){1,}/g;
|
|
|
|
| 99 |
|
| 100 |
return segments
|
| 101 |
.map(seg => {
|
| 102 |
if (seg.isMath) return seg.content;
|
| 103 |
|
| 104 |
-
// Remove stray single backslashes at end of line in plain text
|
| 105 |
-
let cleaned = seg.content
|
|
|
|
|
|
|
| 106 |
|
| 107 |
// Wrap contiguous LaTeX command sequences
|
| 108 |
cleaned = cleaned.replace(latexHeavyPattern, (value) => {
|
|
@@ -112,6 +115,14 @@ function wrapBareLatexOutsideMath(content: string): string {
|
|
| 112 |
return `$${value.trim()}$`;
|
| 113 |
});
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
return cleaned;
|
| 116 |
})
|
| 117 |
.join('');
|
|
|
|
| 96 |
}
|
| 97 |
|
| 98 |
const latexHeavyPattern = /(?:\\[a-zA-Z]+(?:\{[^}]*\}|[\^_]\{?[^\s}]*\}?)*[\s=+\-*/,]*){1,}/g;
|
| 99 |
+
const inlineLatexToken = /\\[a-zA-Z]+(?:\{[^}]*\})?(?:\([^\s$]*?\))?(?:\[[^\s$]*?\])?(?:_\{[^}]*\}|_\w+)?(?:\^\{[^}]*\}|\^\w+)?/g;
|
| 100 |
|
| 101 |
return segments
|
| 102 |
.map(seg => {
|
| 103 |
if (seg.isMath) return seg.content;
|
| 104 |
|
| 105 |
+
// Remove LaTeX line-breaks and stray single backslashes at end of line in plain text
|
| 106 |
+
let cleaned = seg.content
|
| 107 |
+
.replace(/\\\s*\n/g, '\n')
|
| 108 |
+
.replace(/(^|[^\\])\\\s*$/gm, '$1');
|
| 109 |
|
| 110 |
// Wrap contiguous LaTeX command sequences
|
| 111 |
cleaned = cleaned.replace(latexHeavyPattern, (value) => {
|
|
|
|
| 115 |
return `$${value.trim()}$`;
|
| 116 |
});
|
| 117 |
|
| 118 |
+
// Wrap individual LaTeX tokens (e.g., \text{Var}(X^2), \lambda) that remain
|
| 119 |
+
cleaned = cleaned.replace(inlineLatexToken, (value, offset, full) => {
|
| 120 |
+
const before = full[offset - 1];
|
| 121 |
+
const after = full[offset + value.length];
|
| 122 |
+
if (before === '$' || after === '$') return value;
|
| 123 |
+
return `$${value}$`;
|
| 124 |
+
});
|
| 125 |
+
|
| 126 |
return cleaned;
|
| 127 |
})
|
| 128 |
.join('');
|