AfterMath converts LaTeX placeholders inside .docx files into editable Word OMML equations - without MathType, OLE objects, VBA macros, or manual clicking.
AfterMath 可以把 .docx 里的 LaTeX 公式占位符批量转换成 Word 原生可编辑公式,不需要 MathType、不生成 OLE、不使用宏,也不需要手动点击。
No macros. No OLE. No proprietary equation objects. No ritual clicking.
Open formats first. Legacy objects preserved, not worshipped.
AfterMath is an open-source compatibility layer for escaping legacy equation workflows in Word documents. Its core path patches .docx files by converting supported LaTeX placeholders into native Word OMML equations.
It also supports MathType-compatible workflows through inspection, preservation, preview extraction, source-mapped OMML generation, semantic OMML reconstruction, and an optional local MathType UI / clipboard bridge.
AfterMath does not reverse-engineer MathType private binaries, does not generate fake proprietary OLE objects, and does not bundle MathType.
- Patch DOCX placeholders:
$$...$$,\[...\], and{{eq: ...}}. - Convert a practical subset of LaTeX into editable Word OMML.
- Preserve ordinary text, headings, lists, paragraphs, and common equation-number paragraphs.
- Keep failed formulas as source text by default and report the failure.
- Inspect existing DOCX files for placeholders, OMML equations, OLE objects, and MathType-like objects.
- Preserve OLE-heavy documents when no supported placeholder exists.
- Extract preview images from legacy OLE / MathType objects for manual review.
- Best-effort migrate legacy objects only when public source or a user mapping is available.
- Generate open
mathtype-compatibleoutput as OMML plus source metadata. - Reverse Word OMML into EquationIR, Presentation MathML, and normalized LaTeX.
- Optionally drive a locally installed Word + MathType UI to import public source through the clipboard and generate real non-empty MathType OLE objects.
AfterMath must never corrupt a DOCX.
- The input file is never overwritten.
- No-placeholder OLE-heavy documents are not rewritten by default.
--copy-if-noopcreates a byte-for-byte copy when a no-op output is explicitly requested.- Private MathType / OLE binaries are preserved unless a safe public-source migration is explicitly requested.
- Preview images are never reported as editable equations.
- A blank
Equation.DSMT4object is not a successful conversion. - Full MathType UI conversion is blocked until
generate-one-uiproves source import and non-empty object generation.
AfterMath core requires Python 3.10+ and has no heavy runtime dependency.
cd AfterMath
py -m pip install -e .
aftermath --helpWindows fallback launchers are included:
.\aftermath.cmd --help
.\aftermath.ps1 --helpPatch the 20-formula demo:
aftermath patch examples/demo_20_formulas.docx --out examples/output/demo_20_formulas.out.docxExpected summary:
{"total_latex_blocks": 20, "converted_count": 20, "failed_count": 0}Open examples/output/demo_20_formulas.out.docx in Microsoft Word. The formulas should be native editable Word equations, not images.
Run from the repository root:
py -m pip install -e .
aftermath --help
aftermath patch examples/demo_20_formulas.docx --out examples/output/demo_20_formulas.out.docx
aftermath inspect examples/demo_20_formulas.docx --report examples/output/inspect.report.json
aftermath inspect-mathtype tests/fixtures/mathtype_like_synthetic.docx --report examples/output/mathtype_inventory.report.json
aftermath extract-preview tests/fixtures/mathtype_like_synthetic.docx --out-dir examples/output/previews
aftermath patch tests/fixtures/latex_placeholders.docx --out examples/output/mathtype_compatible.out.docx --target mathtype-compatible
aftermath extract-source examples/output/mathtype_compatible.out.docx --out examples/output/extracted_sources.json
aftermath roundtrip examples/output/mathtype_compatible.out.docx --out examples/output/roundtrip.md
py -m unittest discover -s tests -vConvert one formula:
aftermath convert --latex "\frac{a}{b}" --to omml --out examples/output/formula.xmlPatch a DOCX:
aftermath patch input.docx --out output.docx
aftermath patch input.docx --out output.docx --strict
aftermath patch input.docx --out output.docx --copy-if-noopGenerate open MathType-compatible output with source metadata:
aftermath patch input.docx --out output.docx --target mathtype-compatible
aftermath extract-source output.docx --out equations.json
aftermath roundtrip output.docx --out recovered_latex.mdInspect and inventory legacy MathType / OLE documents:
aftermath inspect input.docx --report report.json --with-word-com
aftermath inspect-mathtype input.docx --report inventory.json
aftermath inventory input.docx --report inventory.md
aftermath extract-preview input.docx --out-dir extracted/Best-effort migration from public source or a mapping file:
aftermath migrate-mathtype input.docx --out output.docx --to omml --mapping equations.csvMapping format:
object_index,latex
1,E = mc^2
2,\frac{a}{b}Reverse Word OMML into semantic source:
aftermath reverse extract-omml input.docx --out omml_inventory.json
aftermath reverse omml-to-ir input.docx --out equations_ir.json
aftermath mathtype validate-batch equations_ir.json --out validation_report.jsonOptional MathType UI / clipboard bridge:
aftermath mathtype ui-probe --out ui_probe.report.json
aftermath mathtype clipboard-probe --latex "\frac{a}{b}" --out clipboard_probe.report.json
aftermath mathtype generate-one-ui --latex "\frac{a}{b}" --out test.docx --report test.report.json
aftermath mathtype verify-output test.docx --report verify.report.json
aftermath mathtype semantic-convert-doc input.docx --out output.docx --engine ui-clipboard --max-equations 1
aftermath mathtype repair-output partial.docx --out repaired.docx --equations 46,51Run --max-equations 1, then 3, then 10, then full conversion only after verification passes.
repair-output is for partial UI-bridge outputs. It reads the previous semantic report, maps original equation indexes to the remaining OMath objects, and tries range-safe replacement without touching already converted MathType objects.
AfterMath supports legacy MathType documents in three layers:
- Preserve and inspect existing MathType / OLE objects.
- Generate open, editable, MathType-compatible equation packages based on OMML plus source metadata.
- Optionally use a locally installed Microsoft Word + MathType environment to create real MathType objects through official UI behavior.
The third layer is Windows-only and environment-dependent. It drives the user's installed applications; it does not implement MathType's private binary format.
AfterMath supports MathType-compatible workflows through inspection, preservation, best-effort migration, and open OMML + source-map generation. It does not reverse-engineer MathType private binaries or generate fake proprietary OLE objects.
AfterMath 支持 MathType 兼容工作流,包括检测、保留、预览导出、有限迁移,以及基于 OMML + source map 的开放兼容公式生成。它不逆向 MathType 私有二进制格式,也不伪造专有 OLE 对象。
patch writes JSON and Markdown reports. If --report is not provided, reports are created next to the output DOCX:
output.report.json
output.report.md
Important fields include:
input_pathoutput_pathtotal_latex_blocksconverted_countfailed_countfallback_countdetected_omml_count_beforedetected_ole_or_mathtype_like_countole_object_countmathtype_like_countembedded_binary_partshas_olehas_mathtype_likepatch_safety_levelpatch_actionper_formula_records
See docs/report_schema.md for details.
Example documents live in examples/:
demo_20_formulas.docxsimple_latex.docxnumbered_equations.docxthesis_style_equations.docxmixed_text_math.docx
Synthetic regression fixtures live in tests/fixtures/. They are generated test documents, not real thesis, client, or production documents.
- docs/design.md: why OMML, no OLE by default, no macros, and DOCX patching first.
- docs/limitations.md: current boundaries.
- docs/report_schema.md: JSON report fields.
- docs/mathtype_compatibility.md: compatibility layers and roadmap.
- docs/source_map.md: source metadata and roundtrip design.
- docs/official_adapter.md: optional official bridge rules.
- docs/mathtype_official_adapter.md: official adapter probe and failure behavior.
- docs/semantic_reverse_reconstruction.md: OMML to EquationIR and validation gates.
- docs/mathtype_ui_bridge.md: UI / clipboard source import bridge.
AfterMath does not support:
- full LaTeX macro package compatibility
- MathType private binary parsing
- fake MathType OLE generation
- bundled MathType runtime
- proprietary object cloning
- guaranteed OMML to MathType conversion on every MathType version
- GUI editing
- a Word plugin
- complex cross-reference repair
- complete thesis layout automation
MIT. See LICENSE.