Skip to content

Math equations ​

latexToOmml and mathmlToOmml from pptx-ts/math convert an equation to OMML, the markup PowerPoint's equation editor stores. Pass the result to the math option of a text item and the slide gets an equation you can edit in PowerPoint.

ts
import TsPptx from 'pptx-ts'
import { latexToOmml } from 'pptx-ts/math'

const pptx = new TsPptx()
const slide = pptx.addSlide()

slide.addText([{ math: latexToOmml('x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}') }], { x: 1, y: 2, w: 8, h: 1 })

await pptx.writeFile({ fileName: 'quadratic.pptx' })

A math item ignores any text on the same item. Without inline: true it is also its own centered paragraph.

Install the converters ​

The two converters are optional peer dependencies, so a project that never writes math does not carry them. Install both to use pptx-ts/math:

sh
npm install temml mathml2omml
PackageConvertsLicense
temmlLaTeX to MathMLMIT
mathml2ommlMathML to OMMLLGPL-3.0-or-later

mathml2omml is never bundled into this package's output. It stays a separate, replaceable package in your node_modules. A project whose policy rules out LGPL code can leave it uninstalled and pass OMML to math directly.

pptx-ts/math runs only under Node, because it loads the converters synchronously through Node's createRequire. The OMML it returns is a plain string: store it, and pass it to math in any runtime.

Functions at a glance ​

FunctionOptionReturnsUsable inline
latexToOmml(latex)display omitted or trueA centered display block, <m:oMathPara>Prefer display: false. With inline: true the block's centering wrapper is dropped.
latexToOmml(latex, { display: false })display: falseA bare equation, <m:oMath>, rendered in inline modeYes
mathmlToOmml(mathml)noneA bare equation, <m:oMath>Yes

Neither function puts namespace declarations in its result: the math option declares them.

The text item takes the result through two options:

OptionTypeDefaultEffect
mathstringnoneOMML: a whole <m:oMathPara>, a whole <m:oMath>, or the contents of one.
inlinebooleanfalseFlow the equation between the neighbouring text runs instead of giving it its own centered paragraph. No effect without math.

Flow an equation inside a sentence ​

Set inline: true on the item and give it the bare form:

ts
slide.addText(
  [
    { text: 'where ' },
    { math: latexToOmml('x^2+1=y', { display: false }), inline: true },
    { text: ' holds' },
  ],
  { x: 1, y: 1, w: 8, h: 1 }
)

The equation shares one paragraph with the text on either side of it.

Convert MathML ​

ts
import { mathmlToOmml } from 'pptx-ts/math'

slide.addText(
  [{ math: mathmlToOmml('<math><msup><mi>e</mi><mi>x</mi></msup></math>') }],
  { x: 1, y: 3, w: 8, h: 1 }
)

mathmlToOmml converts your MathML as written. An <mover> becomes an accent only when it states accent="true", and without it the <mover> becomes an upper limit.

Handle invalid LaTeX ​

latexToOmml throws on LaTeX that temml cannot parse, rather than returning a broken equation. The message carries temml's parse position when temml reports one, as in Invalid LaTeX (position 6): .... Catch it when the input comes from users:

ts
import { InvalidOptionError, latexToOmml } from 'pptx-ts/math'

function toOmml(latex: string): string | null {
  try {
    return latexToOmml(latex)
  } catch (err) {
    if (err instanceof InvalidOptionError && err.code === 'math/invalid-latex') return null
    throw err
  }
}

What each LaTeX construct becomes ​

latexToOmml maps these constructs to these PowerPoint equation objects:

LaTeXEquation object
\fracFraction (m:f)
\sqrt, \sqrt[n]Radical (m:rad)
\sum, \intN-ary operator (m:nary)
x^2, x_i, x_i^2Superscript, subscript, both (m:sSup, m:sSub, m:sSubSup)
\lim_{x \to 0}Subscript (m:sSub), not a lower limit
pmatrix, casesMatrix (m:m)
\hat, \^, \tilde, \~, \acute, \', \grave, \`, \ddot, \", \dot, \., \bar, \=, \breve, \u, \check, \v, \mathring, \r, \H, \vec, \dddotAccent (m:acc), carrying the combining mark Word writes
\widehat, \overrightarrow, \overgroup, \overbrace, \underbraceGroup character (m:groupChr)
\overline, \underlineBorder box (m:borderBox)
\stackrel, \xrightarrowUpper limit (m:limUpp)
\utilde and other under-accentsLower limit (m:limLow). OMML has no accent object that sits below its base.
\ddddotUpper limit (m:limUpp). Its four-dot mark is two characters, and an accent holds one.
\left( x \right)Plain bracket characters, not a delimiter object

The accent rows describe latexToOmml. mathmlToOmml follows the accent attribute in your MathML instead.

Invalid input ​

ConditionWarns or throwsCode
LaTeX temml cannot parse, such as \frac{ or an unknown commandthrowsInvalidOptionError math/invalid-latex
temml or mathml2omml is not installedthrows on the first callUnsupportedFeatureError math/missing-optional-peer
An empty LaTeX stringreturns an empty equation, no errornone
A string with no <math> element, or MathML that converts to no equation, passed to mathmlToOmmlthrowsInvalidOptionError math/invalid-mathml

Limits ​

  • The converters run only under Node. The OMML string they return works anywhere.
  • LaTeX goes to temml as given: no custom macros, no \usepackage, and no environment temml does not support.
  • An equation needs a reader that understands PowerPoint 2010's a14 extension. There is no image fallback, and a reader without the extension skips the whole text box.
  • Beyond the table above, how an equation looks is up to temml and mathml2omml.

See also ​