Skip to content

Python tool for converting files and office documents to Markdown.

fix(docx): handle unknown math functions in OMML converter without crashing (#2268)

Nefelibata58783e327250
diff --git a/packages/markitdown/src/markitdown/converter_utils/docx/math/latex_dict.py b/packages/markitdown/src/markitdown/converter_utils/docx/math/latex_dict.py
index 5657a600..21fdeaac 100644
--- a/packages/markitdown/src/markitdown/converter_utils/docx/math/latex_dict.py
+++ b/packages/markitdown/src/markitdown/converter_utils/docx/math/latex_dict.py
@@ -209,13 +209,29 @@ FUNC = {
"arcsin": "\\arcsin({fe})",
"arccos": "\\arccos({fe})",
"arctan": "\\arctan({fe})",
- "arccot": "\\arccot({fe})",
+ "arccot": "\\operatorname{arccot}({fe})",
"sinh": "\\sinh({fe})",
"cosh": "\\cosh({fe})",
"tanh": "\\tanh({fe})",
"coth": "\\coth({fe})",
"sec": "\\sec({fe})",
"csc": "\\csc({fe})",
+ "log": "\\log({fe})",
+ "ln": "\\ln({fe})",
+ "exp": "\\exp({fe})",
+ "det": "\\det({fe})",
+ "gcd": "\\gcd({fe})",
+ "lcm": "\\operatorname{lcm}({fe})",
+ "lim": "\\lim({fe})",
+ "max": "\\max({fe})",
+ "min": "\\min({fe})",
+ "sup": "\\sup({fe})",
+ "inf": "\\inf({fe})",
+ "dim": "\\dim({fe})",
+ "ker": "\\ker({fe})",
+ "hom": "\\hom({fe})",
+ "deg": "\\deg({fe})",
+ "arg": "\\arg({fe})",
}
FUNC_PLACE = "{fe}"
diff --git a/packages/markitdown/src/markitdown/converter_utils/docx/math/omml.py b/packages/markitdown/src/markitdown/converter_utils/docx/math/omml.py
index f566f7d3..6f5d1e83 100644
--- a/packages/markitdown/src/markitdown/converter_utils/docx/math/omml.py
+++ b/packages/markitdown/src/markitdown/converter_utils/docx/math/omml.py
@@ -272,14 +272,24 @@ class oMath2Latex(Tag2Method):
the func name
"""
latex_chars = []
+ # Word may split a single function name over several runs (e.g. on a
+ # formatting boundary), so join adjacent runs before looking it up.
+ name_parts = []
+
+ def flush_name():
+ if not name_parts:
+ return
+ name = BLANK.join(name_parts)
+ del name_parts[:]
+ latex_chars.append(FUNC.get(name) or r"\operatorname{%s}({fe})" % name)
+
for stag, t, e in self.process_children_list(elm):
if stag == "r":
- if FUNC.get(t):
- latex_chars.append(FUNC[t])
- else:
- raise NotImplementedError("Not support func %s" % t)
+ name_parts.append(t)
else:
+ flush_name()
latex_chars.append(t)
+ flush_name()
t = BLANK.join(latex_chars)
return t if FUNC_PLACE in t else t + FUNC_PLACE # do_func will replace this
diff --git a/packages/markitdown/tests/test_docx_math.py b/packages/markitdown/tests/test_docx_math.py
new file mode 100644
index 00000000..07827bec
--- /dev/null
+++ b/packages/markitdown/tests/test_docx_math.py
@@ -0,0 +1,59 @@
+from markitdown.converter_utils.docx.math.omml import load_string
+
+
+def test_omml_known_function():
+ omml_xml = """<m:oMathPara xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
+ <m:oMath>
+ <m:func>
+ <m:fName><m:r><m:t>log</m:t></m:r></m:fName>
+ <m:e><m:r><m:t>x</m:t></m:r></m:e>
+ </m:func>
+ </m:oMath>
+ </m:oMathPara>"""
+ results = list(load_string(omml_xml))
+ assert len(results) == 1
+ assert r"\log" in str(results[0])
+
+
+def test_omml_unknown_function_fallback():
+ omml_xml = """<m:oMathPara xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
+ <m:oMath>
+ <m:func>
+ <m:fName><m:r><m:t>customFunc</m:t></m:r></m:fName>
+ <m:e><m:r><m:t>x</m:t></m:r></m:e>
+ </m:func>
+ </m:oMath>
+ </m:oMathPara>"""
+ results = list(load_string(omml_xml))
+ assert len(results) == 1
+ assert r"\operatorname{customFunc}" in str(results[0])
+
+
+def test_omml_function_name_split_across_runs():
+ # Word can break a single function name into several runs (e.g. at a
+ # formatting boundary); the runs must be joined before the lookup.
+ omml_xml = """<m:oMathPara xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
+ <m:oMath>
+ <m:func>
+ <m:fName><m:r><m:t>custom</m:t></m:r><m:r><m:t>Func</m:t></m:r></m:fName>
+ <m:e><m:r><m:t>x</m:t></m:r></m:e>
+ </m:func>
+ </m:oMath>
+ </m:oMathPara>"""
+ results = list(load_string(omml_xml))
+ assert len(results) == 1
+ assert str(results[0]) == r"\operatorname{customFunc}(x)"
+
+
+def test_omml_known_function_split_across_runs():
+ omml_xml = """<m:oMathPara xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
+ <m:oMath>
+ <m:func>
+ <m:fName><m:r><m:t>si</m:t></m:r><m:r><m:t>n</m:t></m:r></m:fName>
+ <m:e><m:r><m:t>x</m:t></m:r></m:e>
+ </m:func>
+ </m:oMath>
+ </m:oMathPara>"""
+ results = list(load_string(omml_xml))
+ assert len(results) == 1
+ assert str(results[0]) == r"\sin(x)"