fix: 修复 OMML 多字母标识符被空格拆开(BC → B C) #1

Open
opened 2026-10-15 01:01:33 +08:00 by Quentin · 0 comments
Owner

问题现象

Word 里连写的多字母标识符,经 OMML2MML.xsl 转换后会被拆成相邻的 <mi>/<mn> 元素,原转换器用空格 join,导致:

原始 错误输出 正确输出
线段 BC=2 B C = 2(渲染成 B×C) BC = 2
△ABC \triangle A B C \triangle ABC
45° 4 5 ^\circ 45^\circ
AB//CD A B / / C D AB // CD

根因

三处都用 ' '.join(parts) 把所有子元素硬性空格连接:

  1. visitors/mrow_visitor.py 的 _convert_children_with_spaces
  2. compat/legacy_adapter.py 的 <math> 根节点多子元素处理(真实 docx 走这里,因为 OMML2MML 输出的 MathML 无 <mrow> 包裹)
  3. strategies/math_strategy.py 的 format_identifier(Unicode+字母混合内容,如 <mi>△ABC</mi>)

修复

  • 连续单字母 <mi>(无 mathvariant)拼接为整体,先于 fragmented-function 检测之后执行
  • Unicode+字母混合内容改为「仅 LaTeX 命令后跟字母/数字才加空格」的规则,避免 \triangleA 变未定义命令,同时保持 ABC 连写

验证

test_mixed.docx(130 个 OMML 公式)修复前后对比:16 个公式发生变化,全部为正确改善(线段名/角度数字/三角形/隐式乘法拼接),零误伤。sin/log/x+1/2x/\frac 等场景无回归。

关联 commit:d4c2fd1

## 问题现象 Word 里连写的多字母标识符,经 OMML2MML.xsl 转换后会被拆成相邻的 `<mi>`/`<mn>` 元素,原转换器用空格 join,导致: | 原始 | 错误输出 | 正确输出 | |------|---------|---------| | 线段 BC=2 | `B C = 2`(渲染成 B×C) | `BC = 2` | | △ABC | `\triangle A B C` | `\triangle ABC` | | 45° | `4 5 ^\circ` | `45^\circ` | | AB//CD | `A B / / C D` | `AB // CD` | ## 根因 三处都用 `' '.join(parts)` 把所有子元素硬性空格连接: 1. `visitors/mrow_visitor.py` 的 `_convert_children_with_spaces` 2. `compat/legacy_adapter.py` 的 `<math>` 根节点多子元素处理(真实 docx 走这里,因为 OMML2MML 输出的 MathML 无 `<mrow>` 包裹) 3. `strategies/math_strategy.py` 的 `format_identifier`(Unicode+字母混合内容,如 `<mi>△ABC</mi>`) ## 修复 - 连续单字母 `<mi>`(无 mathvariant)拼接为整体,先于 fragmented-function 检测之后执行 - Unicode+字母混合内容改为「仅 LaTeX 命令后跟字母/数字才加空格」的规则,避免 `\triangleA` 变未定义命令,同时保持 `ABC` 连写 ## 验证 `test_mixed.docx`(130 个 OMML 公式)修复前后对比:16 个公式发生变化,**全部为正确改善**(线段名/角度数字/三角形/隐式乘法拼接),零误伤。`sin`/`log`/`x+1`/`2x`/`\frac` 等场景无回归。 关联 commit:`d4c2fd1`
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Quentin/DoLuEdu-WordFileParse#1
No description provided.