fraudTech commited on
Commit
44920d9
·
1 Parent(s): 87bfde3

fix bare LaTeX outside delimiters (like \text{Var} or \lambda in plain text)

Browse files
Files changed (1) hide show
  1. web/src/math-preprocessing.ts +10 -1
web/src/math-preprocessing.ts CHANGED
@@ -97,6 +97,7 @@ function wrapBareLatexOutsideMath(content: string): string {
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 => {
@@ -105,7 +106,8 @@ function wrapBareLatexOutsideMath(content: string): string {
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,6 +117,13 @@ function wrapBareLatexOutsideMath(content: string): string {
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];
 
97
 
98
  const latexHeavyPattern = /(?:\\[a-zA-Z]+(?:\{[^}]*\}|[\^_]\{?[^\s}]*\}?)*[\s=+\-*/,]*){1,}/g;
99
  const inlineLatexToken = /\\[a-zA-Z]+(?:\{[^}]*\})?(?:\([^\s$]*?\))?(?:\[[^\s$]*?\])?(?:_\{[^}]*\}|_\w+)?(?:\^\{[^}]*\}|\^\w+)?/g;
100
+ const latexFunctionPattern = /\\[a-zA-Z]+(?:\{[^}]*\})?\s*\([^\n]*?\)/g;
101
 
102
  return segments
103
  .map(seg => {
 
106
  // Remove LaTeX line-breaks and stray single backslashes at end of line in plain text
107
  let cleaned = seg.content
108
  .replace(/\\\s*\n/g, '\n')
109
+ .replace(/(^|[^\\])\\\s*$/gm, '$1')
110
+ .replace(/\\\./g, '.');
111
 
112
  // Wrap contiguous LaTeX command sequences
113
  cleaned = cleaned.replace(latexHeavyPattern, (value) => {
 
117
  return `$${value.trim()}$`;
118
  });
119
 
120
+ // Wrap command + parenthesis groups (e.g., \text{Var}(X^2))
121
+ cleaned = cleaned.replace(latexFunctionPattern, (value) => {
122
+ const trimmed = value.trim();
123
+ if (trimmed.startsWith('$') || trimmed.endsWith('$')) return value;
124
+ return `$${trimmed}$`;
125
+ });
126
+
127
  // Wrap individual LaTeX tokens (e.g., \text{Var}(X^2), \lambda) that remain
128
  cleaned = cleaned.replace(inlineLatexToken, (value, offset, full) => {
129
  const before = full[offset - 1];