在 .NET 中為 PowerPoint 簡報新增數學方程式
概觀
PowerPoint 以 Office Math Markup Language (OMML) 儲存方程式。使用 Aspose.Slides for .NET,您可以以程式方式建立相同類型的數學內容:分數、根號、函式、極限、N 元運算子、矩陣、陣列以及格式化的數學區塊。
在 PowerPoint 中,使用者通常從 插入 > 方程式 加入方程式:

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

Aspose.Slides 透過三個主要物件建立該數學文字:
- 數學形狀,由 AddMathShape 建立,是包含方程式的形狀。
- MathPortion 在形狀的文字框內儲存數學內容。
- MathParagraph 包含一個或多個 MathBlock 物件。
大部分以下範例使用 MathematicalText 與來自 IMathElement 的串流方法,以保持程式碼簡潔易讀。
欲了解 MathML 匯出情境,請參閱 從簡報匯出數學方程式 (.NET)。
建立方程式
此範例建立一個數學形狀並加入畢氏定理:

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);
AddMathShape 會建立一個已包含數學段落的形狀。取得第一個 MathPortion、取得其 MathParagraph,並向其中加入數學區塊或數學元素。
新增分數
使用 Divide 建立分數。您可以使用 MathFractionTypes 選擇分數樣式。

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:
var stackedFraction = new MathematicalText("x + 1").Divide("y - 1", MathFractionTypes.Bar);
新增根號
使用 Radical 建立平方根、立方根或其他根號。當前元素成為底數,參數成為次方。

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。

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);
若使用自訂函式名稱,將函式名稱設為當前元素:
var customFunction = new MathematicalText("f").Function("x + 1");
新增 N 元運算子與積分
使用 Nary 來建立求和、聯集、交集等大型運算子。使用 Integral 來建立積分。兩種方法都可以設定上下限。

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:
var integralBase = new MathematicalText("x").Join(new MathematicalText("dx").ToBox());
var integral = integralBase.Integral(MathIntegralTypes.Simple, "0", "1");
新增矩陣
使用 MathMatrix 來建立列與欄。矩陣預設不含括號,若需要括號、方括號或大括號,請自行包住矩陣。

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。

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);
新增三角函式
當參數是當前元素且函式名稱已知時,使用 AsArgumentOfFunction。

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。

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 將表達式置於分隔符內。若分隔符表達式包含多個元素,亦可設定分隔字元。

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);
新增框線框
當方程式本身需要被框住時,使用 ToBorderBox。

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 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);
分組項目
使用 Group 將分組字符放在表達式上方或下方。加入上下限以標示分組的項目。

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 會在數學元素上方加一條橫線。

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);
快速參考
| 任務 | 主要 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。