在 Android 上向 PowerPoint 演示文稿添加数学公式

概览

PowerPoint 将公式存储为 Office Math Markup Language (OMML)。使用 Aspose.Slides for Android via Java,您可以以编程方式创建相同类型的数学内容:分数、根式、函数、极限、N 进制运算符、矩阵、数组以及格式化的数学块。

在 PowerPoint 中,用户通常通过 插入 > 公式 添加公式:

PowerPoint 插入选项卡,已选择公式命令

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

包含可编辑数学公式的 PowerPoint 幻灯片

Aspose.Slides 通过以下三个主要对象构建该数学文本:

  • 一个数学形状,通过 [addMathShape] 创建,用于包含公式的形状。
  • [MathPortion] 在形状的文本框中存储数学内容。
  • [MathParagraph] 包含一个或多个 [MathBlock] 对象。

下面的大多数示例使用 [MathematicalText] 和来自 [IMathElement] 的流畅方法,以保持代码简短且易读。

有关 MathML 导出场景,请参阅 Export Math Equations from Presentations on Android

创建公式

此示例创建一个数学形状并添加勾股定理:

c 平方等于 a 平方加 b 平方 的公式

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    IMathBlock equation = new MathematicalText("c")
            .setSuperscript("2")
            .join("=")
            .join(new MathematicalText("a").setSuperscript("2"))
            .join("+")
            .join(new MathematicalText("b").setSuperscript("2"));

    mathParagraph.add(equation);

    presentation.save("pythagorean-theorem.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

添加分数

使用 divide 创建分数。您可以使用 [MathFractionTypes] 选择分数样式。

显示 1 除以 x 的倾斜数学分数

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    IMathFraction fraction = new MathematicalText("1")
            .divide("x", MathFractionTypes.Skewed);

    mathParagraph.add(new MathBlock(fraction));

    presentation.save("fraction.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

对于堆叠分数,使用 MathFractionTypes.Bar

IMathFraction stackedFraction = new MathematicalText("x + 1").divide("y - 1", MathFractionTypes.Bar);

添加根式

使用 radical 创建平方根、立方根或其他根式。当前元素成为底数,参数成为指数。

在根号下有 x 的 n 次根表达式

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    IMathRadical radical = new MathematicalText("x")
            .radical("n");

    mathParagraph.add(new MathBlock(radical));

    presentation.save("radical.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

添加函数和极限

使用 asArgumentOfFunctionfunction 添加 sin(x)log(x) 等函数或自定义函数名。对于极限,将 lim 放入 [MathLimit] 或使用 setLowerLimit

x 趋于无穷大的极限

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    IMathFunction limit = new MathematicalText("lim")
            .setLowerLimit("x→∞")
            .function("x");

    mathParagraph.add(new MathBlock(limit));

    presentation.save("functions-and-limits.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

对于自定义函数名,将函数名设为当前元素:

IMathFunction customFunction = new MathematicalText("f").function("x + 1");

添加 N 进制运算符和积分

使用 nary 添加求和、并集、交集等大运算符。使用 integral 添加积分。这两种方法都允许设置上下限。

带上下限的求和符号

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    IMathBlock summationBase = new MathematicalText("x")
            .setSuperscript("k")
            .join(new MathematicalText("a").setSuperscript("n-k"));

    IMathNaryOperator summation = summationBase.nary(MathNaryOperatorTypes.Summation, "k=0", "n");

    mathParagraph.add(new MathBlock(summation));

    presentation.save("nary-operators.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

N 进制运算符用于可选上下限的大运算符。像 +-= 这样的简单运算符通常作为 MathematicalText 添加并拼接到表达式中。

对于积分,使用 integral

IMathBlock integralBase = new MathematicalText("x").join(new MathematicalText("dx").toBox());
IMathNaryOperator integral = integralBase.integral(MathIntegralTypes.Simple, "0", "1");

添加矩阵

使用 [MathMatrix] 定义行和列。矩阵默认不包含括号,如需括号、方括号或大括号,请自行包裹矩阵。

一个两行矩阵,其中有一个空单元格

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    MathMatrix matrix = new MathMatrix(2, 3);
    matrix.set_Item(0, 0, new MathematicalText("1"));
    matrix.set_Item(0, 1, new MathematicalText("x"));
    matrix.set_Item(1, 0, new MathematicalText("x"));
    matrix.set_Item(1, 1, new MathematicalText("2"));
    matrix.set_Item(1, 2, new MathematicalText("y"));

    mathParagraph.add(new MathBlock(matrix));

    presentation.save("matrix.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

添加公式数组

当需要对齐的公式或垂直堆叠的表达式时,使用 toMathArray

垂直数学数组,x 位于 y 之上

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 140);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    IMathArray equationArray = new MathematicalText("x")
            .join("y")
            .toMathArray();

    mathParagraph.add(new MathBlock(equationArray));

    presentation.save("equation-array.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

添加三角函数

当参数是当前元素且函数名已知时,使用 asArgumentOfFunction

三角函数 cos 作用于 2x

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    IMathFunction cosine = new MathematicalText("2x")
            .asArgumentOfFunction(MathFunctionsOfOneArgument.Cos);

    mathParagraph.add(new MathBlock(cosine));

    presentation.save("trigonometric-function.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

添加下标和上标

使用下标和上标辅助方法添加索引和幂。当索引必须出现在基数左侧时,使用 setSubSuperscriptOnTheLeft

左侧下标 1、上标 n 的大写字母 Y

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    IMathLeftSubSuperscriptElement scripts = new MathematicalText("Y")
            .setSubSuperscriptOnTheLeft("1", "n");

    mathParagraph.add(new MathBlock(scripts));

    presentation.save("subscript-superscript.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

添加分隔符

使用 enclose 将表达式放入分隔符中。对于包含多个元素的分隔符表达式,还可以设置分隔字符。

包含 x、y、z 并用竖线分隔的分隔符表达式

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    IMathDelimiter delimiter = new MathematicalText("x")
            .join("y")
            .join("z")
            .enclose('<', '>');
    delimiter.setSeparatorCharacter('|');

    mathParagraph.add(new MathBlock(delimiter));

    presentation.save("delimiters.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

添加边框盒

当需要给公式加框时,使用 toBorderBox

带框的公式,a 平方等于 b 平方加 c 平方

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    IMathBorderBox boxedEquation = new MathematicalText("a")
            .setSuperscript("2")
            .join("=")
            .join(new MathematicalText("b").setSuperscript("2"))
            .join("+")
            .join(new MathematicalText("c").setSuperscript("2"))
            .toBorderBox();

    mathParagraph.add(new MathBlock(boxedEquation));

    presentation.save("border-box.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

分组项

使用 group 在表达式上方或下方放置分组字符。可以添加极限来为分组项标注标签。

表达式 x 加 y 被分组,下面有任意文本标签

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    IMathLimit grouped = new MathematicalText("x + y")
            .group('\u23DF', MathTopBotPositions.Bottom, MathTopBotPositions.Top)
            .setLowerLimit("any text");

    mathParagraph.add(new MathBlock(grouped));

    presentation.save("grouped-terms.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

格式化数学元素

仅在有助于公式清晰时使用格式化辅助方法。例如,overbar 在数学元素上方添加一条横线。

带上划线的数学表达式 ABC

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
    IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
            .get_Item(0).getPortions().get_Item(0)).getMathParagraph();

    IMathBar overbar = new MathematicalText("ABC").overbar();

    mathParagraph.add(new MathBlock(overbar));

    presentation.save("overbar.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

快速参考

Task Main API
创建数学文本 MathematicalText
合并元素 IMathElement.join
创建分数 IMathElement.divide
添加上标或下标 setSuperscript, setSubscript
添加函数 function, asArgumentOfFunction
添加根式 IMathElement.radical
添加极限 setLowerLimit, setUpperLimit
添加左侧脚本 setSubSuperscriptOnTheLeft
添加求和和积分 nary, integral
添加矩阵 MathMatrix
添加公式数组 toMathArray
添加分隔符 enclose
添加横线和边框 overbar, toBorderBox
分组项 group

常见问题

我可以编辑已有的 PowerPoint 公式吗?

可以。打开演示文稿,找到包含 MathPortion 的形状,获取其 MathParagraph,并更新该段落中的数学块。

公式是否保存为可编辑的 PowerPoint 数学内容?

是的。保存为 PPTX 时,Aspose.Slides 会将公式写入可编辑的 Office 数学内容。

我可以将公式导出为 LaTeX 吗?

Aspose.Slides 将数学公式导出为 MathML。如果需要 LaTeX,可先导出为 MathML,然后使用支持目标 LaTeX 方言的工具将 MathML 转换为 LaTeX。