เพิ่มสมการคณิตศาสตร์ในงานนำเสนอ PowerPoint ด้วย .NET
ภาพรวม
PowerPoint เก็บสมการเป็น Office Math Markup Language (OMML). ด้วย Aspose.Slides for .NET คุณสามารถสร้างเนื้อหาคณิตศาสตร์ประเภทเดียวกันโดยใช้โปรแกรม: เศษส่วน, ราก, ฟังก์ชัน, ขอบเขต, ตัวดำเนินการ N-ary, เมทริกซ์, อาร์เรย์, และบล็อกคณิตศาสตร์ที่มีรูปแบบ
ใน PowerPoint ผู้ใช้มักเพิ่มสมการจาก Insert > Equation:

ผลลัพธ์คือข้อความคณิตศาสตร์ที่สามารถแก้ไขได้บนสไลด์:

Aspose.Slides สร้างข้อความคณิตศาสตร์นั้นผ่านวัตถุหลักสามประเภท:
- รูปคณิตศาสตร์ที่สร้างด้วย AddMathShape, เป็นรูปที่บรรจุสมการ.
- MathPortion เก็บเนื้อหาคณิตศาสตร์ภายในกรอบข้อความของรูป.
- MathParagraph มีหนึ่งหรือมากกว่าอ็อบเจ็กต์ MathBlock
ตัวอย่างส่วนใหญ่ด้านล่างใช้ MathematicalText และเมธอดเชิง fluent จาก IMathElement เพื่อให้โค้ดย่อและอ่านง่าย.
สำหรับกรณีการส่งออก MathML ดูที่ Export Math Equations from Presentations in .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-ary และอินทิกรัล
ใช้ 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-ary ใช้สำหรับตัวดำเนินการขนาดใหญ่พร้อมขอบที่เป็นตัวเลือก. ตัวดำเนินการง่ายเช่น +, -, และ = โดยทั่วไปจะเพิ่มเป็น 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);
เพิ่มตัวเลขชี้ตำแหน่งและตัวเลขชี้กำลัง
ใช้ตัวช่วย subscript และ superscript สำหรับดัชนีและกำลัง. เมื่อดัชนีต้องแสดงที่ด้านซ้ายของฐาน, ใช้ 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 |
| เพิ่ม superscript หรือ subscript | 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 ได้หรือไม่?
ใช่. รับ IMathParagraph ของสมการจาก MathPortion, แล้วเรียก IMathParagraph.ToLatex เพื่อส่งออกโดยตรง. สำหรับตัวอย่างสมบูรณ์, ดูที่ Export Math Equations from Presentations in .NET.