Update app.py
Browse files
app.py
CHANGED
|
@@ -108,29 +108,31 @@ def create_math_system_message():
|
|
| 108 |
Always be precise, educational, and encourage mathematical thinking."""
|
| 109 |
|
| 110 |
def render_latex(text):
|
| 111 |
-
"""Enhanced LaTeX rendering
|
| 112 |
if not text or len(text) < 5:
|
| 113 |
return text
|
| 114 |
|
| 115 |
try:
|
| 116 |
-
#
|
| 117 |
-
text = re.sub(r'(?<!\\)\$([^\$]+)\$(?!\$)', r'$\1$', text)
|
| 118 |
-
text = re.sub(r'\$\$([^\$]+)
|
| 119 |
-
text = re.sub(r'\\\[([^\\]+)\\\]', r'$$\1$$', text)
|
| 120 |
-
text = re.sub(r'\\\(([^\\]+)\\\)', r'$\1$', text)
|
| 121 |
|
| 122 |
-
# Fix
|
| 123 |
-
text = re.sub(r'\\(lim|frac|sqrt|int|sum|prod|partial|nabla|infty|to|le|ge|neq|approx|cdot|times|div|deg|prime|log|ln|sin|cos|tan|cot|sec|csc|arcsin|arccos|arctan|sinh|cosh)', r'
|
| 124 |
|
| 125 |
-
# Ensure boxed answers render
|
| 126 |
text = re.sub(r'\\boxed\{([^}]+)\}', r'$$\boxed{\1}$$', text)
|
| 127 |
text = re.sub(r'\\frac\{([^}]+)\}\{([^}]+)\}', r'$\frac{\1}{\2}$', text)
|
| 128 |
|
| 129 |
-
# Clean
|
| 130 |
-
text = re.sub(r'\s
|
|
|
|
| 131 |
|
| 132 |
except Exception as e:
|
| 133 |
print(f"⚠️ LaTeX formatting error: {e}")
|
|
|
|
| 134 |
|
| 135 |
return text
|
| 136 |
|
|
|
|
| 108 |
Always be precise, educational, and encourage mathematical thinking."""
|
| 109 |
|
| 110 |
def render_latex(text):
|
| 111 |
+
"""Enhanced LaTeX rendering with better error handling and formatting"""
|
| 112 |
if not text or len(text) < 5:
|
| 113 |
return text
|
| 114 |
|
| 115 |
try:
|
| 116 |
+
# Ensure proper LaTeX delimiters
|
| 117 |
+
text = re.sub(r'(?<!\\)\$([^\$]+)\$(?!\$)', r'$\1$', text) # Inline math
|
| 118 |
+
text = re.sub(r'\$\$([^\$]+)\$\$', r'$$\1$$', text) # Display math
|
| 119 |
+
text = re.sub(r'\\\[([^\\]+)\\\]', r'$$\1$$', text) # Display math alternative
|
| 120 |
+
text = re.sub(r'\\\(([^\\]+)\\\)', r'$\1$', text) # Inline math alternative
|
| 121 |
|
| 122 |
+
# Fix common LaTeX commands
|
| 123 |
+
text = re.sub(r'\\(lim|frac|sqrt|int|sum|prod|partial|nabla|infty|to|le|ge|neq|approx|cdot|times|div|deg|prime|log|ln|sin|cos|tan|cot|sec|csc|arcsin|arccos|arctan|sinh|cosh)', r'\\\1', text)
|
| 124 |
|
| 125 |
+
# Ensure boxed answers and fractions render correctly
|
| 126 |
text = re.sub(r'\\boxed\{([^}]+)\}', r'$$\boxed{\1}$$', text)
|
| 127 |
text = re.sub(r'\\frac\{([^}]+)\}\{([^}]+)\}', r'$\frac{\1}{\2}$', text)
|
| 128 |
|
| 129 |
+
# Clean extra spaces and escape issues
|
| 130 |
+
text = re.sub(r'\s+([\$\\])\s+', r'\1', text)
|
| 131 |
+
text = text.replace(r'frac', r'\frac') # Catch any unescaped frac
|
| 132 |
|
| 133 |
except Exception as e:
|
| 134 |
print(f"⚠️ LaTeX formatting error: {e}")
|
| 135 |
+
return text # Return original text if formatting fails
|
| 136 |
|
| 137 |
return text
|
| 138 |
|