DOCX: equations get `\_` and `\*` in their LaTeX (`$\sum\_{i=1}^{n}x\_{i}$`)
メンテナーはふだん 1 日以内に返信
評価
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- Python
- スター
- 198k
- フォーク
- 15k
- 平均マージ
- 2時間 16分
- マージ済み PR(30日)
- 9
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートなし
- コントリビューションガイドなし
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
microsoft/markitdown のほかの issue
-
IpynbConverter uses a comment inside a Markdown code fence as the notebook title対応中かも @Primaverah が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
microsoft/markitdown#2964 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
PPTX image alt text ending in a backslash breaks the Markdown image対応中かも @dongyikuan919 が 3 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
microsoft/markitdown#2943 ·
メンテナーはふだん 1 日以内に返信
-
PPTX chart labels with pipes or line breaks produce malformed Markdown tables対応中かも @dongyikuan919 が 5 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 92/100
microsoft/markitdown#2937 ·
メンテナーはふだん 1 日以内に返信
-
DOCX: math pre-processing re-parses every document.xml even without equations (32 s -> 17.5 s on a 12.5 MB document)対応中かも @houyunsun が 26 日前に担当しました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 89/100
microsoft/markitdown#2834 ·
メンテナーはふだん 1 日以内に返信
-
URL fetch sends default python-requests User-Agent, Wikipedia returns 403対応中かも @sushan5140 が 31 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
microsoft/markitdown#2820 ·
メンテナーはふだん 1 日以内に返信
microsoft/markitdown の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
hugohe3/ppt-master#366 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 89/100
メンテナーはふだん 2 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
521xueweihan/HelloGitHub#4716 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 84/100
pcqpcq/open-source-android-apps#619 ·
メンテナーはふだん 1 日以内に返信
-
contribution Windows 主机
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
Lee-xeo/Claude-Cage#1519 ·
メンテナーはふだん 1 日以内に返信