在 .NET 中向 PowerPoint 演示文稿添加数学方程式

概述

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

在 PowerPoint 中,用户通常从 Insert > Equation 添加方程式:

PowerPoint 插入选项卡,已选择 “Equation” 命令

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

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

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

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

有关 MathML 导出场景,请参阅 Export Math Equations from Presentations in .NET。

创建方程式

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

方程式 c² = a² + b²

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 120);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var 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);

添加分数

使用 Divide 创建分数。您可以使用 MathFractionTypes 选择分数样式。

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

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var fraction = new MathematicalText("1")
    .Divide("x", MathFractionTypes.Skewed);

mathParagraph.Add(new MathBlock(fraction));

presentation.Save("fraction.pptx", SaveFormat.Pptx);

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

using Aspose.Slides.MathText;

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

添加根式

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

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

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var radical = new MathematicalText("x")
    .Radical("n");

mathParagraph.Add(new MathBlock(radical));

presentation.Save("radical.pptx", SaveFormat.Pptx);

添加函数和极限

对于函数(如 sin(x)、log(x) 或自定义函数名),使用 AsArgumentOfFunction 或 Function。对于极限,将 lim 放入 MathLimit 或使用 SetLowerLimit。

x 趋于无穷大的极限

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var limit = new MathematicalText("lim")
    .SetLowerLimit("x→∞")
    .Function("x");

mathParagraph.Add(new MathBlock(limit));

presentation.Save("functions-and-limits.pptx", SaveFormat.Pptx);

对于自定义函数名,将函数名设为当前元素,然后使用 Function 并传入其参数:

using Aspose.Slides.MathText;

var customFunction = new MathematicalText("f").Function("x + 1");

添加 N 元运算符和积分

使用 Nary 进行求和、并集、交集等大运算符。使用 Integral 添加积分。两者都可以设置上下限。

带上下限的求和符号

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 120);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var summationBase = new MathematicalText("x")
    .SetSuperscript("k")
    .Join(new MathematicalText("a").SetSuperscript("n-k"));

var summation = summationBase.Nary(MathNaryOperatorTypes.Summation, "k=0", "n");

mathParagraph.Add(new MathBlock(summation));

presentation.Save("nary-operators.pptx", SaveFormat.Pptx);

N 元运算符用于可选上下限的大运算符。诸如 +、-、= 等简单运算符通常作为 MathematicalText 添加并连入表达式。

对于积分,请使用 Integral:

using Aspose.Slides.MathText;

var integralBase = new MathematicalText("x").Join(new MathematicalText("dx").ToBox());
var integral = integralBase.Integral(MathIntegralTypes.Simple, "0", "1");

添加矩阵

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

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

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 120);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var matrix = new MathMatrix(2, 3);
matrix[0, 0] = new MathematicalText("1");
matrix[0, 1] = new MathematicalText("x");
matrix[1, 0] = new MathematicalText("x");
matrix[1, 1] = new MathematicalText("2");
matrix[1, 2] = new MathematicalText("y");

mathParagraph.Add(new MathBlock(matrix));

presentation.Save("matrix.pptx", SaveFormat.Pptx);

添加方程式数组

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

垂直数学数组,x 在 y 上方

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 140);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var equationArray = new MathematicalText("x")
    .Join("y")
    .ToMathArray();

mathParagraph.Add(new MathBlock(equationArray));

presentation.Save("equation-array.pptx", SaveFormat.Pptx);

将数组与周围元素对齐

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

以下示例创建了三个完全相同的数组,每个数组都有三行,并在两侧添加了相邻元素 A= 和 =B。所有数学文本使用 Cambria Math,字号 24 点。仅改变数组对齐方式。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
presentation.SlideSize.SetSize(780, 200, SlideSizeScaleType.DoNotScale);
var slide = presentation.Slides[0];

var alignments = new[] { MathVerticalAlignment.Top, MathVerticalAlignment.Center, MathVerticalAlignment.Bottom };

for (int i = 0; i < alignments.Length; i++)
{
    var label = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20 + i * 260, 10, 240, 30);
    label.FillFormat.FillType = FillType.NoFill;
    label.LineFormat.FillFormat.FillType = FillType.NoFill;
    label.TextFrame.Text = alignments[i].ToString();
    var labelFormat = label.TextFrame.Paragraphs[0].Portions[0].PortionFormat;
    labelFormat.FontHeight = 16;
    labelFormat.FillFormat.FillType = FillType.Solid;
    labelFormat.FillFormat.SolidFillColor.Color = System.Drawing.Color.Black;

    var mathShape = slide.Shapes.AddMathShape(20 + i * 260, 60, 240, 130);
    mathShape.TextFrame.TextFrameFormat.AnchoringType = TextAnchorType.Top;
    var mathParagraph = ((IMathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
    mathParagraph.Justification = MathJustification.LeftJustified;

    var rows = new IMathElement[] { CreateText("x+y=3"), CreateText("x-y=1"), CreateText("x=2") };
    var equationArray = new MathArray(rows);
    equationArray.BaseJustification = alignments[i];
    var equation = new MathBlock(CreateText("A=")).Join(equationArray).Join(CreateText("=B"));
    mathParagraph.Add(equation);
}

presentation.Save("equation-array-alignment.pptx", SaveFormat.Pptx);

MathematicalText CreateText(string value)
{
    var text = new MathematicalText(value);
    text.Format.FontHeight = 24;
    text.Format.LatinFont = new FontData("Cambria Math");
    return text;
}

下图显示了在 PowerPoint 中保存的演示文稿。请注意每个数组旁边的 A= 和 =B 位置。

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

配置行间距

RowSpacingRule 选择数组内部行的间距规则。RowSpacing 提供数值用于显式间距规则,预设规则会忽略它。

对于精确间距,数值以点为单位。对于比例间距,数值以半行计数:示例中的 3 表示 1.5 行。指定显式值时请同时设置两属性;仅更改数值不会自动选择间距规则。

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

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

var settings = new (MathRowSpacingRule Rule, uint Spacing)[]
{
    (MathRowSpacingRule.SingleLineGap, 0),
    (MathRowSpacingRule.OneAndAHalfLineGap, 0),
    (MathRowSpacingRule.TwoLineGap, 0),
    (MathRowSpacingRule.Exactly, 20),
    (MathRowSpacingRule.Multiple, 3)
};

foreach (var setting in settings)
{
    var fileName = $"equation-array-{setting.Rule}.pptx";
    using (var presentation = new Presentation())
    {
        var mathShape = presentation.Slides[0].Shapes.AddMathShape(20, 20, 600, 300);
        var paragraph = mathShape.TextFrame.Paragraphs[0];
        paragraph.ParagraphFormat.SpaceBefore = 0;
        paragraph.ParagraphFormat.SpaceAfter = 0;
        var mathParagraph = ((IMathPortion)paragraph.Portions[0]).MathParagraph;
        mathParagraph.Justification = MathJustification.LeftJustified;

        var rows = new IMathElement[] { CreateText("x+y=3"), CreateText("x-y=1"), CreateText("x=2") };
        var equationArray = new MathArray(rows);
        equationArray.BaseJustification = MathVerticalAlignment.Center;
        equationArray.RowSpacingRule = setting.Rule;
        equationArray.RowSpacing = setting.Spacing;
        var equation = new MathBlock(CreateText("A=")).Join(equationArray).Join(CreateText("=B"));
        mathParagraph.Add(equation);

        presentation.Save(fileName, SaveFormat.Pptx);
    }

}

MathematicalText CreateText(string value)
{
    var text = new MathematicalText(value);
    text.Format.FontHeight = 24;
    text.Format.LatinFont = new FontData("Cambria Math");
    return text;
}

数组行间距独立于段落间距。SpaceBefore 和 SpaceAfter 为包含段落的上下添加空白,而非数组行之间的间距。同样,IMathParagraph.Justification 横向定位整段数学内容,不会替代数组的垂直对齐设置。

添加三角函数

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

三角函数 cos 作用于 2x

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var cosine = new MathematicalText("2x")
    .AsArgumentOfFunction(MathFunctionsOfOneArgument.Cos);

mathParagraph.Add(new MathBlock(cosine));

presentation.Save("trigonometric-function.pptx", SaveFormat.Pptx);

添加下标和上标

使用下标和上标帮助函数处理索引和指数。当索引需要出现在基数的左侧时,使用 SetSubSuperscriptOnTheLeft。

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

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var scripts = new MathematicalText("Y")
    .SetSubSuperscriptOnTheLeft("1", "n");

mathParagraph.Add(new MathBlock(scripts));

presentation.Save("subscript-superscript.pptx", SaveFormat.Pptx);

添加定界符

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

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

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var delimiter = new MathematicalText("x")
    .Join("y")
    .Join("z")
    .Enclose('<', '>');
delimiter.SeparatorCharacter = '|';

mathParagraph.Add(new MathBlock(delimiter));

presentation.Save("delimiters.pptx", SaveFormat.Pptx);

定界符的大小和对齐

将 GrowToMatchOperandHeight 设为 true,使括号在遇到高操作数时自动增大;设为 false 则保持正常大小。DelimiterShape 采用两种 MathDelimiterShape 值之一。Centered 将括号在数学轴上居中(该轴穿过等号中线),Match 则与所包围内容对齐。这两种设置仅影响定界符本身,内部表达式保持不变。

示例在 x 的指数位置放置分数,使表达式在基线以上的伸展程度超过基线以下。所有四个方程式使用相同的括号和字体设置。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

void AddExpression(float left, float top, bool grow, MathDelimiterShape shape)
{
    var fraction = new MathematicalText("a + b").Divide("c + d");
    var power = new MathematicalText("x").SetSuperscript(fraction);
    var delimiter = power.Enclose('(', ')');
    delimiter.GrowToMatchOperandHeight = grow;
    delimiter.DelimiterShape = shape;

    var mathShape = slide.Shapes.AddMathShape(left, top, 300, 100);
    var portion = (MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0];
    portion.PortionFormat.FontHeight = 32;
    var equation = new MathematicalText("y = ").Join(delimiter);
    portion.MathParagraph.Add(equation);
}

AddExpression(20, 20, true, MathDelimiterShape.Centered);
AddExpression(350, 20, true, MathDelimiterShape.Match);
AddExpression(20, 140, false, MathDelimiterShape.Centered);
AddExpression(350, 140, false, MathDelimiterShape.Match);

presentation.Save("delimiter-sizing.pptx", SaveFormat.Pptx);

在 PowerPoint 中比较各行可看到增长效果。顶部一行的括号会随指数伸展并在两种形状下表现相同;底部一行关闭了增长。左侧的较小括号(Centered)与等号对齐,而右侧的(Match)则在表达式上方更高。

PowerPoint 中四个方程式,展示定界符在上方启用增长、下方关闭,以及左侧 Centered 与右侧 Match 的对比

添加边框框

对分数调用 ToBorderBox 可为整个表达式添加边框。返回的 IMathBorderBox 默认显示四条边。使用 HideTop、HideBottom、HideLeft、HideRight 可单独去除边。

示例在同一幻灯片上放置六个相同的分数副本。为展示消除效果,隐藏四条边并设置 StrikethroughBottomLeftToTopRight 形成上升对角线(↗)或 StrikethroughTopLeftToBottomRight 形成下降对角线(↘)。也可以使用 StrikethroughHorizontal 或 StrikethroughVertical 并任意组合可见的边。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
presentation.SlideSize.SetSize(540, 220, SlideSizeScaleType.DoNotScale);
var slide = presentation.Slides[0];

IMathBorderBox AddFraction(float x, float y, bool hideBorder = false)
{
    var fraction = new MathematicalText("x+1").Divide("y+2");
    var box = fraction.ToBorderBox();
    box.HideTop = box.HideBottom = box.HideLeft = box.HideRight = hideBorder;

    var shape = slide.Shapes.AddMathShape(x, y, 160, 90);
    var portion = (MathPortion)shape.TextFrame.Paragraphs[0].Portions[0];
    portion.PortionFormat.FontHeight = 28;
    portion.MathParagraph.Add(new MathBlock(box));
    return box;
}

AddFraction(10, 10);

var partial = AddFraction(190, 10);
partial.HideTop = true;
partial.HideRight = true;

AddFraction(370, 10, hideBorder: true).StrikethroughBottomLeftToTopRight = true;
AddFraction(10, 120, hideBorder: true).StrikethroughTopLeftToBottomRight = true;
AddFraction(190, 120, hideBorder: true).StrikethroughHorizontal = true;
AddFraction(370, 120).StrikethroughVertical = true;

presentation.Save("border-box.pptx", SaveFormat.Pptx);

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

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

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

将表达式包裹在 MathPhantom 中,可独立控制其可见性以及对方程式布局的贡献。IMathPhantom 的属性控制以下方面:

属性 效果
Show 设置为 false 可隐藏表达式。除非下面的属性为 true,否则其尺寸仍会影响布局。
ZeroWidth 将表达式的水平宽度视为零,使相邻元素更靠近。
ZeroAsc 将其上升(基线以上的高度)视为零,可减少邻近元素上方预留的空间。
ZeroDesc 将其下降(基线以下的深度)视为零,可使分数线或其他内容更靠近下方邻近元素。

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

下面的示例创建了五种并排变体:一个可见分数、一个保留空间的隐藏分数,以及三个分别启用 ZeroWidth、ZeroAsc 或 ZeroDesc 的隐藏分数。每种变体使用相同的外围表达式和 Cambria Math,字号 24 点。所有方程式形状的顶部位置相同,由灰色参考线标记。在外部分数的分子中放置幻影,可更明显地看到垂直布局的变化。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
presentation.SlideSize.SetSize(1000, 180, SlideSizeScaleType.DoNotScale);
var slide = presentation.Slides[0];

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

var guide = slide.Shapes.AddAutoShape(ShapeType.Line, 10, 60, 980, 0);
guide.LineFormat.FillFormat.FillType = FillType.Solid;
guide.LineFormat.FillFormat.SolidFillColor.Color = System.Drawing.Color.LightGray;

for (var i = 0; i < labels.Length; i++)
{
    var left = 10 + i * 200;
    
    var label = slide.Shapes.AddAutoShape(ShapeType.Rectangle, left, 10, 190, 30);
    label.FillFormat.FillType = FillType.NoFill;
    label.LineFormat.FillFormat.FillType = FillType.NoFill;
    label.TextFrame.Text = labels[i];

    var labelFormat = label.TextFrame.Paragraphs[0].Portions[0].PortionFormat;
    labelFormat.FontHeight = 14;
    labelFormat.FillFormat.FillType = FillType.Solid;
    labelFormat.FillFormat.SolidFillColor.Color = System.Drawing.Color.Black;

    var fraction = new MathFraction(CreateText("a+b"), CreateText("c+d"));
    var phantom = new MathPhantom(fraction);
    phantom.Show = i == 0;
    phantom.ZeroWidth = i == 2;
    phantom.ZeroAsc = i == 3;
    phantom.ZeroDesc = i == 4;

    var numerator = new MathBlock(CreateText("x+")).Join(phantom).Join(CreateText("+y"));
    var equation = new MathFraction(numerator, CreateText("z"));

    var shape = slide.Shapes.AddMathShape(left, 60, 190, 100);
    shape.TextFrame.TextFrameFormat.AnchoringType = TextAnchorType.Top;

    var mathParagraph = ((IMathPortion)shape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
    mathParagraph.Add(new MathBlock(equation));
}

presentation.Save("math-phantom.pptx", SaveFormat.Pptx);

MathematicalText CreateText(string value)
{
    var text = new MathematicalText(value);
    text.Format.FontHeight = 24;
    text.Format.LatinFont = new FontData("Cambria Math");
    return text;
}

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

分组项

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

表达式 x+y 被分组,下方带有标签 any text

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 120);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var 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);

格式化数学元素

仅在能提升公式可读性的情况下使用格式化帮助方法。例如,Overbar 可在数学元素上方添加横线。

带有上横线的数学表达式 ABC

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;

var overbar = new MathematicalText("ABC").Overbar();

mathParagraph.Add(new MathBlock(overbar));

presentation.Save("overbar.pptx", SaveFormat.Pptx);

格式化现有方程式中的文本

要在现有方程式中格式化文本,需要遍历每个数学块的元素树。IMathElement.GetChildren 仅返回直接子元素,需递归调用以进入分数、上下标、根式等嵌套结构。既要检查当前元素,也要检查其子元素,以在元素为 MathematicalText 时也能包含在内。

该示例假设演示文稿中包含类似 z = xᵢ / ∛y 的方程式,其中分数的分子带下标,分母带根式。仅处理块的顶层元素会遗漏分数内部的文本。示例在幻灯片文本框中查找数学段落,通过 IMathPortion.MathParagraph 访问其块,并将每个文本元素的 Format 中的 FontHeight 设为 28 点。

using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;

using var presentation = new Presentation("input.pptx");

foreach (var slide in presentation.Slides)
{
    foreach (var shape in slide.Shapes)
    {
        if (shape is not IAutoShape autoShape || autoShape.TextFrame == null)
            continue;

        foreach (var paragraph in autoShape.TextFrame.Paragraphs)
        {
            foreach (var portion in paragraph.Portions)
            {
                if (portion is IMathPortion mathPortion)
                {
                    foreach (var block in mathPortion.MathParagraph)
                        FormatMathText(block);
                }
            }
        }
    }
}

presentation.Save("formatted-equations.pptx", SaveFormat.Pptx);

void FormatMathText(IMathElement element)
{
    if (element is MathematicalText text)
        text.Format.FontHeight = 28;

    foreach (var child in element.GetChildren())
        FormatMathText(child);
}

保存的演示文稿在每个已处理方程式的每个层级都包含更新后的文本格式,同时保留了文本内容和数学结构。

快速参考

任务 主要 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 Math 内容。

我可以将方程式导出为 LaTeX 吗?

可以。从其 MathPortion 获取方程式的 IMathParagraph,然后调用 IMathParagraph.ToLatex 直接导出。完整示例请参阅 Export Math Equations from Presentations in .NET。