!> Math requires an understanding of Sections and Paragraphs.
- To add math, create a
Math
object - Add
MathComponents
insideMath
MathComponents
can have nestedMathComponents
inside. e.g. A fraction where the numerator is a square root, and the denominator as another fraction. More onMathComponents
below- Make sure to add the
Math
object inside aParagraph
new Math({
children: [
new MathRun("2+2"),
new MathFraction({
numerator: [new MathRun("hi")],
denominator: [new MathRun("2")],
}),
],
}),
This will produce:
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.
MathRun
is the most basic MathComponent
.
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"),
],
}),
MathFractions
require a numerator
and a denominator
, which are both a list of MathComponents
new MathFraction({
numerator: [new MathRun("1")],
denominator: [new MathRun("2")],
}),
new MathFraction({
numerator: [
new MathRun("1"),
new MathRadical({
children: [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"),
],
}),
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({
children: [new MathRun("i")],
}),
new MathSum({
children: [
new MathSuperScript({
children: [new MathRun("e")],
superScript: [new MathRun("2")],
})
],
subScript: [new MathRun("i")],
superScript: [new MathRun("10")],
}),
A MathComponent
for the √
symbol. Examples include, square root, cube root etc. There is an optional degree
parameter to specify the number of times the radicand is multiplied by itself. For example, 3
for cube root.
new MathRadical({
children: [new MathRun("2")],
}),
Cube root example:
new MathRadical({
children: [
new MathFraction({
numerator: [new MathRun("1")],
denominator: [new MathRun("2")],
}),
new MathRun('+ 1'),
],
degree: [new MathRun("3")],
}),
MathSuperScripts
are the little numbers written to the top right of numbers or variables. It means the exponent or power if written by itself with the number or variable.
new MathSuperScript({
children: [new MathRun("x")],
superScript: [new MathRun("2")],
}),
An example with cosine:
new MathSuperScript({
children: [new MathRun("cos")],
superScript: [new MathRun("-1")],
}),
MathSubScripts
are similar to MathSuperScripts
, except the little number is written below.
new MathSubScript({
children: [new MathRun("F")],
subScript: [new MathRun("n-1")],
}),
MathSubSuperScripts
are a combination of both MathSuperScript
and MathSubScript
.
new MathSubSuperScript({
children: [new MathRun("test")],
superScript: [new MathRun("hello")],
subScript: [new MathRun("world")],
}),
MathFunctions
are a way of describing what happens to an input variable, in order to get the output result. It takes a name
parameter to specify the name of the function.
new MathFunction({
name: [
new MathSuperScript({
children: [new MathRun("cos")],
superScript: [new MathRun("-1")],
}),
],
children: [new MathRun("100")],
}),
new MathSquareBrackets({
children: [
new MathFraction({
numerator: [new MathRun("1")],
denominator: [new MathRun("2")],
}),
],
}),
new MathRoundBrackets({
children: [
new MathFraction({
numerator: [new MathRun("1")],
denominator: [new MathRun("2")],
}),
],
}),
new MathCurlyBrackets({
children: [
new MathFraction({
numerator: [new MathRun("1")],
denominator: [new MathRun("2")],
}),
],
}),
new MathAngledBrackets({
children: [
new MathFraction({
numerator: [new MathRun("1")],
denominator: [new MathRun("2")],
}),
],
}),
new MathLimitUpper({
children: [new MathRun("x")],
limit: [new MathRun("-")],
}),
new MathLimitLower({
children: [new MathRun("lim")],
limit: [new MathRun("x→0")],
}),