diff --git a/scripts/omml_mathml2latex/visitors/mrow_visitor.py b/scripts/omml_mathml2latex/visitors/mrow_visitor.py index 52946eb..11e56d9 100644 --- a/scripts/omml_mathml2latex/visitors/mrow_visitor.py +++ b/scripts/omml_mathml2latex/visitors/mrow_visitor.py @@ -546,14 +546,28 @@ class MrowVisitor(Visitor): if len(letters) < 2: return ("", 0, None) - # Check if the sequence forms a known function + # Check if the sequence forms a known function. + # Use prefix matching so that "sinx" resolves to "sin" (leaving the + # trailing "x" for the caller to convert), rather than requiring the + # whole run to be a function name. Mirrors the logic in + # legacy_adapter._check_fragmented_function_at_level. from ..data import MATH_FUNCTIONS - potential_function = ''.join(letters) + potential_function = "" + matched_length = 0 + for length in range(min(len(letters), 6), 1, -1): + candidate = ''.join(letters[:length]) + if candidate in MATH_FUNCTIONS: + potential_function = candidate + matched_length = length + break - - if potential_function not in MATH_FUNCTIONS: + if not potential_function: return ("", 0, None) + # Only the matched letters belong to the function; any trailing letters + # (e.g. the "x" in "sinx") remain for the caller to convert. + consumed_count = matched_length + # Build the function LaTeX function_latex = f"\\{potential_function}"