markitdown icon indicating copy to clipboard operation
markitdown copied to clipboard

DOCX math: a boxed formula (`<m:borderBox>`) and everything inside it are dropped

Open Zhuoxi2000 opened this issue 1 month ago • 1 comments

When a Word equation contains a border box (<m:borderBox>), MarkItDown drops the element and all of its content without any warning. An equation that is boxed as a whole converts to an empty $$$$.

Word writes <m:borderBox> for its "Boxed Formula" equation structure.

Repro

from xml.etree import ElementTree as ET
from markitdown.converter_utils.docx.math.omml import OMML_NS, oMath2Latex

M = f'xmlns:m="{OMML_NS[1:-1]}"'

def latex(xml):
    return oMath2Latex(ET.fromstring(f"<m:oMath {M}>{xml}</m:oMath>")).latex

e_mc2 = ("<m:r><m:t>E=m</m:t></m:r><m:sSup><m:e><m:r><m:t>c</m:t></m:r></m:e>"
         "<m:sup><m:r><m:t>2</m:t></m:r></m:sup></m:sSup>")

print(repr(latex(f"<m:borderBox><m:borderBoxPr><m:ctrlPr/></m:borderBoxPr>"
                 f"<m:e>{e_mc2}</m:e></m:borderBox>")))
# -> ''

print(repr(latex("<m:r><m:t>a+</m:t></m:r>"
                 "<m:borderBox><m:e><m:r><m:t>b</m:t></m:r></m:e></m:borderBox>"
                 "<m:r><m:t>=c</m:t></m:r>")))
# -> 'a+=c'

End to end: I added a paragraph with a boxed E=mc^2 to a copy of tests/test_files/equations.docx, placed between the paragraphs BEFORE and AFTER. MarkItDown().convert() returns BEFORE\n\n$$$$\n\nAFTER.

Expected vs actual

Input Expected Actual
<m:borderBox> around E=mc^2 \boxed{E=mc^{2}} ''
a+ <m:borderBox>b</m:borderBox> =c a+\boxed{b}=c a+=c

ISO/IEC 29500-1 says borderBox (Border-Box Object) "specifies the Border Box object, consisting of a border drawn around an instance of mathematical text (such as a formula or equation)". Its children are an optional borderBoxPr and one e that holds ordinary math content. When borderBoxPr is omitted, the default is a full rectangular border. The Open XML SDK BorderBox reference reproduces this text. Dropping the e content loses document text silently. At minimum the content should be kept, and \boxed{...} is the direct LaTeX equivalent of a full border.

Cause

In packages/markitdown/src/markitdown/converter_utils/docx/math/omml.py, oMath2Latex.tag2meth has no entry for borderBox, and __direct_tags lists box but not borderBox. process_unknow returns None for the tag, so process_children_list skips the element together with its children.

Suggested fix

Add a do_borderbox handler that renders process_children_dict(elm).get("e", "") as \boxed{...}, with the template in latex_dict.py. It must not require <m:borderBoxPr>. The hide* and strike* flags in borderBoxPr (partial borders, strike lines) have no direct LaTeX equivalent, so ignoring them and emitting \boxed{} covers Word's default "Boxed Formula".

Environment

  • markitdown main at 1f9530a (0.1.8)
  • Python 3.13, macOS

I have a fix and a regression test ready on a branch and will open the PR for this myself.

Zhuoxi2000 avatar Oct 04 '26 18:10 Zhuoxi2000

I'd like to take this one myself: I have a fix and a regression test ready and will open the PR shortly, linking back to this issue.

Zhuoxi2000 avatar Oct 04 '26 18:10 Zhuoxi2000