def latex_to_unicode(text):
"""
Convert LaTeX-formatted text to Unicode for Excel display.
Handles Greek letters, superscripts, and subscripts.
"""
import re
if isinstance(text, float):
return str(int(text))
elif not isinstance(text, str):
return text
# Greek letters mapping
greek_letters = {
r'\\alpha': 'α', r'\\beta': 'β', r'\\gamma': 'γ', r'\\delta': 'δ',
r'\\epsilon': 'ε', r'\\zeta': 'ζ', r'\\eta': 'η', r'\\theta': 'θ',
r'\\iota': 'ι', r'\\kappa': 'κ', r'\\lambda': 'λ', r'\\mu': 'μ',
r'\\nu': 'ν', r'\\xi': 'ξ', r'\\pi': 'π', r'\\rho': 'ρ',
r'\\sigma': 'σ', r'\\tau': 'τ', r'\\upsilon': 'υ', r'\\phi': 'φ',
r'\\chi': 'χ', r'\\psi': 'ψ', r'\\omega': 'ω',
r'\\Alpha': 'Α', r'\\Beta': 'Β', r'\\Gamma': 'Γ', r'\\Delta': 'Δ',
r'\\Epsilon': 'Ε', r'\\Zeta': 'Ζ', r'\\Eta': 'Η', r'\\Theta': 'Θ',
r'\\Iota': 'Ι', r'\\Kappa': 'Κ', r'\\Lambda': 'Λ', r'\\Mu': 'Μ',
r'\\Nu': 'Ν', r'\\Xi': 'Ξ', r'\\Pi': 'Π', r'\\Rho': 'Ρ',
r'\\Sigma': 'Σ', r'\\Tau': 'Τ', r'\\Upsilon': 'Υ', r'\\Phi': 'Φ',
r'\\Chi': 'Χ', r'\\Psi': 'Ψ', r'\\Omega': 'Ω'
}
# Superscript mapping
superscript_map = { '0': '⁰', '1': '¹', '2': '²', '3': '³', '4': '⁴',
'5': '⁵', '6': '⁶', '7': '⁷', '8': '⁸', '9': '⁹',
'a': 'ᵃ', 'b': 'ᵇ', 'c': 'ᶜ', 'd': 'ᵈ', 'e': 'ᵉ',
'f': 'ᶠ', 'g': 'ᵍ', 'h': 'ʰ', 'i': 'ⁱ', 'j': 'ʲ',
'k': 'ᵏ', 'l': 'ˡ', 'm': 'ᵐ', 'n': 'ⁿ', 'o': 'ᵒ',
'p': 'ᵖ', 'r': 'ʳ', 's': 'ˢ', 't': 'ᵗ', 'u': 'ᵘ',
'v': 'ᵛ', 'w': 'ʷ', 'x': 'ˣ', 'y': 'ʸ', 'z': 'ᶻ',
'A': 'ᴬ', 'B': 'ᴮ', 'D': 'ᴰ', 'E': 'ᴱ', 'G': 'ᴳ',
'H': 'ᴴ', 'I': 'ᴵ', 'J': 'ᴶ', 'K': 'ᴷ', 'L': 'ᴸ',
'M': 'ᴹ', 'N': 'ᴺ', 'O': 'ᴼ', 'P': 'ᴾ', 'R': 'ᴿ',
'T': 'ᵀ', 'U': 'ᵁ', 'V': 'ⱽ', 'W': 'ᵂ',
'+': '⁺', '-': '⁻', '=': '⁼', '(': '⁽', ')': '⁾'}
# Subscript mapping
subscript_map= {'0': '₀', '1': '₁', '2': '₂', '3': '₃', '4': '₄',
'5': '₅', '6': '₆', '7': '₇', '8': '₈', '9': '₉',
'a': 'ₐ', 'e': 'ₑ', 'h': 'ₕ', 'i': 'ᵢ', 'j': 'ⱼ',
'k': 'ₖ', 'l': 'ₗ', 'm': 'ₘ', 'n': 'ₙ', 'o': 'ₒ',
'p': 'ₚ', 'r': 'ᵣ', 's': 'ₛ', 't': 'ₜ', 'u': 'ᵤ',
'v': 'ᵥ', 'x': 'ₓ',
'+': '₊', '-': '₋', '=': '₌', '(': '₍', ')': '₎'}
# Remove math mode delimiters
text = re.sub(r'\$([^\$]+)\$', r'\1', text)
# Replace Greek letters
for latex, unicode_char in greek_letters.items():
text = text.replace(latex, unicode_char)
# Handle superscripts with braces: ^{...}
def replace_superscript_braces(match):
content = match.group(1)
return ''.join(superscript_map.get(c, c) for c in content)
text = re.sub(r'\^\{([^}]+)\}', replace_superscript_braces, text)
# Handle single character superscripts: ^T, ^2, etc.
def replace_superscript_single(match):
char = match.group(1)
return superscript_map.get(char, '^' + char)
text = re.sub(r'\^(.)', replace_superscript_single, text)
# Handle subscripts with braces: _{...}
def replace_subscript_braces(match):
content = match.group(1)
return ''.join(subscript_map.get(c, c) for c in content)
text = re.sub(r'_\{([^}]+)\}', replace_subscript_braces, text)
# Handle single character subscripts: _i, _j, etc.
def replace_subscript_single(match):
char = match.group(1)
return subscript_map.get(char, '_' + char)
text = re.sub(r'_(.)', replace_subscript_single, text)
return text