From 93d070930a0b859d68ab55a68f7940c814910500 Mon Sep 17 00:00:00 2001
From: Quentin <1771956812@qq.com>
Date: Thu, 15 Oct 2026 01:27:13 +0800
Subject: [PATCH] =?UTF-8?q?fix(omml2latex):=20fragmented-function=20?=
=?UTF-8?q?=E5=89=8D=E7=BC=80=E5=8C=B9=E9=85=8D=EF=BC=8C=E4=BF=AE=E5=A4=8D?=
=?UTF-8?q?=20sinx=20=E8=AF=AF=E8=BE=93=E5=87=BA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
mrow_visitor 的 _check_fragmented_function_at_position 把所有连续单字母
拼成整串后只查整串是否在 MATH_FUNCTIONS,导致 sin
x 输出 'sinx' 而非 '\sin x'。
改为前缀匹配(对齐 legacy_adapter._check_fragmented_function_at_level),
从最长到最短找已知函数名,只消耗函数名对应字母,其余留给调用方。
验证:sinx/cosx/tanx → \sin x/\cos x/\tan x;纯 sin/log、log_2(msub)、
BC 拼接、abc 非函数等场景零回归;test_mixed.docx 130 公式输出不变。
---
.../visitors/mrow_visitor.py | 22 +++++++++++++++----
1 file changed, 18 insertions(+), 4 deletions(-)
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}"