Spaces:
Sleeping
Sleeping
Fix Latex with \begin
Browse files- web/src/App.tsx +31 -0
- web/src/math-preprocessing.ts +28 -17
web/src/App.tsx
CHANGED
|
@@ -206,6 +206,37 @@ const splitStreamingContent = (content: string): { safe: string; tail: string }
|
|
| 206 |
|
| 207 |
const char = content[i];
|
| 208 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
if (char === '\\') {
|
| 210 |
i += 1;
|
| 211 |
continue;
|
|
|
|
| 206 |
|
| 207 |
const char = content[i];
|
| 208 |
|
| 209 |
+
// Detect \begin{env}...\end{env} for display math environments BEFORE the generic
|
| 210 |
+
// escape-skip below (which would consume the 'b' in \begin and miss it).
|
| 211 |
+
// If the environment is unclosed (still streaming), treat it as an unsafe tail.
|
| 212 |
+
if (char === '\\' && !inDisplayMath && !inInlineMath && !inCodeFence &&
|
| 213 |
+
content.startsWith('\\begin{', i)) {
|
| 214 |
+
const endBrace = content.indexOf('}', i + 7);
|
| 215 |
+
if (endBrace !== -1) {
|
| 216 |
+
const envName = content.slice(i + 7, endBrace);
|
| 217 |
+
const baseEnv = envName.replace(/\*$/, ''); // strip trailing * for align*, etc.
|
| 218 |
+
const displayEnvNames = [
|
| 219 |
+
'aligned', 'align', 'equation', 'gather', 'multline',
|
| 220 |
+
'array', 'matrix', 'pmatrix', 'bmatrix', 'vmatrix', 'Vmatrix',
|
| 221 |
+
'cases', 'split', 'eqnarray',
|
| 222 |
+
];
|
| 223 |
+
if (displayEnvNames.includes(baseEnv)) {
|
| 224 |
+
const endPattern = `\\end{${envName}}`;
|
| 225 |
+
const endIdx = content.indexOf(endPattern, endBrace);
|
| 226 |
+
if (endIdx === -1) {
|
| 227 |
+
// \end{env} not yet received — keep as raw tail until it arrives
|
| 228 |
+
return {
|
| 229 |
+
safe: content.slice(0, i),
|
| 230 |
+
tail: content.slice(i),
|
| 231 |
+
};
|
| 232 |
+
}
|
| 233 |
+
// Complete block present — skip past \end{env} so the rest is scanned normally
|
| 234 |
+
i = endIdx + endPattern.length - 1;
|
| 235 |
+
continue;
|
| 236 |
+
}
|
| 237 |
+
}
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
if (char === '\\') {
|
| 241 |
i += 1;
|
| 242 |
continue;
|
web/src/math-preprocessing.ts
CHANGED
|
@@ -58,10 +58,28 @@ export function preprocessMathContent(content: string): string {
|
|
| 58 |
(_match: string, inner: string) => `$$\n${inner.trim()}\n$$`
|
| 59 |
);
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
// Step 0: Wrap undelimited \begin{env}...\end{env} blocks in $$ delimiters.
|
| 62 |
// Only wraps blocks that are NOT already inside existing $$ delimiters.
|
| 63 |
-
//
|
| 64 |
-
//
|
| 65 |
{
|
| 66 |
const envPattern = new RegExp(
|
| 67 |
`\\\\begin\\{(${DISPLAY_MATH_ENVS.join('|')})\\}([\\s\\S]*?)\\\\end\\{\\1\\}`,
|
|
@@ -76,21 +94,6 @@ export function preprocessMathContent(content: string): string {
|
|
| 76 |
}).join('');
|
| 77 |
}
|
| 78 |
|
| 79 |
-
// Step 1: Normalize delimiters OUTSIDE existing $$ blocks only.
|
| 80 |
-
// Convert \(...\) to $...$ and \[...\] to $$...$$
|
| 81 |
-
// Protecting existing $$ blocks prevents double-conversion of \[ inside aligned envs.
|
| 82 |
-
{
|
| 83 |
-
const parts1 = processed.split(/(\$\$[\s\S]*?\$\$)/g);
|
| 84 |
-
processed = parts1.map((part) => {
|
| 85 |
-
if (part.startsWith('$$') && part.endsWith('$$')) return part;
|
| 86 |
-
return part
|
| 87 |
-
.replace(/\\\(/g, '$')
|
| 88 |
-
.replace(/\\\)/g, '$')
|
| 89 |
-
.replace(/\\\[/g, '\n$$\n')
|
| 90 |
-
.replace(/\\\]/g, '\n$$\n');
|
| 91 |
-
}).join('');
|
| 92 |
-
}
|
| 93 |
-
|
| 94 |
// Step 1.5: Re-run the lone-$-on-its-own-line fix AFTER delimiter normalization.
|
| 95 |
// \(\ncontent\n\) becomes $\ncontent\n$ after Step 1, so we need another pass.
|
| 96 |
for (let _pass2 = 0; _pass2 < 2; _pass2++) {
|
|
@@ -114,6 +117,11 @@ export function preprocessMathContent(content: string): string {
|
|
| 114 |
|
| 115 |
// Step 3: Clean up delimiter stacking ($$$$, $$$, etc.)
|
| 116 |
processed = processed.replace(/\${3,}/g, '$$');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
// Step 4: Fix malformed \left and \right (only strip when completely invalid)
|
| 119 |
// Only remove \left/\right when followed by another backslash-command (chained incorrectly)
|
|
@@ -142,6 +150,9 @@ export function preprocessMathContent(content: string): string {
|
|
| 142 |
const trimmed = math.trim();
|
| 143 |
return `\n$$\n${trimmed}\n$$\n`;
|
| 144 |
});
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
// Step 7: Fix spacing commands used outside math: \,2mm] → remove
|
| 147 |
// This cleans up stray \2mm] artifacts from malformed \\[2mm] line spacing
|
|
|
|
| 58 |
(_match: string, inner: string) => `$$\n${inner.trim()}\n$$`
|
| 59 |
);
|
| 60 |
|
| 61 |
+
// Step 1: Normalize delimiters OUTSIDE existing $$ blocks only.
|
| 62 |
+
// Convert \(...\) to $...$ and \[...\] to $$...$$
|
| 63 |
+
// MUST run BEFORE Step 0 so that \[ \begin{aligned}...\end{aligned} \] is converted
|
| 64 |
+
// to $$...$$ first — otherwise Step 0 would wrap the inner \begin{env} in $$, then
|
| 65 |
+
// Step 1 would convert the outer \[ and \] into extra $$ markers, creating empty blocks
|
| 66 |
+
// and leaving the \begin{aligned} as raw text outside any $$ wrapper.
|
| 67 |
+
{
|
| 68 |
+
const parts1 = processed.split(/(\$\$[\s\S]*?\$\$)/g);
|
| 69 |
+
processed = parts1.map((part) => {
|
| 70 |
+
if (part.startsWith('$$') && part.endsWith('$$')) return part;
|
| 71 |
+
return part
|
| 72 |
+
.replace(/\\\(/g, '$')
|
| 73 |
+
.replace(/\\\)/g, '$')
|
| 74 |
+
.replace(/\\\[/g, '\n$$\n')
|
| 75 |
+
.replace(/\\\]/g, '\n$$\n');
|
| 76 |
+
}).join('');
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
// Step 0: Wrap undelimited \begin{env}...\end{env} blocks in $$ delimiters.
|
| 80 |
// Only wraps blocks that are NOT already inside existing $$ delimiters.
|
| 81 |
+
// Runs AFTER Step 1 so that \[ \begin{aligned}...\end{aligned} \] is already $$-wrapped
|
| 82 |
+
// and won't be double-wrapped here.
|
| 83 |
{
|
| 84 |
const envPattern = new RegExp(
|
| 85 |
`\\\\begin\\{(${DISPLAY_MATH_ENVS.join('|')})\\}([\\s\\S]*?)\\\\end\\{\\1\\}`,
|
|
|
|
| 94 |
}).join('');
|
| 95 |
}
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
// Step 1.5: Re-run the lone-$-on-its-own-line fix AFTER delimiter normalization.
|
| 98 |
// \(\ncontent\n\) becomes $\ncontent\n$ after Step 1, so we need another pass.
|
| 99 |
for (let _pass2 = 0; _pass2 < 2; _pass2++) {
|
|
|
|
| 117 |
|
| 118 |
// Step 3: Clean up delimiter stacking ($$$$, $$$, etc.)
|
| 119 |
processed = processed.replace(/\${3,}/g, '$$');
|
| 120 |
+
|
| 121 |
+
// Step 3.5: Remove empty display math blocks ($$\s*$$).
|
| 122 |
+
// These are created when \[ \] (empty) is converted to $$…$$ or when delimiter
|
| 123 |
+
// normalization produces adjacent $$ markers with only whitespace between them.
|
| 124 |
+
processed = processed.replace(/\$\$\s*\$\$/g, '');
|
| 125 |
|
| 126 |
// Step 4: Fix malformed \left and \right (only strip when completely invalid)
|
| 127 |
// Only remove \left/\right when followed by another backslash-command (chained incorrectly)
|
|
|
|
| 150 |
const trimmed = math.trim();
|
| 151 |
return `\n$$\n${trimmed}\n$$\n`;
|
| 152 |
});
|
| 153 |
+
|
| 154 |
+
// Step 6.5: Remove empty display math blocks that Step 6 may have normalised to \n$$\n\n$$\n
|
| 155 |
+
processed = processed.replace(/\n\$\$\n\s*\n\$\$\n/g, '\n');
|
| 156 |
|
| 157 |
// Step 7: Fix spacing commands used outside math: \,2mm] → remove
|
| 158 |
// This cleans up stray \2mm] artifacts from malformed \\[2mm] line spacing
|