Quick answer
Install latex-to-docx and docx, then call latexToWordMath(String.raw`\frac{a}{b}`): it returns
native Word equations (OMML) that you put in a Paragraph. Pass the formula without $ or \( \)
delimiters; multi-line aligned blocks return one equation per line.
Why Word equations, not images or text
Word stores equations as OMML (Office Math Markup Language) inside the .docx. Only OMML gives
your users equations they can click, edit and resize — screenshots of formulas can't be edited, and raw LaTeX in a document is
unreadable for most people. The popular docx library can write
OMML, but you have to build every fraction and superscript by hand. latex-to-docx does that for you: one function call
turns a LaTeX string into native Word equations.
Install
npm install latex-to-docx docx
It is an ES module with TypeScript types, has no other dependencies and works in Node.js and in the browser.
Your first equation (Node.js)
import { writeFileSync } from 'node:fs';
import { Document, Packer, Paragraph, TextRun } from 'docx';
import { latexToWordMath } from 'latex-to-docx';
const [equation] = latexToWordMath(String.raw`x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}`);
const doc = new Document({
sections: [{
children: [
new Paragraph({ children: [new TextRun('The quadratic formula:')] }),
new Paragraph({ children: [equation] }),
],
}],
});
writeFileSync('formula.docx', await Packer.toBuffer(doc));
Pass the formula without its delimiters (\( \), \[ \], $, $$).
Use String.raw or double backslashes so JavaScript keeps the backslashes. The function returns an array:
usually one equation, but multi-line aligned blocks give one equation per line.
Inline equations inside a sentence
An equation is a normal paragraph child, so you can mix it with text runs:
new Paragraph({
children: [
new TextRun('Einstein showed that '),
...latexToWordMath('E = mc^2'),
new TextRun(' for a body at rest.'),
],
});
Text with $…$ and \(…\) mixed in
AI answers and Markdown put formulas inside normal text. This helper splits a line and converts only the math:
const MATH = /(\\\(.+?\\\)|\$[^$\n]+?\$)/g;
function paragraphWithMath(line) {
const children = line.split(MATH).flatMap((part) => {
const m = part.match(/^\\\((.+)\\\)$|^\$(.+)\$$/);
if (m) return latexToWordMath(m[1] ?? m[2]);
return part ? [new TextRun(part)] : [];
});
return new Paragraph({ children });
}
paragraphWithMath(String.raw`The area is \(\pi r^2\) and the volume $\frac{4}{3}\pi r^3$.`);
For full Markdown (headings, tables, lists, code), parse it with a Markdown library such as marked and call
latexToWordMath for its math tokens — that is exactly how our ChatGPT to Word converter works.
Watch out for prices like "$5 and $10": only treat $…$ as math when the content looks like a formula.
In the browser
import { Document, Packer, Paragraph } from 'docx';
import { latexToWordMath } from 'latex-to-docx';
const doc = new Document({
sections: [{ children: latexToWordMath(String.raw`\sum_{i=1}^{n} i = \frac{n(n+1)}{2}`).map((eq) => new Paragraph({ children: [eq] })) }],
});
const blob = await Packer.toBlob(doc);
const a = Object.assign(document.createElement('a'), { href: URL.createObjectURL(blob), download: 'math.docx' });
a.click();
Everything runs client-side, so you can offer Word export without sending your users' text to a server.
Multi-line derivations
const lines = latexToWordMath(String.raw`\begin{aligned} (a+b)^2 &= (a+b)(a+b) \\ &= a^2 + 2ab + b^2 \end{aligned}`);
// lines.length === 2 — put each in its own Paragraph
const paragraphs = lines.map((eq) => new Paragraph({ children: [eq] }));
What is supported
| LaTeX | Becomes in Word |
|---|---|
\frac, \dfrac, \tfrac | Fractions |
x^2, x_i, x_i^2 | Super- and subscripts |
\sqrt{x}, \sqrt[n]{x} | Radicals |
\sum, \prod, \int, \oint with limits | N-ary operators |
\lim_{x \to 0} | Limits |
\left( … \right) and other brackets | Growing brackets |
\binom{n}{k}, \sin, \log, \operatorname{…} | Binomials and function names |
Greek letters, \le, \ne, \infty, \to, \in, … | Math symbols |
\hat, \vec, \bar, \mathbb{R}, \text{…} | Accents, double-struck letters, text |
aligned, gathered | One equation per line |
Unknown commands never throw: they are kept as text inside the equation, so a document always gets generated. Not supported
(yet): real two-dimensional matrices (they are written as a row in brackets), \overbrace/\underbrace,
colours and custom macros.
Testing the output
- Unzip the .docx and look for
<m:oMath>elements inword/document.xml— every equation is one. - Open it in Word, LibreOffice or Google Docs: equations stay editable in all three.
- The package ships a test (
npm test) that builds a document with typical AI formulas.
Links
- npm: latex-to-docx
- Source and issues: github.com/Diego-Media/latex-to-docx (MIT licence)
- Not a developer? Convert AI answers without code in ChatGPT to Word, or read how to copy ChatGPT math to Word.
Frequently asked questions
How do I add equations to a Word document in JavaScript?
Use the docx library to build the document and latex-to-docx to turn each LaTeX formula into a native Word equation (OMML) that you add to a Paragraph.
Does it work in the browser?
Yes. It has no Node.js dependencies; combine it with docx Packer.toBlob to offer a download.
What is OMML?
Office Math Markup Language, the format Word uses for editable equations inside a .docx file.
What happens with LaTeX it does not understand?
It never throws: unknown commands are kept as text inside the equation, so the document is always created.
Is it free for commercial use?
Yes, it is MIT licensed.