diff --git a/paddleocr/_doc2md/math/latex_dict.py b/paddleocr/_doc2md/math/latex_dict.py index cd65132a7d0..050b75e29e4 100644 --- a/paddleocr/_doc2md/math/latex_dict.py +++ b/paddleocr/_doc2md/math/latex_dict.py @@ -320,6 +320,9 @@ "lim": "\\lim_{{{lim}}}", "max": "\\max_{{{lim}}}", "min": "\\min_{{{lim}}}", + "inf": "\\inf_{{{lim}}}", + "sup": "\\sup_{{{lim}}}", + "gcd": "\\gcd_{{{lim}}}", } LIM_TO = ("\\rightarrow", "\\to") diff --git a/paddleocr/_doc2md/math/omml.py b/paddleocr/_doc2md/math/omml.py index 8fadc1cef8c..adca74d637b 100644 --- a/paddleocr/_doc2md/math/omml.py +++ b/paddleocr/_doc2md/math/omml.py @@ -141,7 +141,7 @@ def process_unknow(self, elm, stag): class Pr(Tag2Method): text = "" - __val_tags = ("chr", "pos", "begChr", "endChr", "type") + __val_tags = ("chr", "pos", "begChr", "endChr", "sepChr", "type") __innerdict = None # can't use the __dict__ @@ -174,6 +174,7 @@ def do_common(self, elm): "pos": do_common, "begChr": do_common, "endChr": do_common, + "sepChr": do_common, "type": do_common, } @@ -227,33 +228,51 @@ def do_acc(self, elm): the accent function """ c_dict = self.process_children_dict(elm) + pr = c_dict.get("accPr") latex_s = get_val( - c_dict["accPr"].chr, default=CHR_DEFAULT.get("ACC_VAL"), store=CHR + pr.chr if pr is not None else None, + default=CHR_DEFAULT.get("ACC_VAL"), + store=CHR, ) - return latex_s.format(c_dict["e"]) + return (pr.text if pr is not None else "") + latex_s.format(c_dict["e"]) def do_bar(self, elm): """ the bar function """ c_dict = self.process_children_dict(elm) - pr = c_dict["barPr"] - latex_s = get_val(pr.pos, default=POS_DEFAULT.get("BAR_VAL"), store=POS) - return pr.text + latex_s.format(c_dict["e"]) + pr = c_dict.get("barPr") + latex_s = get_val( + pr.pos if pr is not None else None, + default=POS_DEFAULT.get("BAR_VAL"), + store=POS, + ) + return (pr.text if pr is not None else "") + latex_s.format(c_dict["e"]) def do_d(self, elm): """ the delimiter object """ - c_dict = self.process_children_dict(elm) - pr = c_dict["dPr"] + children = list(self.process_children_list(elm)) + pr = next((text for stag, text, _ in children if stag == "dPr"), None) null = D_DEFAULT.get("null") - - s_val = get_val(pr.begChr, default=D_DEFAULT.get("left"), store=T) - e_val = get_val(pr.endChr, default=D_DEFAULT.get("right"), store=T) - delim = pr.text + D.format( + expressions = [text for stag, text, _ in children if stag == "e"] + separator = get_val(pr.sepChr if pr is not None else None, default=",") + separator = r"\middle|" if separator == "|" else escape_latex(separator) + + s_val = get_val( + pr.begChr if pr is not None else None, + default=D_DEFAULT.get("left"), + store=T, + ) + e_val = get_val( + pr.endChr if pr is not None else None, + default=D_DEFAULT.get("right"), + store=T, + ) + delim = (pr.text if pr is not None else "") + D.format( left=null if not s_val else escape_latex(s_val), - text=c_dict["e"], + text=separator.join(expressions), right=null if not e_val else escape_latex(e_val), ) return delim @@ -288,6 +307,8 @@ def do_func(self, elm): """ c_dict = self.process_children_dict(elm) func_name = c_dict.get("fName") + if func_name is None: + return c_dict.get("e", "") return func_name.replace(FUNC_PLACE, c_dict.get("e")) def do_fname(self, elm): @@ -315,9 +336,12 @@ def do_groupchr(self, elm): the Group-Character object """ c_dict = self.process_children_dict(elm) - pr = c_dict["groupChrPr"] - latex_s = get_val(pr.chr) - return pr.text + latex_s.format(c_dict["e"]) + pr = c_dict.get("groupChrPr") + latex_s = get_val( + pr.chr if pr is not None else None, + default=CHR["\u23df"], + ) + return (pr.text if pr is not None else "") + latex_s.format(c_dict["e"]) def do_rad(self, elm): """ @@ -355,9 +379,10 @@ def do_limlow(self, elm): t_dict = self.process_children_dict(elm, include=("e", "lim")) latex_s = LIM_FUNC.get(t_dict["e"]) if not latex_s: - raise RuntimeError("Not support lim {}".format(t_dict["e"])) - else: - return latex_s.format(lim=t_dict.get("lim")) + return r"\underset{{{lim}}}{{{text}}}".format( + lim=t_dict.get("lim"), text=t_dict["e"] + ) + return latex_s.format(lim=t_dict.get("lim")) def do_limupp(self, elm): """ diff --git a/tests/unit/test_doc2md_omml.py b/tests/unit/test_doc2md_omml.py new file mode 100644 index 00000000000..b4cd62e7448 --- /dev/null +++ b/tests/unit/test_doc2md_omml.py @@ -0,0 +1,94 @@ +# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Regression tests for standalone OMML conversion.""" + +import importlib.util +import sys +import types +from pathlib import Path + +import pytest + +etree = pytest.importorskip("lxml.etree") +pytest.importorskip("pylatexenc") + +MATH_DIR = Path(__file__).resolve().parents[2] / "paddleocr" / "_doc2md" / "math" +OMML_NS = "http://schemas.openxmlformats.org/officeDocument/2006/math" + + +def _load_omml(): + """Load the converter without importing the full PaddleOCR model stack.""" + package = types.ModuleType("test_doc2md_math") + package.__path__ = [str(MATH_DIR)] + sys.modules[package.__name__] = package + spec = importlib.util.spec_from_file_location( + "test_doc2md_math.omml", MATH_DIR / "omml.py" + ) + module = importlib.util.module_from_spec(spec) + sys.modules[spec.name] = module + spec.loader.exec_module(module) + return module + + +@pytest.mark.parametrize( + ("tag", "expected"), + [ + ("acc", r"\hat{x}"), + ("bar", r"\overline{x}"), + ("d", r"\left(x\right)"), + ("groupChr", r"\underbrace{x}"), + ("func", "x"), + ], +) +def test_optional_property_nodes_do_not_drop_formula(tag, expected): + omml = _load_omml() + xml = ( + f'' + "x" + f"" + ) + assert str(omml.oMath2Latex(etree.fromstring(xml))) == expected + + +def test_explicit_bar_position_is_preserved(): + omml = _load_omml() + xml = ( + f'' + '' + "x" + "" + ) + assert str(omml.oMath2Latex(etree.fromstring(xml))) == r"\underline{x}" + + +@pytest.mark.parametrize( + ("base", "expected"), + [ + ("min", r"\min_{n}"), + ("inf", r"\inf_{n}"), + ("sup", r"\sup_{n}"), + ("gcd", r"\gcd_{n}"), + ("foo", r"\underset{n}{foo}"), + ], +) +def test_limit_below_preserves_unsupported_base(base, expected): + omml = _load_omml() + xml = ( + f'' + f"{base}" + "n" + "" + ) + assert str(omml.oMath2Latex(etree.fromstring(xml))) == expected diff --git a/tests/unit/test_doc2md_omml_delimiter.py b/tests/unit/test_doc2md_omml_delimiter.py new file mode 100644 index 00000000000..5e4c6f01f9b --- /dev/null +++ b/tests/unit/test_doc2md_omml_delimiter.py @@ -0,0 +1,64 @@ +# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Regression tests for delimiters with multiple OMML expressions.""" + +import importlib.util +import sys +import types +from pathlib import Path + +import pytest + +etree = pytest.importorskip("lxml.etree") +pytest.importorskip("pylatexenc") + +MATH_DIR = Path(__file__).resolve().parents[2] / "paddleocr" / "_doc2md" / "math" +OMML_NS = "http://schemas.openxmlformats.org/officeDocument/2006/math" + + +def _load_omml(): + package = types.ModuleType("test_doc2md_delimiter_math") + package.__path__ = [str(MATH_DIR)] + sys.modules[package.__name__] = package + spec = importlib.util.spec_from_file_location( + "test_doc2md_delimiter_math.omml", MATH_DIR / "omml.py" + ) + module = importlib.util.module_from_spec(spec) + sys.modules[spec.name] = module + spec.loader.exec_module(module) + return module + + +@pytest.mark.parametrize( + ("values", "separator", "expected"), + [ + (["n", "k"], "|", r"\left(n\middle|k\right)"), + (["a", "b", "c"], ";", r"\left(a;b;c\right)"), + (["n", "k"], None, r"\left(n,k\right)"), + (["x"], "|", r"\left(x\right)"), + ], +) +def test_delimiter_keeps_all_expressions_and_separator(values, separator, expected): + omml = _load_omml() + prop = ( + f'' + if separator is not None + else "" + ) + expressions = "".join( + f"{value}" for value in values + ) + xml = f'{prop}{expressions}' + assert str(omml.oMath2Latex(etree.fromstring(xml))) == expected