# @md2docx/math

> Plugin to convert mathematical expressions in Markdown (MDAST) to DOCX using LaTeX-style syntax. Integrates seamlessly with mdast2docx.

Latest version **0.1.1** (published 2026-10-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install @md2docx/math
pnpm add @md2docx/math
yarn add @md2docx/math
bun add @md2docx/math
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2026-10-22 |
| First published | 2025-03-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 7.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 1 |
| Author | Mayank Kumar Chaudhari |
| Maintainers | mayank1513 |
| Keywords | math, latex, math expressions, math plugin, markdown-to-docx, mdast2docx, markdown, mdast, docx, ms-word, text-processing, export, document generator, typescript, remark, unified, open-source, seamless integration, automation, web-development, rich text, mayank1513 |

## Links

- npm: https://www.npmjs.com/package/@md2docx/math
- Repository: https://github.com/md2docx/md2docx
- Homepage: https://github.com/md2docx/md2docx/blob/main/packages/math/README.md
- Issues: https://github.com/md2docx/md2docx/issues
- Funding: https://github.com/sponsors/md2docx
- npm.io page: https://npm.io/package/@md2docx/math

## Dependencies (1)

- [@m2d/math](https://npm.io/package/@m2d/math.md) 0.1.1

## Alternatives

- [@mdxeditor/editor](https://npm.io/package/@mdxeditor/editor.md) — 962.4K weekly downloads
- [mmdb-lib](https://npm.io/package/mmdb-lib.md) — 680.9K weekly downloads
- [playcanvas](https://npm.io/package/playcanvas.md) — 36.2K weekly downloads
- [@glw907/cairn-cms](https://npm.io/package/@glw907/cairn-cms.md) — 967 weekly downloads
- [markdown-to-confluence](https://npm.io/package/markdown-to-confluence.md) — 103 weekly downloads

## Recent versions

- 0.1.1 (latest) — 2026-10-22
- 0.0.6 — 2025-06-17
- 0.0.5 — 2025-05-20
- 0.0.4 — 2025-04-08
- 0.0.3 — 2025-03-26
- 0.0.0 — 2025-03-26

## README

# @m2d/math <img src="https://raw.githubusercontent.com/mayank1513/mayank1513/main/popper.png" style="height: 40px"/>

<p className="flex gap-2">
  <a href="https://github.com/md2docx/md2docx/actions/workflows/ci.yml" rel="noopener noreferrer">
    <img alt="CI" src="https://github.com/md2docx/md2docx/actions/workflows/ci.yml/badge.svg" />
  </a>
  <a href="https://codecov.io/gh/md2docx/md2docx/tree/main/packages/@m2d/math" rel="noopener noreferrer">
    <img alt="codecov" src="https://codecov.io/gh/md2docx/md2docx/graph/badge.svg?flag=@m2d/math" />
  </a> 
  <a href="https://npmjs.com/package/@m2d/math" rel="noopener noreferrer">
    <img alt="npm version" src="https://img.shields.io/npm/v/@m2d/math" />
  </a>
  <a href="https://npmjs.com/package/@m2d/math" rel="noopener noreferrer">
    <img alt="npm downloads" src="https://img.shields.io/npm/d18m/@m2d/math" />
  </a>
  <a href="https://npmjs.com/package/@m2d/math" rel="noopener noreferrer">
    <img alt="npm bundle size" src="https://img.shields.io/bundlephobia/minzip/@m2d/math" />
  </a>
  <img alt="license" src="https://img.shields.io/npm/l/@m2d/math" />
</p>

> @m2d/math: LaTeX Math to Word DOCX plugin for `@m2d/core`. Transforms inline and block math into native editable Word OpenXML equations.

---

## ✨ Features

- 📐 **Native Word Equations**: Converts LaTeX math into native `docx.Math` structures (`MathRun`, `MathFraction`, `MathRadical`, `MathSuperScript`, `MathSubScript`) — completely editable in Microsoft Word with crisp vector rendering.
- 🔣 **Rich Mathematical Macros**: Comprehensive support for fractions (`\frac`), roots (`\sqrt`), superscripts (`^`), subscripts (`_`), summations (`\sum`), integrals (`\int`), products (`\prod`), Greek letters, and standard mathematical relations.
- 🛡️ **Delimiter Boundary Guards**: Protects against runaway parsing caused by unescaped currency symbols or unclosed delimiters by checking for structural markdown markers.
- ⚙️ **Flexible Error Modes**: Choose between styled inline/block warning badges (`warn`), raw monospace code fallbacks (`raw`), or strict pipeline errors (`throw`).
- ⚡ **Lightweight & Fast**: Zero browser or heavy rendering engine overhead — converts LaTeX AST to Word OpenXML in pure TypeScript.

---

## 📦 Installation


> [!TIP]
> **This package (@md2docx/math) is an official alias of [@m2d/math](https://npmjs.com/package/@m2d/math).**

<details>
<summary>Why does this exist?</summary>

We provide this package to offer shorter import paths and improved discoverability. While both packages provide identical functionality, **@m2d/math** is the primary source of truth.
</details>

<details>
<summary>Which one should I use?</summary>

* **Use @md2docx/math** if you prefer the shorter name or specific branding or ESM Only.
* **Use @m2d/math** for the most stable long-term reference and standard alignment.

| Feature | @m2d/math | @md2docx/math |
| --- | --- | --- |
| **Source Code** | ✅ Primary | 🔗 Proxy |
| **Updates** | Immediate | Synchronized (Immediately) |
| **Bundle Size** | 100% | 100% (Zero overhead) |
| **Format** | ESM + CJS | ESM Only |
| **Maintenance** | ✅ Primary | 🔗 Proxy (inherits) |
| **Security** | ✅ Primary | 🔗 Proxy (inherits) |

</details>

<details>
<summary>Maintenance & Support</summary>

> **Security:** Security audits are performed on the canonical package; this alias inherits all security patches automatically.
</details>

---



```bash
$ pnpm add @m2d/math remark-math @m2d/core docx
```

**_or_**

```bash
$ npm install @m2d/math remark-math @m2d/core docx
```

**_or_**

```bash
$ yarn add @m2d/math remark-math @m2d/core docx
```

---

## 🚀 Quick Start

```typescript
import { toDocx } from "@m2d/core";
import { mathPlugin } from "@m2d/math";
import remarkMath from "remark-math";
import remarkParse from "remark-parse";
import { unified } from "unified";

const markdown = `
# Physics Notes

Einstein's mass-energy equivalence is expressed as $E = mc^2$.

A quadratic formula:

$$
x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}
$$
`;

// 1. Parse markdown with remark-math to generate math AST nodes
const mdast = unified().use(remarkParse).use(remarkMath).parse(markdown);

// 2. Convert to DOCX with the math plugin
const docxBlob = await toDocx(mdast, {}, {
  plugins: [mathPlugin()],
});
```

---

## ⚙️ Plugin Options

```typescript
export interface IMathPluginOptions {
  /**
   * Error handling strategy when LaTeX parsing fails or runaway syntax is detected.
   * - "warn": Emits a styled warning text badge: `⚠️ [Math Syntax Error: ...]`
   * - "raw": Emits the raw formula string as monospace code text
   * - "throw": Re-throws the underlying parse error
   * @default "warn"
   */
  errorMode?: "warn" | "raw" | "throw";

  /**
   * Maximum character length allowed for inline equations.
   * Prevents runaway AST nodes when unclosed dollar signs occur.
   * @default 1000
   */
  maxInlineLength?: number;
}
```

---

## 📚 Deep Dive Guides

- 🔣 **[LaTeX Syntax & Symbols Guide](./docs/latex-syntax-and-symbols.mdx)**: Detailed documentation of supported operators, fractions, radicals, and Greek symbols.
- 🛡️ **[Resiliency & Error Modes Guide](./docs/resiliency-and-error-modes.mdx)**: Error handling strategies, delimiter boundary protection, and runaway length guards.

---

## License

This library is licensed under the MIT open-source license.

<hr />

<p align="center">with 💖 by <a href="https://mayankchaudhari.com" target="_blank">Mayank Kumar Chaudhari</a></p>

---
_Source: https://npm.io/package/@md2docx/math · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
