Skip to content

math

math

The Math proxy, giving access to the equations in a document.

Math

Math(oMath: CT_OMath, parent: ProvidesStoryPart)

Bases: StoryChild

Proxy for an m:oMath element, one equation.

Reached through Paragraph.math or Document.math.

Word stores equations in OMML, a notation of its own with no overlap with the wordprocessing run content. This exposes the equation's XML and the characters in it; it deliberately does not model the notation, which would be a substantially larger piece of work with nothing to render the result.

Source code in src/docx/math.py
def __init__(self, oMath: CT_OMath, parent: t.ProvidesStoryPart):
    super().__init__(parent)
    self._element = oMath
    self._oMath = oMath

text property

text: str

The characters of this equation, with none of its structure.

A fraction reads as its numerator followed by its denominator, a superscript as its base followed by its exponent. This is what a plain-text extraction can offer; it is not a rendering of the equation and will not round-trip.

xml property

xml: str

The OMML source of this equation.

The reliable representation, and what to hand to anything that understands OMML — an XSLT to MathML, say.

is_display property

is_display: bool

True when this equation is displayed on a line of its own.

Word wraps a display equation in an m:oMathPara; an inline one sits directly among the runs of its paragraph.

iter_math

iter_math(
    element: BaseOxmlElement, parent: ProvidesStoryPart
) -> Iterator[Math]

Generate a Math for each equation under element, in document order.

Both the inline m:oMath and the m:oMath children of a display m:oMathPara are found, and each is yielded once.

Source code in src/docx/math.py
def iter_math(element: BaseOxmlElement, parent: t.ProvidesStoryPart) -> Iterator[Math]:
    """Generate a |Math| for each equation under `element`, in document order.

    Both the inline `m:oMath` and the `m:oMath` children of a display `m:oMathPara` are
    found, and each is yielded once.
    """
    for oMath in element.iter(qn("m:oMath")):
        yield Math(oMath, parent)  # pyright: ignore[reportArgumentType]

math_list

math_list(
    element: BaseOxmlElement, parent: ProvidesStoryPart
) -> List[Math]

The equations under element, in document order.

Source code in src/docx/math.py
def math_list(element: BaseOxmlElement, parent: t.ProvidesStoryPart) -> List[Math]:
    """The equations under `element`, in document order."""
    return list(iter_math(element, parent))