在 .NET 中於 PowerPoint 簡報加入數學方程式

概觀

PowerPoint 以 Office Math Markup Language(OMML)儲存方程式。使用 Aspose.Slides for .NET,您可以以程式方式建立相同類型的數學內容:分數、根號、函式、極限、N 元運算子、矩陣、陣列以及格式化的數學區塊。

在 PowerPoint 中,使用者通常從 Insert > Equation 新增方程式:

PowerPoint 插入索標籤,選取方程式指令的畫面

結果是在投影片上可編輯的數學文字:

包含可編輯數學方程式的 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);

新增函式與極限

使用 AsArgumentOfFunction 或 Function 來建立 sin(x)、log(x) 或自訂函式名稱等函式。對於極限,將 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 提供數值型的明確間距,會在預設規則被覆寫時才生效。

若使用精確間距,數值的單位為點(pt)。若使用比例間距,則以半行為單位:範例中值 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)則在表達式上方稍高。

四個方程式,分別在括號伸長開啟與關閉的情況下,左側使用 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 開啟簡報後,仍可分別編輯分子與分母。

使用幻影元素控制可見性與版面配置

將表達式包在 MathPhantom 中,可獨立控制其可見性以及對方程式所佔的空間。IMathPhantom 的屬性說明如下:

Property Effect
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 這個表達式以分組符號包起,且在下方標示任意文字

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 數學內容。

我可以將方程式匯出為 LaTeX 嗎?

可以。從其 MathPortion 取得 IMathParagraph,然後呼叫 IMathParagraph.ToLatex 直接匯出。完整範例請參閱 Export Math Equations from Presentations in .NET。