diff --git a/docs/_sidebar.md b/docs/_sidebar.md index 5d2ba31551..bcdb03c55f 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -21,6 +21,7 @@ * [Table of Contents](usage/table-of-contents.md) * [Page Numbers](usage/page-numbers.md) * [Change Tracking](usage/change-tracking.md) + * [Math](usage/math.md) * Styling * [Styling with JS](usage/styling-with-js.md) * [Styling with XML](usage/styling-with-xml.md) diff --git a/docs/images/math-example.png b/docs/images/math-example.png new file mode 100644 index 0000000000..c92f8806f9 Binary files /dev/null and b/docs/images/math-example.png differ diff --git a/docs/usage/math.md b/docs/usage/math.md new file mode 100644 index 0000000000..e38f393ac9 --- /dev/null +++ b/docs/usage/math.md @@ -0,0 +1,126 @@ +# Math + +!> Math requires an understanding of [Sections](usage/sections.md) and [Paragraphs](usage/paragraph.md). + +## 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 + +```ts +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](usage/text.md). + +### Math Run + +`MathRun` is the most basic `MathComponent`. + +#### Example + +```ts +new MathRun("2+2"); +``` + +```ts +new MathRun("hello"); +``` + +An example of it being used inside `Math`: + +```ts +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 + +```ts +new MathFraction({ + numerator: [new MathRun("1")], + denominator: [new MathRun("2")], +}), +``` + +```ts +new MathFraction({ + numerator: [ + new MathRun("1"), + new MathRadical({ + child: [new MathRun("2")], + }), + ], + denominator: [new MathRun("2")], +}), +``` + +An example of it being used inside `Math`: + +```ts +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 + +```ts +new MathSum({ + child: [new MathRun("i")], +}), +``` + +```ts +new MathSum({ + child: [ + new MathSuperScript({ + child: new MathRun("e"), + superScript: new MathRun("2"), + }) + ], + subScript: [new MathRun("i")], + superScript: [new MathRun("10")], +}), +```