Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ def do_fname(self, elm):
if FUNC.get(t):
latex_chars.append(FUNC[t])
else:
raise NotImplementedError("Not support func %s" % t)
latex_chars.append(
"\\operatorname{%s}(%s)" % (escape_latex(t), FUNC_PLACE)
)
else:
latex_chars.append(t)
t = BLANK.join(latex_chars)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def _convert_omath_to_latex(tag: Tag) -> str:
math_root = ET.fromstring(MATH_ROOT_TEMPLATE.format(str(tag)))
# Find the 'oMath' element within the XML document
math_element = math_root.find(OMML_NS + "oMath")
if math_element is None:
return tag.get_text("", strip=True)
# Convert the 'oMath' element to LaTeX using the oMath2Latex function
latex = oMath2Latex(math_element).latex
return latex
Expand Down
26 changes: 26 additions & 0 deletions packages/markitdown/tests/test_docx_math.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from bs4 import BeautifulSoup
from defusedxml import ElementTree as ET

from markitdown.converter_utils.docx.math.omml import oMath2Latex
from markitdown.converter_utils.docx.pre_process import _convert_omath_to_latex


def test_convert_omath_without_namespaced_child_returns_text() -> None:
soup = BeautifulSoup(b"<oMath><r><t>x</t></r></oMath>", "xml")

assert _convert_omath_to_latex(soup.find("oMath")) == "x"


def test_unknown_omml_function_uses_operatorname() -> None:
root = ET.fromstring(
"""
<m:oMath xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
<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>
"""
)

assert oMath2Latex(root).latex == r"\operatorname{log}(x)"