fraudTech commited on
Commit
ea349e2
·
1 Parent(s): 0248c94

fix some latex not rendering

Browse files
Files changed (1) hide show
  1. web/src/mathjax-config.ts +15 -8
web/src/mathjax-config.ts CHANGED
@@ -81,7 +81,7 @@ export function preprocessMathContent(content: string): string {
81
  (match, math) => {
82
  // Check if this looks like LaTeX (has backslash commands)
83
  if (/\\[a-zA-Z]+/.test(math)) {
84
- return `$$${math.trim()}$$`;
85
  }
86
  return match; // Return unchanged if not LaTeX
87
  }
@@ -94,7 +94,7 @@ export function preprocessMathContent(content: string): string {
94
  (match, math) => {
95
  // Check if this looks like LaTeX
96
  if (/\\[a-zA-Z]+/.test(math)) {
97
- return `$${math.trim()}$`;
98
  }
99
  return match; // Return unchanged if not LaTeX
100
  }
@@ -104,28 +104,30 @@ export function preprocessMathContent(content: string): string {
104
 
105
  // Convert \( ... \) to $...$ (inline math)
106
  // Use non-greedy match with negative lookbehind for escape
 
107
  processed = processed.replace(
108
  /(?<!\\)\\\((.+?)(?<!\\)\\\)/gs,
109
- (_, math) => `$${math.trim()}$`
110
  );
111
 
112
  // Convert \[ ... \] to $$...$$ (display math)
 
113
  processed = processed.replace(
114
  /(?<!\\)\\\[\s*([\s\S]*?)(?<!\\)\\\]/g,
115
- (_, math) => `$$${math.trim()}$$`
116
  );
117
 
118
  // Handle escaped parentheses that might indicate inline math: \( content \)
119
  // This handles cases where the backslash is already escaped in the string
120
  processed = processed.replace(
121
  /\\\\\(\s*(.*?)\s*\\\\\)/g,
122
- (_, math) => `$${math.trim()}$`
123
  );
124
 
125
  // Handle escaped square brackets for display math: \[ content \]
126
  processed = processed.replace(
127
  /\\\\\[\s*([\s\S]*?)\s*\\\\\]/g,
128
- (_, math) => `$$${math.trim()}$$`
129
  );
130
 
131
  // Step 3: Fix spacing issues around single dollar signs
@@ -134,18 +136,23 @@ export function preprocessMathContent(content: string): string {
134
 
135
  // Step 4: Ensure display math with newlines is properly formatted
136
  // MathJax needs $$ on separate lines for multi-line display math
 
137
  processed = processed.replace(
138
  /\$\$([\s\S]*?)\$\$/g,
139
  (_match, math) => {
140
  const trimmed = math.trim();
141
  // If math contains newlines and isn't already wrapped, ensure clean formatting
142
  if (trimmed.includes('\n')) {
143
- return `$$\n${trimmed}\n$$`;
144
  }
145
- return `$$${trimmed}$$`;
146
  }
147
  );
148
 
 
 
 
 
149
  // Step 5: Handle begin/end environments that might be inline
150
  // Ensure \begin{align}...\end{align} is in display mode
151
  processed = processed.replace(
 
81
  (match, math) => {
82
  // Check if this looks like LaTeX (has backslash commands)
83
  if (/\\[a-zA-Z]+/.test(math)) {
84
+ return `\n$$${math.trim()}$$\n`;
85
  }
86
  return match; // Return unchanged if not LaTeX
87
  }
 
94
  (match, math) => {
95
  // Check if this looks like LaTeX
96
  if (/\\[a-zA-Z]+/.test(math)) {
97
+ return ` $${math.trim()}$ `;
98
  }
99
  return match; // Return unchanged if not LaTeX
100
  }
 
104
 
105
  // Convert \( ... \) to $...$ (inline math)
106
  // Use non-greedy match with negative lookbehind for escape
107
+ // Add spaces around inline math for proper text flow
108
  processed = processed.replace(
109
  /(?<!\\)\\\((.+?)(?<!\\)\\\)/gs,
110
+ (_, math) => ` $${math.trim()}$ `
111
  );
112
 
113
  // Convert \[ ... \] to $$...$$ (display math)
114
+ // Ensure newline after to prevent text concatenation
115
  processed = processed.replace(
116
  /(?<!\\)\\\[\s*([\s\S]*?)(?<!\\)\\\]/g,
117
+ (_, math) => `\n$$${math.trim()}$$\n`
118
  );
119
 
120
  // Handle escaped parentheses that might indicate inline math: \( content \)
121
  // This handles cases where the backslash is already escaped in the string
122
  processed = processed.replace(
123
  /\\\\\(\s*(.*?)\s*\\\\\)/g,
124
+ (_, math) => ` $${math.trim()}$ `
125
  );
126
 
127
  // Handle escaped square brackets for display math: \[ content \]
128
  processed = processed.replace(
129
  /\\\\\[\s*([\s\S]*?)\s*\\\\\]/g,
130
+ (_, math) => `\n$$${math.trim()}$$\n`
131
  );
132
 
133
  // Step 3: Fix spacing issues around single dollar signs
 
136
 
137
  // Step 4: Ensure display math with newlines is properly formatted
138
  // MathJax needs $$ on separate lines for multi-line display math
139
+ // Also ensure there's whitespace after closing $$ to prevent text concatenation
140
  processed = processed.replace(
141
  /\$\$([\s\S]*?)\$\$/g,
142
  (_match, math) => {
143
  const trimmed = math.trim();
144
  // If math contains newlines and isn't already wrapped, ensure clean formatting
145
  if (trimmed.includes('\n')) {
146
+ return `$$\n${trimmed}\n$$\n`;
147
  }
148
+ return `$$${trimmed}$$\n`;
149
  }
150
  );
151
 
152
+ // Step 4.5: Clean up multiple consecutive newlines created by Step 4
153
+ // Replace 3+ newlines with just 2 newlines (blank line)
154
+ processed = processed.replace(/\n{3,}/g, '\n\n');
155
+
156
  // Step 5: Handle begin/end environments that might be inline
157
  // Ensure \begin{align}...\end{align} is in display mode
158
  processed = processed.replace(