在 JavaScript 中向 PowerPoint 演示文稿添加数学公式
概述
PowerPoint 将公式存储为 Office Math Markup Language (OMML)。使用 Aspose.Slides for Node.js via Java,您可以以编程方式创建相同类型的数学内容:分数、根式、函数、极限、N 元运算符、矩阵、数组以及格式化的数学块。
在 PowerPoint 中,用户通常通过 Insert > Equation 添加公式:

结果是在幻灯片上可编辑的数学文本:

Aspose.Slides 通过三个主要对象构建此数学文本:
- 一个数学形状,由 addMathShape 创建,用于包含公式的形状。
- MathPortion 将数学内容存储在形状文本框内。
- MathParagraph 包含一个或多个 MathBlock 对象。
下面的大多数示例使用 MathematicalText 和来自 MathElementBase 的流畅方法,以保持代码简短且易读。
对于 MathML 导出场景,请参见 从演示文稿导出数学公式(Node.js via Java)。
创建公式
此示例创建一个数学形状并添加勾股定理:

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let equation = new aspose.slides.MathematicalText("c")
.setSuperscript("2")
.join("=")
.join(new aspose.slides.MathematicalText("a").setSuperscript("2"))
.join("+")
.join(new aspose.slides.MathematicalText("b").setSuperscript("2"));
mathParagraph.add(equation);
presentation.save("pythagorean-theorem.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
addMathShape 创建一个已经包含数学段落的形状。访问第一个 MathPortion,获取其 MathParagraph,并向其中添加数学块或数学元素。
添加分数
使用 divide 创建分数。您可以使用 MathFractionTypes 选择分数样式。

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let fraction = new aspose.slides.MathematicalText("1")
.divide("x", aspose.slides.MathFractionTypes.Skewed);
mathParagraph.add(new aspose.slides.MathBlock(fraction));
presentation.save("fraction.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
对于堆叠式分数,使用 MathFractionTypes.Bar:
let stackedFraction = new aspose.slides.MathematicalText("x + 1").divide("y - 1", aspose.slides.MathFractionTypes.Bar);
添加根式
使用 radical 创建平方根、立方根或其他根式。当前元素成为底数,参数成为指数。

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let radical = new aspose.slides.MathematicalText("x")
.radical("n");
mathParagraph.add(new aspose.slides.MathBlock(radical));
presentation.save("radical.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
添加函数和极限
使用 asArgumentOfFunction 或 function 来表示 sin(x)、log(x) 或自定义函数名等函数。对于极限,将 lim 放入 MathLimit,或使用 setLowerLimit。

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let limit = new aspose.slides.MathematicalText("lim")
.setLowerLimit("x\u2192\u221E")
.function("x");
mathParagraph.add(new aspose.slides.MathBlock(limit));
presentation.save("functions-and-limits.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
若使用自定义函数名,请将函数名设为当前元素:
let customFunction = new aspose.slides.MathematicalText("f").function("x + 1");
添加 N 元运算符和积分
使用 nary 处理求和、并集、交集等大运算符。使用 integral 处理积分。这两种方法均可设置上下限。

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let summationBase = new aspose.slides.MathematicalText("x")
.setSuperscript("k")
.join(new aspose.slides.MathematicalText("a").setSuperscript("n-k"));
let summation = summationBase.nary(aspose.slides.MathNaryOperatorTypes.Summation, "k=0", "n");
mathParagraph.add(new aspose.slides.MathBlock(summation));
presentation.save("nary-operators.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
N 元运算符用于可选上下限的大运算符。诸如 +、-、= 等简单运算符通常通过 MathematicalText 添加并组合到表达式中。
对于积分,使用 integral:
let integralBase = new aspose.slides.MathematicalText("x").join(new aspose.slides.MathematicalText("dx").toBox());
let integral = integralBase.integral(aspose.slides.MathIntegralTypes.Simple, "0", "1");
添加矩阵
使用 MathMatrix 来定义行和列。矩阵默认不包含括号,因此在需要圆括号、方括号或花括号时,需要自行包裹矩阵。

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let matrix = new aspose.slides.MathMatrix(2, 3);
matrix.set_Item(0, 0, new aspose.slides.MathematicalText("1"));
matrix.set_Item(0, 1, new aspose.slides.MathematicalText("x"));
matrix.set_Item(1, 0, new aspose.slides.MathematicalText("x"));
matrix.set_Item(1, 1, new aspose.slides.MathematicalText("2"));
matrix.set_Item(1, 2, new aspose.slides.MathematicalText("y"));
mathParagraph.add(new aspose.slides.MathBlock(matrix));
presentation.save("matrix.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
添加方程数组
当需要对齐的方程或垂直堆叠的表达式时,使用 toMathArray。

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 140);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let equationArray = new aspose.slides.MathematicalText("x")
.join("y")
.toMathArray();
mathParagraph.add(new aspose.slides.MathBlock(equationArray));
presentation.save("equation-array.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
添加三角函数
当参数是当前元素且函数名称已知时,使用 asArgumentOfFunction。

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let cosine = new aspose.slides.MathematicalText("2x")
.asArgumentOfFunction(aspose.slides.MathFunctionsOfOneArgument.Cos);
mathParagraph.add(new aspose.slides.MathBlock(cosine));
presentation.save("trigonometric-function.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
添加下标和上标
使用下标和上标帮助方法来表示索引和幂。当索引需要出现在基底左侧时,使用 setSubSuperscriptOnTheLeft。

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let scripts = new aspose.slides.MathematicalText("Y")
.setSubSuperscriptOnTheLeft("1", "n");
mathParagraph.add(new aspose.slides.MathBlock(scripts));
presentation.save("subscript-superscript.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
添加分隔符
使用 enclose 将表达式放入分隔符中。对于包含多个元素的分隔符表达式,还可以设置分隔字符。

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let delimiter = new aspose.slides.MathematicalText("x")
.join("y")
.join("z")
.enclose(java.newChar('<'), java.newChar('>'));
delimiter.setSeparatorCharacter(java.newChar('|'));
mathParagraph.add(new aspose.slides.MathBlock(delimiter));
presentation.save("delimiters.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
添加边框框
当需要为公式本身加框时,使用 toBorderBox。

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let boxedEquation = new aspose.slides.MathematicalText("a")
.setSuperscript("2")
.join("=")
.join(new aspose.slides.MathematicalText("b").setSuperscript("2"))
.join("+")
.join(new aspose.slides.MathematicalText("c").setSuperscript("2"))
.toBorderBox();
mathParagraph.add(new aspose.slides.MathBlock(boxedEquation));
presentation.save("border-box.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
分组项
使用 group 在表达式上方或下方放置分组字符。可以添加上下限为分组项加标签。

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let grouped = new aspose.slides.MathematicalText("x + y")
.group(java.newChar('\u23DF'), aspose.slides.MathTopBotPositions.Bottom, aspose.slides.MathTopBotPositions.Top)
.setLowerLimit("any text");
mathParagraph.add(new aspose.slides.MathBlock(grouped));
presentation.save("grouped-terms.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
格式化数学元素
仅在有助于阐明公式时使用格式化帮助方法。例如,overbar 会在数学元素上方加一条横杠。

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let overbar = new aspose.slides.MathematicalText("ABC").overbar();
mathParagraph.add(new aspose.slides.MathBlock(overbar));
presentation.save("overbar.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
快速参考
| 任务 | 主要 API |
|---|---|
| 创建数学文本 | MathematicalText |
| 组合元素 | join |
| 创建分数 | divide |
| 添加上标或下标 | setSuperscript, setSubscript |
| 添加函数 | function, asArgumentOfFunction |
| 添加根式 | radical |
| 添加极限 | setLowerLimit, setUpperLimit |
| 添加左侧脚本 | setSubSuperscriptOnTheLeft |
| 添加求和和积分 | nary, integral |
| 添加矩阵 | MathMatrix |
| 添加方程数组 | toMathArray |
| 添加分隔符 | enclose |
| 添加横线和边框 | overbar, toBorderBox |
| 分组项 | group |
常见问题
我可以编辑现有的 PowerPoint 公式吗?
可以。打开演示文稿,查找包含 MathPortion 的形状,获取其 MathParagraph,并更新该段落中的数学块。
公式会被保存为可编辑的 PowerPoint 数学吗?
可以。保存为 PPTX 时,Aspose.Slides 会将公式写入为可编辑的 Office 数学内容。
我可以将公式导出为 LaTeX 吗?
可以。通过其 MathPortion 获取公式的 MathParagraph,然后调用 MathParagraph.toLatex 直接导出。完整示例请参见 从演示文稿导出数学公式(Node.js via Java)。