diff --git a/demo/55-math.ts b/demo/55-math.ts index 607071ec526..6fb6ac2be84 100644 --- a/demo/55-math.ts +++ b/demo/55-math.ts @@ -21,6 +21,8 @@ import { Packer, Paragraph, TextRun, + MathLimitLower, + MathLimitUpper, } from "docx"; const doc = new Document({ @@ -316,6 +318,23 @@ const doc = new Document({ }), ], }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathLimitUpper({ + children: [new MathRun("x")], + limit: [new MathRun("-")], + }), + new MathRun("="), + new MathLimitLower({ + children: [new MathRun("lim")], + limit: [new MathRun("x→0")], + }), + ], + }), + ], + }), ], }, ], diff --git a/docs/usage/math.md b/docs/usage/math.md index 5ebc83fc39f..57bddcd2012 100644 --- a/docs/usage/math.md +++ b/docs/usage/math.md @@ -263,3 +263,23 @@ new MathAngledBrackets({ ], }), ``` + +### Limit + +#### Limit Upper + +```ts +new MathLimitUpper({ + children: [new MathRun("x")], + limit: [new MathRun("-")], +}), +``` + +#### Limit Lower + +```ts +new MathLimitLower({ + children: [new MathRun("lim")], + limit: [new MathRun("x→0")], +}), +``` diff --git a/src/file/paragraph/math/n-ary/index.ts b/src/file/paragraph/math/n-ary/index.ts index b255d17e21a..81a5c239576 100644 --- a/src/file/paragraph/math/n-ary/index.ts +++ b/src/file/paragraph/math/n-ary/index.ts @@ -6,3 +6,6 @@ export * from "./math-sub-script"; export * from "./math-sum"; export * from "./math-integral"; export * from "./math-super-script"; +export * from "./math-limit"; +export * from "./math-limit-upper"; +export * from "./math-limit-lower"; diff --git a/src/file/paragraph/math/n-ary/math-limit-lower.spec.ts b/src/file/paragraph/math/n-ary/math-limit-lower.spec.ts new file mode 100644 index 00000000000..ea4fa900597 --- /dev/null +++ b/src/file/paragraph/math/n-ary/math-limit-lower.spec.ts @@ -0,0 +1,45 @@ +import { describe, expect, it } from "vitest"; + +import { Formatter } from "@export/formatter"; + +import { MathRun } from "../math-run"; +import { MathLimitLower } from "./math-limit-lower"; + +describe("MathLimitLower", () => { + describe("#constructor()", () => { + it("should create a MathLimitLower with correct root key", () => { + const mathLimitLower = new MathLimitLower({ + children: [new MathRun("lim")], + limit: [new MathRun("x→0")], + }); + + const tree = new Formatter().format(mathLimitLower); + expect(tree).to.deep.equal({ + "m:limLow": [ + { + "m:e": [ + { + "m:r": [ + { + "m:t": ["lim"], + }, + ], + }, + ], + }, + { + "m:lim": [ + { + "m:r": [ + { + "m:t": ["x→0"], + }, + ], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/n-ary/math-limit-lower.ts b/src/file/paragraph/math/n-ary/math-limit-lower.ts new file mode 100644 index 00000000000..0227694cba8 --- /dev/null +++ b/src/file/paragraph/math/n-ary/math-limit-lower.ts @@ -0,0 +1,19 @@ +// http://www.datypic.com/sc/ooxml/e-m_limLow-1.html +import { XmlComponent } from "@file/xml-components"; +import { MathComponent } from "../math-component"; +import { MathBase } from "./math-base"; +import { MathLimit } from "./math-limit"; + +export interface IMathLimitLowerOptions { + readonly children: readonly MathComponent[]; + readonly limit: readonly MathComponent[]; +} + +export class MathLimitLower extends XmlComponent { + public constructor(options: IMathLimitLowerOptions) { + super("m:limLow"); + + this.root.push(new MathBase(options.children)); + this.root.push(new MathLimit(options.limit)); + } +} diff --git a/src/file/paragraph/math/n-ary/math-limit-upper.spec.ts b/src/file/paragraph/math/n-ary/math-limit-upper.spec.ts new file mode 100644 index 00000000000..9d7d20511a2 --- /dev/null +++ b/src/file/paragraph/math/n-ary/math-limit-upper.spec.ts @@ -0,0 +1,45 @@ +import { describe, expect, it } from "vitest"; + +import { Formatter } from "@export/formatter"; + +import { MathRun } from "../math-run"; +import { MathLimitUpper } from "./math-limit-upper"; + +describe("MathLimitUpper", () => { + describe("#constructor()", () => { + it("should create a MathLimitUpper with correct root key", () => { + const mathLimitUpper = new MathLimitUpper({ + children: [new MathRun("x")], + limit: [new MathRun("-")], + }); + + const tree = new Formatter().format(mathLimitUpper); + expect(tree).to.deep.equal({ + "m:limUpp": [ + { + "m:e": [ + { + "m:r": [ + { + "m:t": ["x"], + }, + ], + }, + ], + }, + { + "m:lim": [ + { + "m:r": [ + { + "m:t": ["-"], + }, + ], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/n-ary/math-limit-upper.ts b/src/file/paragraph/math/n-ary/math-limit-upper.ts new file mode 100644 index 00000000000..d292c6db629 --- /dev/null +++ b/src/file/paragraph/math/n-ary/math-limit-upper.ts @@ -0,0 +1,19 @@ +// http://www.datypic.com/sc/ooxml/e-m_limUpp-1.html +import { XmlComponent } from "@file/xml-components"; +import { MathComponent } from "../math-component"; +import { MathBase } from "./math-base"; +import { MathLimit } from "./math-limit"; + +export interface IMathLimitUpperOptions { + readonly children: readonly MathComponent[]; + readonly limit: readonly MathComponent[]; +} + +export class MathLimitUpper extends XmlComponent { + public constructor(options: IMathLimitUpperOptions) { + super("m:limUpp"); + + this.root.push(new MathBase(options.children)); + this.root.push(new MathLimit(options.limit)); + } +} diff --git a/src/file/paragraph/math/n-ary/math-limit.spec.ts b/src/file/paragraph/math/n-ary/math-limit.spec.ts new file mode 100644 index 00000000000..8a6f9db8693 --- /dev/null +++ b/src/file/paragraph/math/n-ary/math-limit.spec.ts @@ -0,0 +1,27 @@ +import { describe, expect, it } from "vitest"; + +import { Formatter } from "@export/formatter"; + +import { MathRun } from "../math-run"; +import { MathLimit } from "./math-limit"; + +describe("MathLimit", () => { + describe("#constructor()", () => { + it("should create a MathLimit with correct root key", () => { + const mathLimit = new MathLimit([new MathRun("x→0")]); + + const tree = new Formatter().format(mathLimit); + expect(tree).to.deep.equal({ + "m:lim": [ + { + "m:r": [ + { + "m:t": ["x→0"], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/n-ary/math-limit.ts b/src/file/paragraph/math/n-ary/math-limit.ts new file mode 100644 index 00000000000..f66a54bc5c9 --- /dev/null +++ b/src/file/paragraph/math/n-ary/math-limit.ts @@ -0,0 +1,13 @@ +// http://www.datypic.com/sc/ooxml/e-m_lim-1.html +import { XmlComponent } from "@file/xml-components"; +import { MathComponent } from "../math-component"; + +export class MathLimit extends XmlComponent { + public constructor(children: readonly MathComponent[]) { + super("m:lim"); + + for (const child of children) { + this.root.push(child); + } + } +}