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

概述

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

在 PowerPoint 中,用户通常通过 Insert > Equation 添加公式:

PowerPoint 插入选项卡中选中的 “Equation” 命令

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

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

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

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

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

创建公式

此示例使用 addMathShape 创建数学形状并添加勾股定理:

公式 c² = a² + b² 的示意图

import com.aspose.slides.*;

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();
}

组合数学块

当公式的部分作为 IMathBlock 对象独立构建时,使用 joinBlock 将它们组合。示例在左块中构建 c² =,在右块中构建 a² + b²。setSuperscript 为这三个幂创建可编辑的上标。

import com.aspose.slides.*;

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

    IAutoShape shape = slide.getShapes().addMathShape(20, 20, 600, 100);
    IMathPortion mathPortion = (IMathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
    IMathParagraph mathParagraph = mathPortion.getMathParagraph();

    MathBlock leftBlock = new MathBlock();
    leftBlock.add(new MathematicalText("c").setSuperscript("2"));
    leftBlock.add(new MathematicalText("="));

    MathBlock rightBlock = new MathBlock();
    rightBlock.add(new MathematicalText("a").setSuperscript("2"));
    rightBlock.add(new MathematicalText("+"));
    rightBlock.add(new MathematicalText("b").setSuperscript("2"));

    IMathBlock combinedBlock = leftBlock.joinBlock(rightBlock);
    mathParagraph.add(combinedBlock);

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

joinBlock 返回 leftBlock 本身,现已按顺序包含两个部分,同时让 rightBlock 为空。仅将组合后的块一次添加到 IMathParagraph。重新打开 equation.pptx 后,该段落只包含一个块,表示 c² = a² + b²,且三个幂均保留为数学上标。

将 add 写成 leftBlock.add(rightBlock) 会尝试将一个块直接嵌套在另一个块内部,进而抛出 InvalidOperationException。

添加分数

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

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

import com.aspose.slides.*;

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:

import com.aspose.slides.*;

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

添加根式

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

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

import com.aspose.slides.*;

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();
}

显示或隐藏平方根的次数

平方根可以显示显式的 2,或使用传统的 √x 记法。setHideDegree 方法(属于 IMathRadical)控制显示哪种形式。两种形式都保留次数 2 和被开方数 x。

左侧显示次数的平方根,右侧隐藏次数的同一平方根

示例将相同的平方根保存两次。visible-degree.pptx 中次数可见,hidden-degree.pptx 中次数隐藏。

import com.aspose.slides.*;

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

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

    IAutoShape shape = slide.getShapes().addMathShape(20, 20, 200, 100);
    IMathPortion portion = (IMathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
    portion.getMathParagraph().add(new MathBlock(radical));

    radical.setHideDegree(false);
    presentation.save("visible-degree.pptx", SaveFormat.Pptx);

    radical.setHideDegree(true);
    presentation.save("hidden-degree.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

添加函数和极限

使用 asArgumentOfFunction 或 function 来创建 sin(x)、log(x) 或自定义函数名等函数。对于极限,将 lim 放入 MathLimit 或使用 setLowerLimit。

x 趋向无穷大的极限示意图

import com.aspose.slides.*;

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();
}

若使用自定义函数名,请将函数名设为当前元素并调用 function,并传入其参数:

import com.aspose.slides.*;

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

添加 N 元运算符和积分

使用 nary 处理求和、并集、交集等大型运算符。使用 integral 创建积分。两种方法均可设置上下限。

带有上下限的求和示例

import com.aspose.slides.*;

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:

import com.aspose.slides.*;

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

添加矩阵

使用 MathMatrix 定义行列。矩阵默认不包含括号,如需圆括号、方括号或大括号,请自行在矩阵外部添加。

带有一个空单元格的两行矩阵示例

import com.aspose.slides.*;

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();
}

编辑已有矩阵

假设在 matrix.pptx 中已有系数矩阵(由前面的示例保存),现在需要在下一练习中保留第一条方程,替换第二条为两条新方程,并修改变量集合。通过 IMathMatrix 打开已有矩阵,以保留仍然适用的系数。

原始两行系数矩阵,第一行包含 1、x 和一个空单元格,第二行包含 x、2、y

行和列索引从零开始,指向每次调用时矩阵的当前状态。在索引 1 之前插入行会将原第二行移动到索引 2。随后在索引 2 之后插入会在末尾追加一行。删除索引 2 的行会移除原第二条方程,并将追加的行上移一位。列的移动方式相同。

import com.aspose.slides.*;

Presentation presentation = new Presentation("matrix.pptx");
try {
    IAutoShape shape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
    IMathPortion portion = (IMathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
    IMathMatrix matrix = (IMathMatrix) portion.getMathParagraph().get_Item(0).get_Item(0);

    matrix.insertRowBefore(1);
    matrix.insertRowAfter(2);
    matrix.deleteRow(2);

    matrix.insertColumnBefore(1);
    matrix.insertColumnAfter(3);
    matrix.deleteColumn(3);

    // 新单元格初始为 null。先设置非零系数。
    matrix.set_Item(1, 0, new MathematicalText("12"));
    matrix.set_Item(1, 1, new MathematicalText("1"));
    matrix.set_Item(2, 2, new MathematicalText("10"));
    matrix.set_Item(2, 3, new MathematicalText("1"));
    for (int row = 0; row < matrix.getRowCount(); row++) {
        for (int column = 0; column < matrix.getColumnCount(); column++) {
            if (matrix.get_Item(row, column) == null) {
                matrix.set_Item(row, column, new MathematicalText("0"));
            }
        }
    }

    matrix.setColumnAlignment(0, MathHorizontalAlignment.Left);
    matrix.setColumnsAlignment(1, 3, MathHorizontalAlignment.Right);

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

setColumnAlignment 将第一列左对齐。setColumnsAlignment 将从索引 1 开始的连续三列右对齐。在 updated-matrix.pptx 中,矩阵包含三条方程和四个变量,原第一行的系数 1、x 现在出现在第一列和第三列。

更新后的三行矩阵,显示行 1,0,x,0;12,1,0,0;以及 0,0,10,1

添加方程数组

当需要对齐的方程组或垂直堆叠的表达式时,使用 toMathArray。

垂直数学数组示例,x 在上方,y 在下方

import com.aspose.slides.*;

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();
}

将数组与相邻元素对齐

setBaseJustification 控制数组相对于旁边数学元素的垂直对齐方式。下面的示例比较了以第一行、数组中心和最后一行对齐的效果。

示例创建了三个完全相同的数组,每个数组都有三行,并在左右两侧分别放置元素 A= 和 =B。所有数学文本使用 Cambria Math,字号 24 点。唯一变化的是数组的对齐方式。

import com.aspose.slides.*;
import android.graphics.Color;

class MathTextFactory {
    MathematicalText createText(String value) {
        MathematicalText text = new MathematicalText(value);
        text.getFormat().setFontHeight(24);
        text.getFormat().setLatinFont(new FontData("Cambria Math"));
        return text;
    }
}

MathTextFactory factory = new MathTextFactory();
Presentation presentation = new Presentation();
try {
    presentation.getSlideSize().setSize(780, 200, SlideSizeScaleType.DoNotScale);
    ISlide slide = presentation.getSlides().get_Item(0);

    int[] alignments = { MathVerticalAlignment.Top, MathVerticalAlignment.Center, MathVerticalAlignment.Bottom };
    String[] alignmentNames = { "Top", "Center", "Bottom" };

    for (int i = 0; i < alignments.length; i++) {
        IAutoShape label = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20 + i * 260, 10, 240, 30);
        label.getFillFormat().setFillType(FillType.NoFill);
        label.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
        label.getTextFrame().setText(alignmentNames[i]);
        IPortionFormat labelFormat = label.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
        labelFormat.setFontHeight(16);
        labelFormat.getFillFormat().setFillType(FillType.Solid);
        labelFormat.getFillFormat().getSolidFillColor().setColor(Color.BLACK);

        IAutoShape mathShape = slide.getShapes().addMathShape(20 + i * 260, 60, 240, 130);
        mathShape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Top);
        IMathParagraph mathParagraph = ((IMathPortion) mathShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)).getMathParagraph();
        mathParagraph.setJustification(MathJustification.LeftJustified);

        IMathElement[] rows = { factory.createText("x+y=3"), factory.createText("x-y=1"), factory.createText("x=2") };
        MathArray equationArray = new MathArray(rows[0]);
        equationArray.getArguments().add(rows[1]);
        equationArray.getArguments().add(rows[2]);
        equationArray.setBaseJustification(alignments[i]);
        MathematicalText leftText = factory.createText("A=");
        MathematicalText rightText = factory.createText("=B");
        IMathBlock equation = new MathBlock(leftText).join(equationArray).join(rightText);
        mathParagraph.add(equation);
    }

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

下图展示了在 PowerPoint 中保存的演示文稿。请留意每个数组旁边的 A= 与 =B 所在位置。

PowerPoint 中的方程数组,分别展示顶部、居中和底部对齐,旁边元素相同

配置行间距

setRowSpacingRule 用于选择数组内部行的间距规则。setRowSpacing 提供具体的数值用于显式间距规则,预设规则会忽略该值。

显式间距的数值以点为单位。比例间距的数值以半行计:示例中数值 3 表示 1.5 行。设置显式数值时需同时设置间距规则;仅更改数值而不修改规则不会生效。

下面的示例保存了五个演示文稿,所有方程、对齐方式和字体设置相同。前三个使用预设规则,后两个展示显式数值。

import com.aspose.slides.*;

class MathTextFactory {
    MathematicalText createText(String value) {
        MathematicalText text = new MathematicalText(value);
        text.getFormat().setFontHeight(24);
        text.getFormat().setLatinFont(new FontData("Cambria Math"));
        return text;
    }
}

MathTextFactory factory = new MathTextFactory();
int[] rules = { MathRowSpacingRule.SingleLineGap, MathRowSpacingRule.OneAndAHalfLineGap, MathRowSpacingRule.TwoLineGap, MathRowSpacingRule.Exactly, MathRowSpacingRule.Multiple };
int[] spacingValues = { 0, 0, 0, 20, 3 };
String[] ruleNames = { "SingleLineGap", "OneAndAHalfLineGap", "TwoLineGap", "Exactly", "Multiple" };

for (int i = 0; i < rules.length; i++) {
    String fileName = "equation-array-" + ruleNames[i] + ".pptx";
    Presentation presentation = new Presentation();
    try {
        IAutoShape mathShape = presentation.getSlides().get_Item(0).getShapes().addMathShape(20, 20, 600, 300);
        IParagraph paragraph = mathShape.getTextFrame().getParagraphs().get_Item(0);
        paragraph.getParagraphFormat().setSpaceBefore(0);
        paragraph.getParagraphFormat().setSpaceAfter(0);
        IMathParagraph mathParagraph = ((IMathPortion) paragraph.getPortions().get_Item(0)).getMathParagraph();
        mathParagraph.setJustification(MathJustification.LeftJustified);

        IMathElement[] rows = { factory.createText("x+y=3"), factory.createText("x-y=1"), factory.createText("x=2") };
        MathArray equationArray = new MathArray(rows[0]);
        equationArray.getArguments().add(rows[1]);
        equationArray.getArguments().add(rows[2]);
        equationArray.setBaseJustification(MathVerticalAlignment.Center);
        equationArray.setRowSpacingRule(rules[i]);
        equationArray.setRowSpacing(spacingValues[i]);
        MathematicalText leftText = factory.createText("A=");
        MathematicalText rightText = factory.createText("=B");
        IMathBlock equation = new MathBlock(leftText).join(equationArray).join(rightText);
        mathParagraph.add(equation);

        presentation.save(fileName, SaveFormat.Pptx);
    } finally {
        presentation.dispose();
    }
}

数组行间距独立于段落间距。setSpaceBefore 与 setSpaceAfter 调整的是包含段落的前后空间,而非数组行之间的间距。同样,IMathParagraph.setJustification 只影响整个数学段落的水平位置,并不替代数组的垂直对齐设置。

添加三角函数

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

三角函数 cos 作用于 2x 的示例

import com.aspose.slides.*;

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 示例

import com.aspose.slides.*;

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 表达式示例

import com.aspose.slides.*;

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();
}

分界符的大小和对齐

将 true 传递给 setGrowToMatchOperandHeight,可让括号随高大的操作数自动伸展;传 false 则保持普通大小。使用 setDelimiterShape 并配合 MathDelimiterShape 的两种取值之一。Centered 使括号在数学轴(等号中线)上居中,Match 则使其与被包裹内容对齐。两种设置仅影响分界符本身,内部表达式保持不变。

示例在 x 的指数位置放置分数,使表达式在基线上方的延伸大于下方。所有四个公式使用相同的括号和字体设置。

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    for (int row = 0; row < 2; row++) {
        for (int column = 0; column < 2; column++) {
            IMathFraction fraction = new MathematicalText("a + b").divide("c + d");
            IMathSuperscriptElement power = new MathematicalText("x").setSuperscript(fraction);
            IMathDelimiter delimiter = power.enclose('(', ')');
            delimiter.setGrowToMatchOperandHeight(row == 0);
            delimiter.setDelimiterShape(column == 0 ? MathDelimiterShape.Centered : MathDelimiterShape.Match);

            IAutoShape mathShape = slide.getShapes().addMathShape(20 + 330 * column, 20 + 120 * row, 300, 100);
            MathPortion portion = (MathPortion) mathShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
            portion.getPortionFormat().setFontHeight(32);
            IMathBlock equation = new MathematicalText("y = ").join(delimiter);
            portion.getMathParagraph().add(equation);
        }
    }
    presentation.save("delimiter-sizing.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

在 PowerPoint 中对比各行可见伸展效果。顶部行的括号会随指数伸展,左右两种形状表现相同。底部行则关闭伸展:左侧的 Centered 括号与等号对齐,右侧的 Match 括号则在表达式上方略高。

四个 PowerPoint 公式,展示分界符伸展开启(上)和关闭(下),左侧使用 Centered,右侧使用 Match

添加边框框

对分数调用 toBorderBox 可为整个表达式添加边框。返回的 IMathBorderBox 默认显示四条边。使用 setHideTop、setHideBottom、setHideLeft、setHideRight 可单独隐藏相应边。

示例在同一张幻灯片上放置六个相同的分数。为展示抵消效果,隐藏四条边,并将 true 传递给 setStrikethroughBottomLeftToTopRight 生成上升对角线 (↗),或传给 setStrikethroughTopLeftToBottomRight 生成下降对角线 (↘)。也可以使用 setStrikethroughHorizontal 或 setStrikethroughVertical 并任意组合可见的边。

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    presentation.getSlideSize().setSize(540, 220, SlideSizeScaleType.DoNotScale);
    ISlide slide = presentation.getSlides().get_Item(0);

    class FractionBuilder {
        IMathBorderBox addFraction(float x, float y, boolean hideBorder) {
            IMathFraction fraction = new MathematicalText("x+1").divide("y+2");
            IMathBorderBox box = fraction.toBorderBox();
            box.setHideTop(hideBorder);
            box.setHideBottom(hideBorder);
            box.setHideLeft(hideBorder);
            box.setHideRight(hideBorder);

            IAutoShape shape = slide.getShapes().addMathShape(x, y, 160, 90);
            MathPortion portion = (MathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
            portion.getPortionFormat().setFontHeight(28);
            portion.getMathParagraph().add(new MathBlock(box));
            return box;
        }
    }

    FractionBuilder builder = new FractionBuilder();
    builder.addFraction(10, 10, false);

    IMathBorderBox partial = builder.addFraction(190, 10, false);
    partial.setHideTop(true);
    partial.setHideRight(true);

    builder.addFraction(370, 10, true).setStrikethroughBottomLeftToTopRight(true);
    builder.addFraction(10, 120, true).setStrikethroughTopLeftToBottomRight(true);
    builder.addFraction(190, 120, true).setStrikethroughHorizontal(true);
    builder.addFraction(370, 120, false).setStrikethroughVertical(true);

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

PowerPoint 中的六个可编辑分数。上排:完整边框、仅底部和左侧边框、上升对角线。下排:下降对角线、水平划线、垂直划线并带完整边框

在此分数上,水平划线会覆盖分数线,垂直划线会穿过分子和分母,而外围边框仍保持可见。这些线条装饰整个表达式。普通文本的删除线仅作用于文本运行中的字符。重新在 PowerPoint 中打开演示文稿后,仍可独立编辑分子和分母。

使用幻影元素控制可见性和布局

将表达式包装在 MathPhantom 中,可独立控制其可见性以及在公式中占用的空间。IMathPhantom 的方法控制以下行为:

方法 效果
setShow 设为 false 可隐藏表达式。除非后续方法对应设为 true,否则其尺寸仍会影响布局。
setZeroWidth 将表达式的水平宽度视为零,使相邻元素能够更靠近。
setZeroAsc 将其上升高度(基线以上的部分)视为零,可减少邻近元素上方保留的空间。
setZeroDesc 将其下降深度(基线以下的部分)视为零,可使分数线或其他内容更靠近下方元素。

基线是普通数学字符所坐落的参考线。隐藏内容并不会移除其尺寸,将尺寸设为零也不会删除表达式。底层表达式仍可通过 getBase 访问。将 true 传给 setShow 可再次显示,尺寸设置保持不变。

下面的示例创建了五个并排的变体:一个可见分数、一个保留空间的隐藏分数,以及三个分别将宽度、上升或下降设为零的隐藏分数。每个变体使用相同的外围表达式和 Cambria Math,字号 24 点。所有公式形状的顶部位置相同,以灰色参考线标记。将幻影放在外层分数的分子中,可更直观看到垂直布局的变化。

import com.aspose.slides.*;
import android.graphics.Color;

class MathTextFactory {
    MathematicalText createText(String value) {
        MathematicalText text = new MathematicalText(value);
        text.getFormat().setFontHeight(24);
        text.getFormat().setLatinFont(new FontData("Cambria Math"));
        return text;
    }
}

MathTextFactory factory = new MathTextFactory();
Presentation presentation = new Presentation();
try {
    presentation.getSlideSize().setSize(1000, 180, SlideSizeScaleType.DoNotScale);
    ISlide slide = presentation.getSlides().get_Item(0);

    String[] labels = { "Visible", "Hidden: reserves space", "Hidden: ZeroWidth", "Hidden: ZeroAsc", "Hidden: ZeroDesc" };

    IAutoShape guide = slide.getShapes().addAutoShape(ShapeType.Line, 10, 60, 980, 0);
    guide.getLineFormat().getFillFormat().setFillType(FillType.Solid);
    guide.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.LTGRAY);

    for (int i = 0; i < labels.length; i++) {
        int left = 10 + i * 200;

        IAutoShape label = slide.getShapes().addAutoShape(ShapeType.Rectangle, left, 10, 190, 30);
        label.getFillFormat().setFillType(FillType.NoFill);
        label.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
        label.getTextFrame().setText(labels[i]);

        IPortionFormat labelFormat = label.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
        labelFormat.setFontHeight(14);
        labelFormat.getFillFormat().setFillType(FillType.Solid);
        labelFormat.getFillFormat().getSolidFillColor().setColor(Color.BLACK);

        MathematicalText fractionNumerator = factory.createText("a+b");
        MathematicalText fractionDenominator = factory.createText("c+d");
        MathFraction fraction = new MathFraction(fractionNumerator, fractionDenominator);
        MathPhantom phantom = new MathPhantom(fraction);
        phantom.setShow(i == 0);
        phantom.setZeroWidth(i == 2);
        phantom.setZeroAsc(i == 3);
        phantom.setZeroDesc(i == 4);

        MathematicalText leftText = factory.createText("x+");
        MathematicalText rightText = factory.createText("+y");
        IMathBlock numerator = new MathBlock(leftText).join(phantom).join(rightText);
        MathematicalText denominator = factory.createText("z");
        MathFraction equation = new MathFraction(numerator, denominator);

        IAutoShape shape = slide.getShapes().addMathShape(left, 60, 190, 100);
        shape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Top);

        IMathPortion mathPortion = (IMathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
        IMathParagraph mathParagraph = mathPortion.getMathParagraph();
        mathParagraph.add(new MathBlock(equation));
    }

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

PowerPoint 中的五个方程,对比可见分数、保留空间的隐藏分数以及启用 ZeroWidth、ZeroAsc、ZeroDesc 的隐藏分数

分组项

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

带有下方标签 “any text” 的 x 与 y 分组示例

import com.aspose.slides.*;

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 示例

import com.aspose.slides.*;

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();
}

格式化已有公式中的文本

要在已有公式的整个文本中应用格式,遍历每个数学块的元素树。IMathElement.getChildren 仅返回直接子节点,需要递归调用以到达分数、上下标、根式等嵌套结构内部的文本。检查当前元素及其子元素,以便在元素本身是 MathematicalText 时也能处理。

本示例假设 input.pptx 包含类似 z = xᵢ / ∛y 的公式,其中分数的分子带有下标,分母为根式。仅处理块的顶层元素会遗漏分数内部的文本。示例在幻灯片文本框中查找数学片段,获取其块 via IMathPortion.getMathParagraph,并通过每个文本元素的 getFormat 调用 setFontHeight 将字体大小设为 28 点。

import com.aspose.slides.*;

class MathTextFormatter {
    void formatMathText(IMathElement element) {
        if (element instanceof MathematicalText) {
            MathematicalText text = (MathematicalText) element;
            text.getFormat().setFontHeight(28);
        }

        for (IMathElement child : element.getChildren()) {
            formatMathText(child);
        }
    }
}

MathTextFormatter formatter = new MathTextFormatter();
Presentation presentation = new Presentation("input.pptx");
try {
    for (ISlide slide : presentation.getSlides()) {
        for (IShape shape : slide.getShapes()) {
            if (!(shape instanceof IAutoShape)) {
                continue;
            }

            IAutoShape autoShape = (IAutoShape) shape;
            if (autoShape.getTextFrame() == null) {
                continue;
            }

            for (IParagraph paragraph : autoShape.getTextFrame().getParagraphs()) {
                for (IPortion portion : paragraph.getPortions()) {
                    if (portion instanceof IMathPortion) {
                        IMathPortion mathPortion = (IMathPortion) portion;
                        for (IMathBlock block : mathPortion.getMathParagraph()) {
                            formatter.formatMathText(block);
                        }
                    }
                }
            }
        }
    }

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

保存后的演示文稿在每个已处理公式的各层级上均更新了文本格式,同时保留了其文本内容和数学结构。

快速参考

任务 主要 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 的形状,调用 getMathParagraph,并在该段落中更新数学块。

公式会以可编辑的 PowerPoint 数学格式保存吗?

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

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

可以。先从其 IMathPortion 获取 IMathParagraph,然后调用 IMathParagraph.toLatex 直接导出。完整示例请参阅 Export Math Equations from Presentations in Android via Java。