"""Test LaTeX syntax space cleaning functionality. Tests the _clean_latex_syntax_spaces() function which removes unwanted spaces in LaTeX syntax that are common OCR errors. """ import re def _clean_latex_syntax_spaces(expr: str) -> str: """Clean unwanted spaces in LaTeX syntax (common OCR errors).""" # Pattern 1: Spaces around _ and ^ expr = re.sub(r'\s*_\s*', '_', expr) expr = re.sub(r'\s*\^\s*', '^', expr) # Pattern 2: Spaces inside braces that follow _ or ^ def clean_subscript_superscript_braces(match): operator = match.group(1) content = match.group(2) # Remove spaces but preserve LaTeX commands cleaned = re.sub(r'(?>> Mismatch!") print() print("=" * 80) print("USER'S SPECIFIC EXAMPLE") print("=" * 80) user_example = r"a _ {i 1}" expected_output = r"a_{i1}" result = _clean_latex_syntax_spaces(user_example) print(f"Input: {user_example}") print(f"Expected: {expected_output}") print(f"Got: {result}") print(f"Status: {'✅ CORRECT' if result == expected_output else '❌ INCORRECT'}") print("\n" + "=" * 80) print("SUMMARY") print("=" * 80) print(f"Total tests: {len(test_cases)}") print(f"✅ Passed: {passed}") print(f"❌ Failed: {failed}") print(f"⚠️ Close: {warnings}") if failed == 0: print("\n✅ All tests passed!") else: print(f"\n⚠️ {failed} test(s) failed") print("\n" + "=" * 80) print("IMPORTANT NOTES") print("=" * 80) print(""" 1. ✅ Subscript/superscript spaces: a _ {i 1} -> a_{i1} 2. ✅ Fraction spaces: \\frac { a } { b } -> \\frac{a}{b} 3. ✅ Command spaces: \\ alpha -> \\alpha 4. ⚠️ This might remove some intentional spaces in expressions 5. ⚠️ LaTeX commands inside braces are preserved (e.g., _{\\alpha}) If any edge cases are broken, the patterns can be adjusted to be more conservative. """) print("=" * 80)