Skip to content

Latest commit

 

History

History
106 lines (86 loc) · 1.73 KB

README.md

File metadata and controls

106 lines (86 loc) · 1.73 KB

docx-math-converter

npm version

Converting LaTeX, MathML, or OMML strings to math objects using docx.js. Works for Node and on the Browser.

API

convertMathMl2Math

import { convertMathMl2Math } from 'docx-math-converter';

const mathObj = convertMathMl2Math(`
  <math display="block">
    <mrow>
      <mfrac>
        <mi>a</mi>
        <mi>b</mi>
      </mfrac>
      <mo>=</mo>
      <mfrac>
        <mrow>
          <mi>a</mi>
          <mo>×</mo>
          <mi>m</mi>
        </mrow>
        <mrow>
          <mi>b</mi>
          <mo>×</mo>
          <mi>m</mi>
        </mrow>
      </mfrac>
    </mrow>
  </math>
`);

convertOmml2Math

import { convertOmml2Math } from 'docx-math-converter';

const mathObj = convertOmml2Math(`
  <m:oMath xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
    <m:rad>
      <m:deg>
        <m:r>
          <m:t>3</m:t>
        </m:r>
      </m:deg>
      <m:e>
        <m:r>
          <m:t>1234</m:t>
        </m:r>
      </m:e>
    </m:rad>
  </m:oMath>
`);

convertLatex2Math

import { mathJaxReady, convertLatex2Math } from 'docx-math-converter';

await mathJaxReady();
const mathObj = convertLatex2Math('(a\\pm b)^2=a^2\\pm2ab+b^2');

required

NodeJS version

"node": ">=16.17.0"

Installing docx dependence require:

"docx": "^8.5.0"

Use

const doc = new Document({
  sections: [
    {
      properties: {},
      children: [
        new Paragraph({
          children: [
            mathObj, // include mathObj
          ],
        }),
      ],
    },
  ],
});

Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("My Document.docx", buffer);
});