Files
docx-js/docs/usage/math.md
2020-10-13 01:23:27 +01:00

2.5 KiB

Math

!> Math requires an understanding of Sections and Paragraphs.

Intro

  • To add math, create a Math object
  • Add MathComponents inside Math
  • MathComponents can have nested MathComponents inside. e.g. A fraction where the numerator is a square root, and the demoninator as another fraction. More on MathComponents below

Example

new Math({
    children: [
        new MathRun("2+2"),
        new MathFraction({
            numerator: [new MathRun("hi")],
            denominator: [new MathRun("2")],
        }),
    ],
}),

This will produce:

clippy the assistant

Math Components

MathComponents are the unit sized building blocks of an equation in docx. A MathComponent takes in more nested MathComponents until you reach MathRun, which has no children. MathRun is similar to a TextRun.

Math Run

MathRun is the most basic MathComponent.

Example

new MathRun("2+2");
new MathRun("hello");

An example of it being used inside Math:

new Math({
    children: [
        new MathRun("2"),
        new MathRun("+"),
        new MathRun("2"),
    ],
}),

Math Fraction

MathFractions require a numerator and a demoninator, which are both a list of MathComponents

Example

new MathFraction({
    numerator: [new MathRun("1")],
    denominator: [new MathRun("2")],
}),
new MathFraction({
    numerator: [
        new MathRun("1"),
        new MathRadical({
            child: [new MathRun("2")],
        }),
    ],
    denominator: [new MathRun("2")],
}),

An example of it being used inside Math:

new Math({
    children: [
        new MathFraction({
            numerator: [new MathRun("1")],
            denominator: [new MathRun("2")],
        }),
        new MathText("+"),
        new MathFraction({
            numerator: [new MathRun("1")],
            denominator: [new MathRun("2")],
        }),
        new MathText("= 1"),
    ],
}),

Sum

A MathComponent for Σ. It can take a superScript and/or subScript as arguments to add MathComponents (usually limits) on the top and bottom

new MathSum({
    child: [new MathRun("i")],
}),
new MathSum({
    child: [
        new MathSuperScript({
            child: new MathRun("e"),
            superScript: new MathRun("2"),
        })
    ],
    subScript: [new MathRun("i")],
    superScript: [new MathRun("10")],
}),