Spaces:
Sleeping
Sleeping
continue fixing latex
Browse files- web/src/mathjax-config.ts +23 -0
web/src/mathjax-config.ts
CHANGED
|
@@ -60,6 +60,10 @@ export function preprocessMathContent(content: string): string {
|
|
| 60 |
|
| 61 |
let processed = content;
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
// Step 1: Protect currency amounts from being parsed as math
|
| 64 |
// Strategy: Replace $number with a placeholder, restore after
|
| 65 |
const currencyPlaceholders: string[] = [];
|
|
@@ -100,6 +104,25 @@ export function preprocessMathContent(content: string): string {
|
|
| 100 |
}
|
| 101 |
);
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
// Step 2: Normalize various LaTeX delimiters to MathJax-compatible format
|
| 104 |
|
| 105 |
// Convert \( ... \) to $...$ (inline math)
|
|
|
|
| 60 |
|
| 61 |
let processed = content;
|
| 62 |
|
| 63 |
+
// Step 0.5: Remove trailing backslashes that are line continuations (not LaTeX)
|
| 64 |
+
// These appear at the end of lines and break LaTeX parsing
|
| 65 |
+
processed = processed.replace(/\s+\\$/gm, '');
|
| 66 |
+
|
| 67 |
// Step 1: Protect currency amounts from being parsed as math
|
| 68 |
// Strategy: Replace $number with a placeholder, restore after
|
| 69 |
const currencyPlaceholders: string[] = [];
|
|
|
|
| 104 |
}
|
| 105 |
);
|
| 106 |
|
| 107 |
+
// Step 1.6: Detect standalone LaTeX expressions (line starting with \command)
|
| 108 |
+
// Convert lines like "\frac{...} \le \frac{...}" to display math
|
| 109 |
+
// Only wrap if:
|
| 110 |
+
// 1. Line contains LaTeX commands (\frac, \le, \sqrt, etc.)
|
| 111 |
+
// 2. Not already in $ delimiters
|
| 112 |
+
// 3. Not inside code blocks
|
| 113 |
+
processed = processed.replace(
|
| 114 |
+
/^(?!\s*\$)(\s*\\(?:frac|le|ge|sqrt|cdot|ln|text|quad|implies|iff|ne|in|sum|prod|int|lim|infty|alpha|beta|gamma|delta|epsilon|theta|phi|psi|omega|mathbb|mathrm|begin|end)[^\n$]+)$/gm,
|
| 115 |
+
(match) => {
|
| 116 |
+
// Check if it looks like a standalone math expression
|
| 117 |
+
// Count backslash commands - if 2 or more, it's likely math
|
| 118 |
+
const commandCount = (match.match(/\\[a-zA-Z]+/g) || []).length;
|
| 119 |
+
if (commandCount >= 2) {
|
| 120 |
+
return `$$${match.trim()}$$`;
|
| 121 |
+
}
|
| 122 |
+
return match;
|
| 123 |
+
}
|
| 124 |
+
);
|
| 125 |
+
|
| 126 |
// Step 2: Normalize various LaTeX delimiters to MathJax-compatible format
|
| 127 |
|
| 128 |
// Convert \( ... \) to $...$ (inline math)
|