添加数学公式到 PowerPoint 演示文稿(Java)

概述

在 PowerPoint 中,可以编写数学方程或公式并在演示文稿中显示。为此,PowerPoint 中提供了各种数学符号,可将其添加到文本或公式中。PowerPoint 使用数学公式构造器来帮助创建诸如以下的复杂公式:

  • 数学分数
  • 数学根式
  • 数学函数
  • 极限和对数函数
  • N 元运算
  • 矩阵
  • 大运算符
  • 正弦、余弦函数

要在 PowerPoint 中添加数学公式,请使用 Insert → Equation 菜单:

todo:image_alt_text

这将在 XML 中创建可在 PowerPoint 中显示的数学文本,如下所示:

todo:image_alt_text

PowerPoint 支持大量数学符号来创建公式。然而,在 PowerPoint 中创建复杂的数学公式往往难以获得专业的外观。需要频繁制作数学演示文稿的用户,通常会求助第三方解决方案来创建美观的公式。

使用Aspose.Slide API,您可以在 C# 中以编程方式处理 PowerPoint 演示文稿中的数学公式。可以创建新的数学表达式或编辑已有的表达式。对数学结构导出为图像的功能也部分受支持。

如何创建数学公式

数学元素用于构建任意层次嵌套的数学结构。线性集合的数学元素形成由MathBlock类表示的数学块。MathBlock本质上是一个独立的数学表达式、公式或方程。MathPortion是用于保存数学文本的数学段(请勿与Portion混淆)。MathParagraph用于操作一组数学块。上述类是通过 Aspose.Slides API 操作 PowerPoint 数学公式的关键。

下面演示如何使用 Aspose.Slides API 创建下列数学公式:

todo:image_alt_text

要在幻灯片上添加数学表达式,首先添加一个将容纳数学文本的形状:

Presentation pres = new Presentation();
try {
    IAutoShape mathShape = pres.getSlides().get_Item(0).getShapes().addMathShape(0, 0, 720, 150);
} finally {
    if (pres != null) pres.dispose();
}

创建后,形状默认已包含一个带有数学段的段落。MathPortion类表示包含数学文本的段。要访问MathPortion中的数学内容,请引用MathParagraph变量:

IMathParagraph mathParagraph = ((MathPortion)mathShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)).getMathParagraph();

MathParagraph类允许读取、添加、编辑和删除由数学元素组合而成的数学块(MathBlock)。例如,创建一个分数并将其放入演示文稿:

IMathFraction fraction = new MathematicalText("x").divide("y");

mathParagraph.add(new MathBlock(fraction));

每个数学元素都由实现IMathElement接口的类表示。该接口提供大量方法,可轻松创建数学表达式。只需一行代码即可构建相当复杂的表达式。例如,勾股定理可以这样写:

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

IMathElement接口的操作在任何元素类型中都已实现,包括MathBlock

完整源码示例:

Presentation pres = new Presentation();
try {
    IAutoShape mathShape = pres.getSlides().get_Item(0).getShapes().addMathShape(0, 0, 720, 150);

    IMathParagraph mathParagraph = ((MathPortion)mathShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)).getMathParagraph();
    
    IMathFraction fraction = new MathematicalText("x").divide("y");

    mathParagraph.add(new MathBlock(fraction));

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

    pres.save("math.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

数学元素类型

数学表达式由一系列数学元素组成。数学元素序列由数学块表示,数学元素的参数形成树状嵌套。

有许多数学元素类型可用于构建数学块。每个元素都可以被包含(聚合)到另一个元素中。也就是说,元素本身是其他元素的容器,形成树状结构。最简单的元素类型不包含其他数学文本元素。

每种数学元素类型实现IMathElement接口,从而可以在不同类型的数学元素上使用统一的数学操作集合。

MathematicalText 类

MathematicalText类表示数学文本——所有数学构造的基础元素。数学文本可表示操作数、运算符、变量以及任何其他线性文本。

示例:𝑎=𝑏+𝑐

MathFraction 类

MathFraction类指定分数对象,由分子和分母组成,之间有分数线。分数线可以是水平的或对角的,取决于分数属性。该对象也用于表示堆叠函数,即一个元素位于另一个元素之上且没有分数线。

示例:

todo:image_alt_text

MathRadical 类

MathRadical类指定根函数(数学根),由基底和可选的指数构成。

示例:

todo:image_alt_text

MathFunction 类

MathFunction类指定一个带参数的函数。包含属性:getName——函数名称以及getBase——函数参数。

示例:

todo:image_alt_text

MathNaryOperator 类

MathNaryOperator类指定 N 元数学对象,例如求和或积分。它由运算符、基底(或操作数)以及可选的上、下限组成。N 元运算的示例包括求和、并集、交集、积分。

该类不包括加法、减法等简单运算符,这些由单一文本元素MathematicalText表示。

示例:

todo:image_alt_text

MathLimit 类

MathLimit类用于创建上限或下限。它由基线文本和紧挨其上的(或下方)缩小文本组成。该元素本身不包含 “lim” 字样,但可用于在表达式顶部或底部放置文本。例如,以下表达式

todo:image_alt_text

可以通过组合MathFunctionMathLimit元素实现:

MathLimit funcName = new MathLimit(new MathematicalText("lim"), new MathematicalText("𝑥→∞"));

MathFunction mathFunc = new MathFunction(funcName, new MathematicalText("𝑥"));

MathSubscriptElement、MathSuperscriptElement、MathRightSubSuperscriptElement、MathLeftSubSuperscriptElement 类

上述类用于指定下标或上标。可以在左侧或右侧同时设置下标和上标,但仅在右侧支持单独的下标或上标。MathSubscriptElement也可用于设置数字的数学指数。

示例:

todo:image_alt_text

MathMatrix 类

MathMatrix类指定矩阵对象,由子元素按行列布局组成。需要注意的是矩阵本身没有内置分隔符。若要在括号中放置矩阵,需要使用分隔符对象——IMathDelimiter。可以使用空参数在矩阵中创建空格。

示例:

todo:image_alt_text

MathArray 类

MathArray类指定垂直排列的方程或任意数学对象数组。

示例:

todo:image_alt_text

格式化数学元素

  • MathBorderBox类:在IMathElement周围绘制矩形或其他边框。

    示例:todo:image_alt_text

  • MathBox类:指定数学元素的逻辑盒装。例如,可将盒装对象用作运算符模拟器,带或不带对齐点,或用作换行点,亦可将其分组以防止在内部换行。例如,"==" 运算符应盒装以防止换行。

  • MathDelimiter类:指定分隔符对象,由左、右字符(如圆括号、花括号、方括号、竖线)以及内部一个或多个数学元素组成,元素之间可使用指定字符分隔。示例:(𝑥²); [𝑥²|𝑦²]。

    示例:todo:image_alt_text

  • MathAccent类:指定重音函数,由基底和组合变音符组成。

    示例:𝑎́。

  • MathBar类:指定横杠函数,由基底参数和上横线或下横线组成。

    示例:todo:image_alt_text

  • MathGroupingCharacter类:指定用于在表达式上方或下方标记分组的符号,通常用于突出元素之间的关系。

    示例:todo:image_alt_text

数学运算

每个数学元素和数学表达式(通过MathBlock)实现了IMathElement接口。该接口允许对现有结构执行操作,以形成更复杂的数学表达式。所有操作都有两套参数:可以是[IMathElement]或字符串。当使用字符串参数时,会隐式创建MathematicalText实例。以下列出 Aspose.Slides 支持的数学操作。

Join 方法

将数学元素连接并形成数学块。例如:

IMathElement element1 = new MathematicalText("x");

IMathElement element2 = new MathematicalText("y");

IMathBlock block = element1.join(element2);

Divide 方法

使用给定分子和指定分母创建指定类型的分数。例如:

IMathElement numerator = new MathematicalText("x");

IMathFraction fraction = numerator.divide("y", MathFractionTypes.Linear);

Enclose 方法

用指定字符(如括号或其他字符)将元素括起来。

/**
 * <p>
 * Enclose a math element in parenthesis
 * </p>
 */
public IMathDelimiter enclose();

/**
 * <p>
 * Encloses this element in specified characters such as parenthesis or another characters as framing
 * </p>
 */
public IMathDelimiter enclose(char beginningCharacter, char endingCharacter);

示例:

IMathDelimiter delimiter = new MathematicalText("x").enclose('[', ']');

IMathDelimiter delimiter2 = new MathematicalText("elem1").join("elem2").enclose();

Function 方法

使用当前对象作为函数名称,创建带参数的函数。

/**
 * <p>
 * Takes a function of an argument using this instance as the function name
 * </p>
 */
public IMathFunction function(IMathElement functionArgument);

/**
 * <p>
 * Takes a function of an argument using this instance as the function name
 * </p>
 */
public IMathFunction function(String functionArgument);

示例:

IMathFunction func = new MathematicalText("sin").function("x");

AsArgumentOfFunction 方法

使用当前实例作为参数,调用指定函数。您可以:

示例:

MathLimit funcName = new MathLimit(new MathematicalText("lim"), new MathematicalText("𝑛→∞"));

IMathFunction func1 = new MathematicalText("2x").asArgumentOfFunction(funcName);

IMathFunction func2 = new MathematicalText("x").asArgumentOfFunction("sin");

IMathFunction func3 = new MathematicalText("x").asArgumentOfFunction(MathFunctionsOfOneArgument.Sin);

IMathFunction func4 = new MathematicalText("x").asArgumentOfFunction(MathFunctionsOfTwoArguments.Log, "3");

SetSubscript、SetSuperscript、SetSubSuperscriptOnTheRight、SetSubSuperscriptOnTheLeft 方法

设置下标和上标。可以在左侧或右侧同时设置下标和上标,但单独的下标或上标仅在右侧受支持。Superscript 还能用于设置数字的数学指数。

示例:

IMathLeftSubSuperscriptElement script = new MathematicalText("y").setSubSuperscriptOnTheLeft("2x", "3z");

Radical 方法

指定给定指数的数学根。

示例:

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

SetUpperLimit 与 SetLowerLimit 方法

设置上限或下限。上限和下限仅表示参数相对于基底的位置。

例如表达式:

todo:image_alt_text

此类表达式可通过组合MathFunctionMathLimit以及IMathElement的操作创建:

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

Nary 与 Integral 方法

naryintegral 方法均返回表示 N 元运算符的IMathNaryOperator类型。nary 方法的 MathNaryOperatorTypes 枚举指定运算符类型(求和、并集等),不包括积分。integral 方法则使用 MathIntegralTypes 枚举表示积分类型。

示例:

IMathBlock baseArg = new MathematicalText("x").join(new MathematicalText("dx").toBox());

IMathNaryOperator integral = baseArg.integral(MathIntegralTypes.Simple, "0", "1");

ToMathArray 方法

toMathArray 将元素放入垂直数组中。如果对MathBlock实例调用此操作,所有子元素将被放入返回的数组。

示例:

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

格式化操作:Accent、Overbar、Underbar、Group、ToBorderBox、ToBox

  • accent 方法为元素添加重音符(位于元素顶部的字符)。
  • overbarunderbar 方法分别在元素上方或下方添加横线。
  • group 方法使用分组字符(如底部大括号等)将元素分组。
  • toBorderBox 方法将元素放入边框盒。
  • toBox 方法将元素放入非可视的逻辑盒。

示例:

IMathAccent accent = new MathematicalText("x").accent('\u0303');

IMathBar bar = new MathematicalText("x").overbar();

IMathGroupingCharacter groupChr = new MathematicalText("x").join("y").join("z").group('\u23E1', MathTopBotPositions.Bottom, MathTopBotPositions.Top);

IMathBorderBox borderBox = new MathematicalText("x+y+z").toBorderBox();

IMathBox boxedOperator = new MathematicalText(":=").toBox();

FAQ

如何在 PowerPoint 幻灯片中添加数学公式?

要添加数学公式,需要创建一个自动包含数学段的数学形状对象。然后,从MathPortion获取MathParagraph,并向其添加MathBlock对象。

是否可以创建复杂的嵌套数学表达式?

可以,Aspose.Slides 通过嵌套 MathBlocks 支持创建复杂的数学表达式。每个数学元素实现了IMathElement接口,可使用 Join、Divide、Enclose 等操作将元素组合成更复杂的结构。

如何更新或修改已有的数学公式?

要更新公式,需要通过MathParagraph访问已有的 MathBlocks。然后使用 Join、Divide、Enclose 等方法修改公式的各个元素。编辑完成后,保存演示文稿即可应用更改。