diff --git a/test_unstructured/partition/html/test_parser.py b/test_unstructured/partition/html/test_parser.py
index 37014f18f3..9ca24b0b3c 100644
--- a/test_unstructured/partition/html/test_parser.py
+++ b/test_unstructured/partition/html/test_parser.py
@@ -1534,14 +1534,14 @@ class DescribeMath:
"""
@pytest.mark.parametrize("wrapper", ["{}", "{}", "{}"])
- def it_skips_mathml_that_has_no_text_representation_but_not_its_tail(self, wrapper: str):
+ def it_preserves_presentation_mathml_and_its_tail(self, wrapper: str):
html = "
Before {} after.
".format(
wrapper.format("")
)
div = etree.fromstring(html, html_parser).xpath(".//div")[0]
assert isinstance(div.xpath(".//math")[0], Math)
- assert [e.text for e in div.iter_elements()] == ["Before after."]
+ assert [e.text for e in div.iter_elements()] == ["Before x=2 after."]
class DescribeRemovedPhrasing:
diff --git a/test_unstructured/partition/html/test_partition.py b/test_unstructured/partition/html/test_partition.py
index 26379f14ec..00a5472a86 100644
--- a/test_unstructured/partition/html/test_partition.py
+++ b/test_unstructured/partition/html/test_partition.py
@@ -894,7 +894,7 @@ def test_partition_html_keeps_katex_html_of_unusable_mathml_inside_an_unrecogniz
wrapper.format(
"Before"
' '
- ''
+ ''
''
'x2'
""
@@ -2223,7 +2223,7 @@ def test_partition_html_keeps_katex_html_when_mathml_is_unusable():
diff --git a/test_unstructured/partition/html/test_presentation_mathml.py b/test_unstructured/partition/html/test_presentation_mathml.py
new file mode 100644
index 0000000000..6afa75acda
--- /dev/null
+++ b/test_unstructured/partition/html/test_presentation_mathml.py
@@ -0,0 +1,74 @@
+import pytest
+
+from unstructured.partition.html import partition_html
+
+
+@pytest.mark.parametrize(
+ ("expression", "expected"),
+ [
+ ("x+2", "x+2"),
+ ("ab", r"\frac{a}{b}"),
+ (
+ "1+52",
+ r"\frac{1+\sqrt{5}}{2}",
+ ),
+ ("x3", r"\sqrt[3]{x}"),
+ ("x2", r"{x}^{2}"),
+ ("xi", r"{x}_{i}"),
+ ("xi2", r"{x}_{i}^{2}"),
+ ("x¯", r"\overset{¯}{x}"),
+ ("limx", r"\underset{x}{lim}"),
+ (
+ "∑in",
+ r"\overset{n}{\underset{i}{∑}}",
+ ),
+ ('xy', "[x;y]"),
+ (
+ "12"
+ "34",
+ r"\begin{matrix}1 & 2 \\ 3 & 4\end{matrix}",
+ ),
+ ("x=50% & rising", r"x=\text{50\% \& rising}"),
+ (
+ "x2"
+ 'do not duplicate',
+ r"{x}^{2}",
+ ),
+ ],
+)
+def test_presentation_mathml_is_retained_without_a_tex_annotation(expression, expected):
+ elements = partition_html(
+ text=f"Value remains.
", languages=["eng"]
+ )
+
+ assert [element.text for element in elements] == [f"Value {expected} remains."]
+
+
+def test_presentation_mathml_preserves_display_boundary_and_page_number():
+ html = (
+ 'BeforeAfter
"
+ )
+
+ elements = partition_html(text=html, languages=["eng"])
+
+ assert [element.text for element in elements] == ["Before", r"\frac{x}{2}", "After"]
+ assert [element.metadata.page_number for element in elements] == [3, 3, 3]
+
+
+@pytest.mark.parametrize(
+ "expression",
+ [
+ "x",
+ "xy",
+ 'xy',
+ ],
+)
+def test_unsupported_or_malformed_math_keeps_existing_visual_fallback(expression):
+ html = (
+ ''
+ f""
+ 'Visual fallback
'
+ )
+
+ assert partition_html(text=html, languages=["eng"])[0].text == "Visual fallback"
diff --git a/unstructured/partition/html/mathml.py b/unstructured/partition/html/mathml.py
new file mode 100644
index 0000000000..1feea58730
--- /dev/null
+++ b/unstructured/partition/html/mathml.py
@@ -0,0 +1,100 @@
+"""A structural LaTeX fallback for common presentation MathML without a TeX annotation."""
+
+from lxml import etree
+
+
+def _escape(text: str) -> str:
+ escapes = {"\\": r"\backslash{}", "^": r"\^{}", "~": r"\~{}"}
+ escapes.update({char: "\\" + char for char in "{}_%$"})
+ return "".join(escapes.get(char, char) for char in text)
+
+
+def presentation_to_latex(node: etree._Element) -> str | None:
+ """Render supported presentation constructs, or return None for an unsupported expression.
+
+ This is a fallback for extraction, not a MathML layout engine. Unknown nodes, malformed
+ arity and unhandled fraction-line semantics reject the whole expression so a paired visual
+ fallback remains available. Existing TeX annotations and ``alttext`` take precedence.
+ """
+ tag = node.tag
+ if tag in {"mi", "mn", "mo", "mtext"}:
+ if len(node):
+ return None
+ text = _escape(node.text or "")
+ return rf"\text{{{text}}}" if tag == "mtext" else text.strip()
+ if tag == "mspace":
+ return " "
+ if tag == "semantics":
+ children = [child for child in node if child.tag not in {"annotation", "annotation-xml"}]
+ if len(children) != 1 or (node.text or "").strip():
+ return None
+ return presentation_to_latex(children[0])
+
+ supported = {
+ "math",
+ "mrow",
+ "mstyle",
+ "mpadded",
+ "mtd",
+ "mfrac",
+ "msqrt",
+ "mroot",
+ "msup",
+ "msub",
+ "msubsup",
+ "mover",
+ "munder",
+ "munderover",
+ "mfenced",
+ "mtable",
+ "mtr",
+ }
+ if tag not in supported:
+ return None
+ if (node.text or "").strip() or any((child.tail or "").strip() for child in node):
+ return None
+ parts = []
+ for child in node:
+ part = presentation_to_latex(child)
+ if part is None:
+ return None
+ parts.append(part)
+ if tag in {"math", "mrow", "mstyle", "mpadded", "mtd"}:
+ return "".join(parts)
+ if tag == "msqrt":
+ return rf"\sqrt{{{''.join(parts)}}}"
+ if tag == "mfenced":
+ separators = "".join(node.get("separators", ",").split())
+ inside = "".join(
+ (_escape(separators[min(i - 1, len(separators) - 1)]) if i and separators else "")
+ + part
+ for i, part in enumerate(parts)
+ )
+ return _escape(node.get("open", "(")) + inside + _escape(node.get("close", ")"))
+ if tag == "mtr":
+ return " & ".join(parts) if all(child.tag == "mtd" for child in node) else None
+ if tag == "mtable":
+ if any(child.tag != "mtr" for child in node):
+ return None
+ return r"\begin{matrix}" + r" \\ ".join(parts) + r"\end{matrix}"
+ expected_arity = 3 if tag in {"msubsup", "munderover"} else 2
+ if len(parts) != expected_arity:
+ return None
+ base, second = parts[:2]
+ if tag == "mfrac":
+ if node.get("linethickness") is not None:
+ return None
+ return rf"\frac{{{base}}}{{{second}}}"
+ if tag == "mroot":
+ return rf"\sqrt[{second}]{{{base}}}"
+ if tag == "msup":
+ return f"{{{base}}}^{{{second}}}"
+ if tag == "msub":
+ return f"{{{base}}}_{{{second}}}"
+ if tag == "msubsup":
+ return f"{{{base}}}_{{{second}}}^{{{parts[2]}}}"
+ if tag == "mover":
+ return rf"\overset{{{second}}}{{{base}}}"
+ if tag == "munder":
+ return rf"\underset{{{second}}}{{{base}}}"
+ return rf"\overset{{{parts[2]}}}{{\underset{{{second}}}{{{base}}}}}"
diff --git a/unstructured/partition/html/parser.py b/unstructured/partition/html/parser.py
index 30c45e836e..c7c44e5360 100644
--- a/unstructured/partition/html/parser.py
+++ b/unstructured/partition/html/parser.py
@@ -100,6 +100,7 @@
Title,
)
from unstructured.partition.common.metadata import category_depth_from_html_tag
+from unstructured.partition.html.mathml import presentation_to_latex
from unstructured.partition.text_type import (
is_bulleted_text,
is_email_address,
@@ -798,7 +799,7 @@ def _latex(self) -> str | None:
if alt_text is not None and alt_text.strip():
return alt_text
- return None
+ return presentation_to_latex(self)
@cached_property
def _page_number(self) -> int | None: