Skip to content

axobase001/aftermath

Repository files navigation

AfterMath

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.

What It Is

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.

Current Capabilities

  • 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-compatible output 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.

Safety

AfterMath must never corrupt a DOCX.

  • The input file is never overwritten.
  • No-placeholder OLE-heavy documents are not rewritten by default.
  • --copy-if-noop creates 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.DSMT4 object is not a successful conversion.
  • Full MathType UI conversion is blocked until generate-one-ui proves source import and non-empty object generation.

Install

AfterMath core requires Python 3.10+ and has no heavy runtime dependency.

cd AfterMath
py -m pip install -e .
aftermath --help

Windows fallback launchers are included:

.\aftermath.cmd --help
.\aftermath.ps1 --help

Quick Start

Patch the 20-formula demo:

aftermath patch examples/demo_20_formulas.docx --out examples/output/demo_20_formulas.out.docx

Expected 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.

Smoke Test

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 -v

CLI

Convert one formula:

aftermath convert --latex "\frac{a}{b}" --to omml --out examples/output/formula.xml

Patch 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-noop

Generate 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.md

Inspect 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.csv

Mapping 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.json

Optional 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,51

Run --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.

MathType Compatibility

AfterMath supports legacy MathType documents in three layers:

  1. Preserve and inspect existing MathType / OLE objects.
  2. Generate open, editable, MathType-compatible equation packages based on OMML plus source metadata.
  3. 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 对象。

Reports

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_path
  • output_path
  • total_latex_blocks
  • converted_count
  • failed_count
  • fallback_count
  • detected_omml_count_before
  • detected_ole_or_mathtype_like_count
  • ole_object_count
  • mathtype_like_count
  • embedded_binary_parts
  • has_ole
  • has_mathtype_like
  • patch_safety_level
  • patch_action
  • per_formula_records

See docs/report_schema.md for details.

Examples

Example documents live in examples/:

  • demo_20_formulas.docx
  • simple_latex.docx
  • numbered_equations.docx
  • thesis_style_equations.docx
  • mixed_text_math.docx

Synthetic regression fixtures live in tests/fixtures/. They are generated test documents, not real thesis, client, or production documents.

Documentation

Current Limits

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

License

MIT. See LICENSE.

About

Convert LaTeX placeholders inside .docx files into editable Word OMML equations — no MathType, no OLE, no macros, no ritual clicking.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages