DOCX: equations get `\_` and `\*` in their LaTeX (`$\sum\_{i=1}^{n}x\_{i}$`)

Aperta
#2,744 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@pujitha24 ci sta già lavorando.

Dal 23/10/2026.

  • #2745 di @pujitha24 — aperta
  • #2755 di @OUAO-FRANK — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
68/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Start with packages/markitdown/src/markitdown/converter_utils/docx/pre_process.py, where _get_omath_tag_replacement inserts equation text, and trace how _CustomMarkdownify.convert_soup handles it. Reproduce with the supplied math.py example and compare inline and display output. Done means LaTeX underscores and asterisks remain unescaped in both equation forms while ordinary DOCX prose still converts correctly.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

MarkItDown converts OMML equations to LaTeX and inserts the result into the document as text before mammoth runs. markdownify then escapes _ and * in that text as it would in prose. So every subscript in an equation becomes \_ and every * becomes \*. This happens in inline (m:oMath) and display (m:oMathPara) equations. In LaTeX, \_ is a literal underscore, so x\_{i} no longer means x subscript i.

Reproduction

math.py writes one paragraph with an inline equation and one with a display equation, both the sum of x_i for i = 1 to n:

import zipfile

W = 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"'
M = 'xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"'
REL = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
SUM = (
    '<m:nary><m:naryPr><m:chr m:val="\u2211"/></m:naryPr>'
    "<m:sub><m:r><m:t>i=1</m:t></m:r></m:sub><m:sup><m:r><m:t>n</m:t></m:r></m:sup>"
    "<m:e><m:sSub><m:e><m:r><m:t>x</m:t></m:r></m:e><m:sub><m:r><m:t>i</m:t></m:r></m:sub></m:sSub></m:e></m:nary>"
)
body = (
    f"<w:p><w:r><w:t xml:space=\"preserve\">Inline: </w:t></w:r><m:oMath>{SUM}</m:oMath></w:p>"
    f"<w:p><m:oMathPara><m:oMath>{SUM}</m:oMath></m:oMathPara></w:p>"
)
with zipfile.ZipFile("math.docx", "w") as z:
    z.writestr(
        "[Content_Types].xml",
        '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">'
        '<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>'
        '<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>'
        "</Types>",
    )
    z.writestr(
        "_rels/.rels",
        '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
        f'<Relationship Id="rId1" Type="{REL}/officeDocument" Target="word/document.xml"/>'
        "</Relationships>",
    )
    z.writestr("word/document.xml", f"<w:document {W} {M}><w:body>{body}</w:body></w:document>")
python math.py
markitdown math.docx
Actual output
Inline: $\sum\_{i=1}^{n}x\_{i}$

$$\sum\_{i=1}^{n}x\_{i}$$

With <m:t>a*b</m:t> in place of <m:t>x</m:t>, the output contains a\*b.

Expected output
Inline: $\sum_{i=1}^{n}x_{i}$

$$\sum_{i=1}^{n}x_{i}$$

Versions

markitdown 0.1.9 (markitdown[docx]), mammoth 1.13.0, markdownify 1.2.3, Python 3.14, Linux x86_64. main (7482c09) has no changes to this code since 0.1.9.

Cause

_get_omath_tag_replacement replaces each equation with a plain w:r/w:t run holding $…$ or $$…$$. mammoth outputs it as ordinary text, and markdownify's escape() (with the default escape_underscores and escape_asterisks) escapes it during _CustomMarkdownify.convert_soup. A possible fix: mark the inserted runs so that the HTML-to-Markdown step can write their text unescaped, or replace them with placeholders that are substituted after markdownify.

Related

  • PR #1924 ("fix: preserve underscores in LaTeX math blocks during DOCX conversion") fixed this bug by unescaping inside $…$ after markdownify. Its author closed it unmerged on 2026-10-05, and no issue tracked the bug.
  • #1512 (open): equations missing entirely (older version).
  • #2573, #2610 (open) and PRs #2368, #2445, #2574, #2602, #2714 (open): other OMML conversion problems (dropped arguments, dropped equations). None changes the escaping.
  • #2157 (open): characters that are not escaped in DOCX text.

Drafted with the help of an AI assistant; every reproduction was run and its output checked.

Lingua principale
Python
Stelle
198k
Fork
15k
Merge medio
2h 16m
PR unite (30g)
9

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

  • Include un Dockerfile o un file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di microsoft/markitdown

Tutte le issue di microsoft/markitdown

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.