Thêm Phương trình Toán học vào Bài thuyết trình PowerPoint trong .NET
Tổng quan
PowerPoint lưu các phương trình dưới dạng Office Math Markup Language (OMML). Với Aspose.Slides for .NET, bạn có thể tạo các nội dung toán học tương tự một cách lập trình: phân số, căn bậc, hàm, giới hạn, toán tử N-ary, ma trận, mảng và các khối toán học được định dạng.
Trong PowerPoint, người dùng thường thêm phương trình bằng Insert > Equation:

Kết quả là văn bản toán học có thể chỉnh sửa trên slide:

Aspose.Slides xây dựng văn bản toán học này qua ba đối tượng chính:
- Một hình toán học, được tạo bằng AddMathShape, là hình chứa phương trình.
- MathPortion lưu nội dung toán học bên trong khung văn bản của hình.
- MathParagraph chứa một hoặc nhiều đối tượng MathBlock.
Hầu hết các ví dụ dưới đây sử dụng MathematicalText và các phương thức fluent từ IMathElement để giữ code ngắn gọn và dễ đọc.
Đối với các kịch bản xuất MathML, xem Export Math Equations from Presentations in .NET.
Tạo một Phương trình
Ví dụ này sử dụng AddMathShape để tạo một hình toán và thêm định lý Pythagoras:

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);
Note
AddMathShape tạo một hình đã bao gồm một đoạn toán. Truy cập MathPortion đầu tiên, lấy MathParagraph của nó, và thêm các khối hoặc phần tử toán vào đó.Kết hợp các Khối Toán
Khi các phần của một phương trình được xây dựng độc lập dưới dạng đối tượng IMathBlock, sử dụng JoinBlock để ghép chúng lại. Ví dụ xây dựng c² = trong khối trái và a² + b² trong khối phải. SetSuperscript tạo các chỉ số mũ có thể chỉnh sửa cho cả ba lũy thừa.
using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddMathShape(20, 20, 600, 100);
var mathParagraph = ((IMathPortion)shape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var leftBlock = new MathBlock();
leftBlock.Add(new MathematicalText("c").SetSuperscript("2"));
leftBlock.Add(new MathematicalText("="));
var rightBlock = new MathBlock();
rightBlock.Add(new MathematicalText("a").SetSuperscript("2"));
rightBlock.Add(new MathematicalText("+"));
rightBlock.Add(new MathematicalText("b").SetSuperscript("2"));
var combinedBlock = leftBlock.JoinBlock(rightBlock);
mathParagraph.Add(combinedBlock);
presentation.Save("equation.pptx", SaveFormat.Pptx);
JoinBlock trả về chính leftBlock, giờ đã chứa cả hai phần theo thứ tự, và để rightBlock trống. Thêm khối đã ghép vào IMathParagraph chỉ một lần. Sau khi mở lại equation.pptx, đoạn văn chứa một khối đại diện cho c² = a² + b², với cả ba lũy thừa vẫn được giữ dưới dạng chỉ số mũ.
Gọi Add dưới dạng leftBlock.Add(rightBlock) sẽ cố gắng nhúng một khối bên trong khối khác và gây ra InvalidOperationException.
Thêm Phân số
Sử dụng Divide để tạo một phân số. Bạn có thể chọn kiểu phân số bằng MathFractionTypes.

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);
Đối với phân số chồng, sử dụng MathFractionTypes.Bar:
using Aspose.Slides.MathText;
var stackedFraction = new MathematicalText("x + 1").Divide("y - 1", MathFractionTypes.Bar);
Thêm Căn bậc
Sử dụng Radical để tạo căn bậc hai, căn bậc ba hoặc các căn bậc khác. Phần tử hiện tại trở thành cơ số, và đối số trở thành độ bậc.

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);
Hiển thị hoặc Ẩn Độ bậc của Căn bậc hai
Căn bậc hai có thể hiển thị số 2 rõ ràng hoặc dùng ký hiệu thông thường √x. Thuộc tính HideDegree của IMathRadical kiểm soát dạng hiển thị. Cả hai dạng đều giữ độ bậc 2 và radicand x trong phương trình có thể chỉnh sửa.

Ví dụ lưu cùng một căn bậc hai hai lần. Trong visible-degree.pptx số 2 hiện ra. Trong hidden-degree.pptx nó bị ẩn.
using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var radical = new MathematicalText("x").Radical("2");
var shape = slide.Shapes.AddMathShape(20, 20, 200, 100);
var portion = (IMathPortion)shape.TextFrame.Paragraphs[0].Portions[0];
portion.MathParagraph.Add(new MathBlock(radical));
radical.HideDegree = false;
presentation.Save("visible-degree.pptx", SaveFormat.Pptx);
radical.HideDegree = true;
presentation.Save("hidden-degree.pptx", SaveFormat.Pptx);
Thêm Hàm và Giới hạn
Sử dụng AsArgumentOfFunction hoặc Function cho các hàm như sin(x), log(x), hoặc tên hàm tùy chỉnh. Đối với giới hạn, đặt lim trong một MathLimit hoặc dùng SetLowerLimit.

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);
Đối với tên hàm tùy chỉnh, làm tên hàm thành phần tử hiện tại và gọi Function với đối số của nó:
using Aspose.Slides.MathText;
var customFunction = new MathematicalText("f").Function("x + 1");
Thêm Toán tử N-ary và Tích phân
Sử dụng Nary cho các tổng, hợp, giao và các toán tử lớn khác. Sử dụng Integral cho tích phân. Cả hai phương pháp đều cho phép đặt giới hạn dưới và trê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, 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);
Toán tử N-ary dành cho các toán tử lớn có thể có giới hạn tùy chọn. Các toán tử đơn giản như +, -, và = thường được thêm dưới dạng MathematicalText và nối vào biểu thức.
Đối với tích phân, sử dụng Integral:
using Aspose.Slides.MathText;
var integralBase = new MathematicalText("x").Join(new MathematicalText("dx").ToBox());
var integral = integralBase.Integral(MathIntegralTypes.Simple, "0", "1");
Tạo Toán tử Hợp Nhất
Để sử dụng == như một toán tử duy nhất giữa hai toán hạng, gọi ToBox trên MathematicalText chứa cả hai dấu bằng. Trên IMathBox kết quả, đặt OperatorEmulator thành true để nhóm hoạt động như một toán tử toán học. Mặc định là false.
NoBreak kiểm soát ngắt dòng bên trong hộp. Ví dụ đặt rõ ràng giá trị mặc định true để giữ hai dấu bằng liền nhau. Phần còn lại của phương trình vẫn có thể ngắt dòng.
using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddMathShape(20, 20, 300, 60);
var portion = (IMathPortion)shape.TextFrame.Paragraphs[0].Portions[0];
IMathBox compoundOperator = new MathematicalText("==").ToBox();
compoundOperator.OperatorEmulator = true;
compoundOperator.NoBreak = true;
var equation = new MathematicalText("a").Join(compoundOperator).Join(new MathematicalText("b"));
portion.MathParagraph.Add(equation);
presentation.Save("compound-operator.pptx", SaveFormat.Pptx);
Thêm Ma trận
Sử dụng MathMatrix cho hàng và cột. Mặc định ma trận không có dấu ngoặc, vì vậy hãy bao quanh ma trận khi bạn cần dấu ngoặc tròn, vuông hoặc ngoặc nhọ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, 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);
Hiển thị hoặc Ẩn Trình giữ chỗ trong Ô Ma trận Trống
Mặc định, IMathMatrix.HidePlaceholders là false. PowerPoint hiển thị một ô chấm trong mỗi ô trống khi bạn chỉnh sửa slide. Đặt thuộc tính này thành true để ẩn các ô đó.
Ví dụ sau đặt hai ma trận 2×2 cạnh nhau. Mỗi ma trận có a và b trên đường chéo. Hai ô còn lại để null.
using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
foreach (bool hide in new[] { false, true })
{
IMathMatrix matrix = new MathMatrix(2, 2);
matrix[0, 0] = new MathematicalText("a");
matrix[1, 1] = new MathematicalText("b");
matrix.HidePlaceholders = hide;
var shape = slide.Shapes.AddMathShape(hide ? 360 : 80, 150, 220, 120);
var portion = (IMathPortion)shape.TextFrame.Paragraphs[0].Portions[0];
portion.MathParagraph.Add(new MathBlock(matrix));
}
presentation.Save("matrix-placeholders.pptx", SaveFormat.Pptx);
Mở matrix-placeholders.pptx trong chế độ Normal của PowerPoint. Nhấp vào bất kỳ phương trình nào để chỉnh sửa, rồi nhấp vào vùng trống của slide để so sánh hai ma trận.
HidePlaceholders = false |
HidePlaceholders = true |
|---|---|
![]() |
![]() |
Việc ẩn các trình giữ chỗ giữ nguyên mọi hàng và cột. Cả hai ma trận vẫn là 2×2, và các ô trống vẫn là null mà không bị chuyển thành 0. Các ô chấm chỉ là hướng dẫn chỉnh sửa trong PowerPoint.
Chỉnh sửa Ma trận hiện có
Giả sử bài học đại số tuyến tính bắt đầu với ma trận hệ số trong matrix.pptx, được lưu bởi ví dụ trước. Đối với bài tập tiếp theo, giữ nguyên phương trình đầu tiên, thay thế phương trình thứ hai bằng hai phương trình mới, và điều chỉnh tập biến. Mở ma trận hiện có qua IMathMatrix để giữ các hệ số vẫn áp dụng.

Chỉ số hàng và cột bắt đầu từ zero và tham chiếu tới ma trận tại thời điểm mỗi lần gọi. Chèn một hàng trước chỉ số 1 sẽ đẩy hàng gốc thứ hai sang chỉ số 2. Chèn sau chỉ số 2 sẽ thêm một hàng mới. Xóa hàng 2 sẽ loại bỏ phương trình gốc thứ hai và di chuyển hàng mới lên một vị trí. Các cột cũng dịch tương tự.
using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;
using var presentation = new Presentation("matrix.pptx");
var shape = (IAutoShape)presentation.Slides[0].Shapes[0];
var portion = (IMathPortion)shape.TextFrame.Paragraphs[0].Portions[0];
var matrix = (IMathMatrix)portion.MathParagraph[0][0];
matrix.InsertRowBefore(1);
matrix.InsertRowAfter(2);
matrix.DeleteRow(2);
matrix.InsertColumnBefore(1);
matrix.InsertColumnAfter(3);
matrix.DeleteColumn(3);
// Các ô mới ban đầu là null. Đặt các hệ số khác không trước tiên.
matrix[1, 0] = new MathematicalText("12");
matrix[1, 1] = new MathematicalText("1");
matrix[2, 2] = new MathematicalText("10");
matrix[2, 3] = new MathematicalText("1");
for (var row = 0; row < matrix.RowCount; row++)
{
for (var column = 0; column < matrix.ColumnCount; column++)
{
matrix[row, column] ??= new MathematicalText("0");
}
}
matrix.SetColumnAlignment(0, MathHorizontalAlignment.Left);
matrix.SetColumnsAlignment(1, 3, MathHorizontalAlignment.Right);
presentation.Save("updated-matrix.pptx", SaveFormat.Pptx);
SetColumnAlignment căn trái cột đầu tiên. SetColumnsAlignment căn phải ba cột liên tiếp, bắt đầu từ chỉ số 1. Trong updated-matrix.pptx, ma trận có ba phương trình và bốn biến. Các hệ số gốc của hàng đầu 1 và x giờ xuất hiện ở cột thứ nhất và thứ ba.

Đặt Khoảng cách Hàng và Cột cho Ma trận
Ví dụ tạo một ma trận 2 × 2 gồm các phân số với chiều rộng cột tối thiểu 48 điểm, khoảng cách cột 12 điểm, và khoảng cách hàng 1,5 dòng.
using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddMathShape(20, 40, 300, 180);
var portion = (IMathPortion)shape.TextFrame.Paragraphs[0].Portions[0];
var matrix = new MathMatrix(2, 2);
matrix[0, 0] = new MathematicalText("1").Divide("2");
matrix[0, 1] = new MathematicalText("1").Divide("3");
matrix[1, 0] = new MathematicalText("2").Divide("3");
matrix[1, 1] = new MathematicalText("3").Divide("4");
matrix.MinColumnWidth = 960;
matrix.ColumnGapRule = MathSpacingRules.Exactly;
matrix.ColumnGap = 240;
matrix.RowGapRule = MathSpacingRules.Multiple;
matrix.RowGap = 3;
portion.MathParagraph.Add(new MathBlock(matrix));
presentation.Save("matrix-spacing.pptx", SaveFormat.Pptx);
MinColumnWidth đặt chiều rộng tối thiểu cho mỗi cột bằng twips (20 twips = 1 điểm). ColumnGap thêm khoảng cách giữa các cột kề nhau. Cả hai đều ảnh hưởng tới khoảng cách từ đầu một cột tới đầu cột tiếp theo.
ColumnGapRule và RowGapRule chọn giá trị MathSpacingRules cho mỗi hướng. Với Exactly, cả hai giá trị khoảng cách đều đo bằng twips. Với Multiple, mỗi đơn vị của ColumnGap tương đương 0.5 em, và mỗi đơn vị của RowGap tương đương nửa dòng. Các quy tắc khoảng cách cột khác sẽ bỏ qua ColumnGap.
Các thiết lập này chỉ áp dụng trong ma trận. Khoảng cách đoạn văn kiểm soát bố cục văn bản xung quanh. Đối với các hàng trong một mảng phương trình, xem Configure Row Spacing.
Thêm Mảng Phương trình
Sử dụng ToMathArray khi bạn cần các phương trình được căn chỉnh hoặc một ngăn xếp dọc các biểu thức.

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);
Căn chỉnh Mảng với Các Phần tử Xung quanh
IMathArray.BaseJustification điều khiển căn dọc của một mảng so với các phần tử toán học bên cạnh. Ví dụ dưới đây so sánh căn chỉnh với hàng đầu, trung tâm mảng, và hàng cuối.
Ví dụ sau tạo ba mảng giống hệt nhau, mỗi mảng có ba hàng và các phần tử lân cận A= và =B. Toàn bộ văn bản toán học dùng Cambria Math 24 điểm. Chỉ thay đổi cách căn mảng.
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;
}
So sánh sau hiển thị bản trình chiếu đã lưu trong PowerPoint. Lưu ý vị trí của A= và =B bên cạnh mỗi mảng.

Cấu hình Khoảng cách Hàng
RowSpacingRule chọn quy tắc khoảng cách cho các hàng trong mảng. RowSpacing cung cấp giá trị số cho quy tắc khoảng cách cụ thể và bị bỏ qua khi dùng các quy tắc mặc định.
Đối với khoảng cách chính xác, giá trị đo bằng điểm. Đối với khoảng cách tỷ lệ, đo bằng nửa dòng: giá trị 3 trong ví dụ tương đương 1,5 dòng. Khi chỉ định giá trị cụ thể, hãy đặt cả hai thuộc tính; thay đổi chỉ một giá trị số sẽ không chọn quy tắc.
Ví dụ dưới đây lưu năm bản trình chiếu với cùng phương trình, căn chỉnh và cài đặt phông. Ba bản đầu dùng các preset; hai bản cuối hiển thị giá trị cụ thể.
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;
}
Khoảng cách hàng trong mảng tách biệt với khoảng cách đoạn văn. SpaceBefore và SpaceAfter thêm không gian quanh đoạn văn chứa, không phải giữa các hàng mảng. Tương tự, IMathParagraph.Justification định vị toàn bộ đoạn toán theo chiều ngang; nó không thay thế cài đặt căn dọc của mảng.
Thêm Hàm lượng giác
Sử dụng AsArgumentOfFunction khi đối số là phần tử hiện tại và tên hàm đã biết.

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);
Thêm Chỉ số và Chỉ số trên
Sử dụng các trợ giúp chỉ số và chỉ số trên cho các chỉ mục và lũy thừa. Khi các chỉ mục phải xuất hiện phía bên trái của cơ số, dùng SetSubSuperscriptOnTheLeft.

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);
Căn chỉnh Chỉ số dưới và trên bên phải
Khi cả hai chỉ số thuộc cùng một cơ số, sử dụng MathRightSubSuperscriptElement. Thuộc tính AlignScripts điều khiển vị trí ngang của chúng: false (mặc định) cho phép chúng theo đường viền của cơ số, trong khi true căn các chỉ số ngang với nhau.
Ví dụ này đặt hai bản sao của cùng một biểu thức có thể chỉnh sửa cạnh nhau. Cả hai đều dùng ký tự in nghiêng toán học f (U+1D453), chỉ số dưới là 1, chỉ số trên là 2, và Cambria Math 72 điểm. Cơ số nghiêng làm sự khác biệt dễ quan sát trong PowerPoint. Hãy cài đặt Cambria Math trước khi chạy ví dụ.
using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;
using var presentation = new Presentation();
presentation.SlideSize.SetSize(900, 250, SlideSizeScaleType.DoNotScale);
var slide = presentation.Slides[0];
var format = new PortionFormat
{
FontHeight = 72,
LatinFont = new FontData("Cambria Math")
};
for (var i = 0; i < 2; i++)
{
var left = 40 + i * 440;
var shape = slide.Shapes.AddMathShape(left, 75, 380, 150);
var baseText = new MathematicalText("\U0001D453", format);
var subscript = new MathematicalText("1", format);
var superscript = new MathematicalText("2", format);
var scripts = new MathRightSubSuperscriptElement(baseText, subscript, superscript);
scripts.AlignScripts = i == 1;
var portion = (MathPortion)shape.TextFrame.Paragraphs[0].Portions[0];
portion.PortionFormat.FontHeight = format.FontHeight;
portion.PortionFormat.LatinFont = format.LatinFont;
portion.MathParagraph.Add(new MathBlock(scripts));
}
presentation.Save("aligned-scripts.pptx", SaveFormat.Pptx);

Ở bên trái, chỉ số dưới bắt đầu xa hơn phía trái so với chỉ số trên, theo đường viền cơ số. Ở bên phải, hai chỉ số được căn ngang với nhau.
Thêm Dấu phân cách
Sử dụng Enclose để đặt một biểu thức trong dấu phân cách. Bạn cũng có thể đặt ký tự ngăn cách cho các biểu thức chứa nhiều phần tử.

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);
Kích thước và Căn Đối tượng Phân cách
Đặt GrowToMatchOperandHeight thành true để cho ngoặc mở rộng quanh một toán hạng cao, hoặc false để giữ chúng ở kích thước bình thường. DelimiterShape nhận một trong hai giá trị MathDelimiterShape. Centered căn giữa ngoặc trên trục toán học, trục chạy qua giữa dấu bằng. Match căn các ngoặc với nội dung bao quanh. Cả hai chỉ ảnh hưởng tới dấu phân cách; biểu thức bên trong không đổi.
Ví dụ đặt một phân số ở mẫu số của x để biểu thức mở rộng cao hơn đường cơ sở so với phía dưới. Bốn phương trình đều dùng cùng một cặp ngoặc và cài đặt phông.
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);
Trong PowerPoint, so sánh các hàng để thấy hiệu ứng mở rộng. Ở hàng trên, ngoặc mở rộng để bao quanh mẫu số và trông giống nhau cho cả hai dạng. Ở hàng dưới, tính năng mở rộng tắt. Ngoặc nhỏ hơn ở bên trái (Centered) căn với dấu bằng, trong khi ngoặc bên phải (Match) cao hơn bao quanh biểu thức.

Thêm Hộp Viền
Gọi ToBorderBox trên một phân số để khung toàn bộ biểu thức. IMathBorderBox trả về mặc định hiển thị bốn phía. Dùng HideTop, HideBottom, HideLeft, và HideRight để loại bỏ các phía riêng lẻ.
Ví dụ đặt sáu bản sao của cùng một phân số trên một slide. Để hiển thị phép bớt, ẩn bốn phía và đặt StrikethroughBottomLeftToTopRight cho một đường chéo lên (↗) hoặc StrikethroughTopLeftToBottomRight cho một đường chéo xuống (↘). Bạn cũng có thể dùng StrikethroughHorizontal hoặc StrikethroughVertical kết hợp với bất kỳ các phía nào hiển thị.
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);

Trên phân số này, dấu gạch ngang chồng lên thanh phân số. Dấu gạch dọc cắt cả tử và mẫu trong khi khung bao quanh vẫn hiển thị. Các đường này trang trí toàn bộ biểu thức. Định dạng gạch ngang thông thường áp dụng cho ký tự trong một đoạn văn bản. Sau khi mở lại bản trình chiếu trong PowerPoint, bạn vẫn có thể chỉnh sửa tử và mẫu riêng biệt.
Kiểm soát Hiển thị và Bố cục với Phần tử Ảo (Phantom)
Bao bọc một biểu thức trong MathPhantom để kiểm soát việc hiển thị và không gian mà nó đóng góp cho phương trình một cách độc lập. Các thuộc tính của IMathPhantom kiểm soát các yếu tố sau:
| Property | Effect |
|---|---|
| Show | Đặt false để ẩn biểu thức. Kích thước của nó vẫn đóng góp vào bố cục trừ khi các thuộc tính bên dưới được đặt true. |
| ZeroWidth | Xem chiều rộng ngang của biểu thức là zero, cho phép các phần tử lân cận di chuyển lại gần nhau hơn. |
| ZeroAsc | Xem phần lên trên (ascent) của nó là zero, có thể giảm không gian dành cho phía trên các phần tử lân cận. |
| ZeroDesc | Xem phần xuống dưới (descent) của nó là zero, có thể đưa thanh phân số hoặc nội dung khác dưới biểu thức gần hơn các phần tử xung quanh. |
Đường cơ sở là đường tham chiếu mà các ký tự toán học bình thường nằm trên đó. Ẩn nội dung không loại bỏ kích thước của nó, và xem một kích thước là zero không xóa biểu thức. Biểu thức gốc vẫn tồn tại qua Base. Đặt lại Show thành true để hiển thị lại; các cài đặt kích thước vẫn giữ nguyên.
Ví dụ dưới tạo năm biến thể đặt cạnh nhau: một phân số hiển thị, một phân số ẩn nhưng vẫn giữ không gian, và ba phân số ẩn với một trong các kích thước được xem là zero. Mỗi biến thể dùng cùng một biểu thức bao quanh và Cambria Math 24 điểm. Tất cả các hình phương trình có cùng vị trí trên cùng, được đánh dấu bằng đường dẫn màu xám. Đặt phantom ở tử của một phân số ngoài giúp dễ quan sát thay đổi bố cục dọc.
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;
}

Nhóm các Thành phần
Sử dụng Group để đặt ký tự nhóm trên hoặc dưới một biểu thức. Thêm giới hạn để dán nhãn cho các thành phần đã nhóm.

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);
Định dạng Thành phần Toán học
Chỉ dùng các trợ giúp định dạng khi chúng làm rõ công thức. Ví dụ, Overbar đặt một thanh trên một thành phần toá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 overbar = new MathematicalText("ABC").Overbar();
mathParagraph.Add(new MathBlock(overbar));
presentation.Save("overbar.pptx", SaveFormat.Pptx);
Sử dụng Phông ký tự Symbol trong Phương trình
Để chèn một mũi tên Wingdings giữa hai toán hạng, tạo một phần tử MathematicalText riêng cho mũi tên. Đặt SymbolFont của nó qua MathematicalText.Format. Cách này chỉ áp dụng phông Wingdings cho mũi tên và giữ nguyên định dạng của cả hai toán hạng.
Chọn mã ký tự và phông Symbol cùng nhau. Wingdings hiển thị U+F0E0 dưới dạng mũi tên sang phải. Phông khác có thể hiển thị glyph khác tại mã này hoặc không có glyph.
using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.MathText;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddMathShape(20, 20, 240, 90);
var mathPortion = (IMathPortion)shape.TextFrame.Paragraphs[0].Portions[0];
var left = new MathematicalText("A");
var arrow = new MathematicalText("\uF0E0");
var right = new MathematicalText("B");
arrow.Format.SymbolFont = new FontData("Wingdings");
var block = new MathBlock();
block.Add(left);
block.Add(arrow);
block.Add(right);
mathPortion.MathParagraph.Add(block);
presentation.Save("symbol-font-arrow.pptx", SaveFormat.Pptx);
PowerPoint hiển thị phương trình đã lưu như dưới. Cả ba phần tử vẫn có thể chỉnh sửa.

LoadOptions.DefaultSymbolFont cấu hình phông Symbol mặc định khi tải bản trình chiếu. Gán phông Symbol cho một phần tử riêng không nhúng phông vào bản trình chiếu. Wingdings phải có sẵn trong môi trường render.
Định dạng Văn bản trong Phương trình hiện có
Để định dạng văn bản trên toàn bộ một phương trình hiện có, duyệt cây phần tử của mỗi khối toán. IMathElement.GetChildren chỉ trả về các con trực tiếp, vì vậy gọi đệ quy để tới văn bản bên trong phân số, chỉ số, căn bậc và các cấu trúc lồng nhau khác. Kiểm tra phần tử hiện tại cũng như các con để bao gồm phần tử bắt đầu khi nó là MathematicalText.
Ví dụ này giả định rằng một bản trình chiếu chứa một phương trình như z = xᵢ / ∛y, với một phân số chứa chỉ số trong tử và một căn bậc trong mẫu. Chỉ xử lý các phần tử cấp cao nhất của khối sẽ bỏ lỡ văn bản trong phân số. Ví dụ tìm các phần toán trong khung văn bản slide, truy cập các khối qua IMathPortion.MathParagraph, và đặt FontHeight thành 28 điểm cho mỗi phần tử văn bản qua thuộc tính Format.
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);
}
Bản trình chiếu đã lưu chứa định dạng văn bản mới ở mọi cấp của mỗi phương trình đã xử lý, đồng thời bảo toàn nội dung văn bản và cấu trúc toán học.
Tham khảo nhanh
| Task | Main API |
|---|---|
| Tạo văn bản toán học | MathematicalText |
| Kết hợp các thành phần | IMathElement.Join |
| Tạo phân số | IMathElement.Divide |
| Thêm chỉ số trên hoặc dưới | SetSuperscript, SetSubscript |
| Thêm hàm | Function, AsArgumentOfFunction |
| Thêm căn bậc | IMathElement.Radical |
| Thêm giới hạn | SetLowerLimit, SetUpperLimit |
| Thêm chỉ số bên trái | SetSubSuperscriptOnTheLeft |
| Thêm tổng và tích phân | Nary, Integral |
| Thêm ma trận | MathMatrix |
| Thêm mảng phương trình | ToMathArray |
| Thêm dấu phân cách | Enclose |
| Thêm thanh và viền | Overbar, ToBorderBox |
| Nhóm các thành phần | Group |
Câu hỏi thường gặp
Tôi có thể chỉnh sửa một phương trình PowerPoint hiện có không?
Có. Mở bản trình chiếu, tìm hình chứa MathPortion, lấy MathParagraph của nó, và cập nhật các khối toán trong đoạn văn đó.
Phương trình có được lưu dưới dạng toán học PowerPoint có thể chỉnh sửa không?
Có. Khi lưu dưới dạng PPTX, Aspose.Slides ghi phương trình dưới dạng nội dung Office Math có thể chỉnh sửa.
Tôi có thể xuất phương trình sang LaTeX không?
Có. Lấy IMathParagraph từ MathPortion của nó, và gọi IMathParagraph.ToLatex để xuất trực tiếp. Đối với một ví dụ đầy đủ, xem Export Math Equations from Presentations in .NET.

