เพิ่มสมการคณิตศาสตร์ในงานนำเสนอ PowerPoint บน .NET

Contents
[ ]

ภาพรวม

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

ใน PowerPoint ผู้ใช้มักเพิ่มสมการจาก Insert > Equation:

PowerPoint Insert tab with the Equation command selected

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

A PowerPoint slide containing an editable math equation

Aspose.Slides สร้างข้อความคณิตศาสตร์นั้นผ่านวัตถุหลักสามประเภท:

  • รูปร่างคณิตศาสตร์ที่สร้างด้วย AddMathShape, ซึ่งเป็นรูปร่างที่บรรจุสมการ
  • MathPortion เก็บเนื้อหาคณิตศาสตร์ภายในกรอบข้อความของรูปร่าง
  • MathParagraph ประกอบด้วยหนึ่งหรือหลายวัตถุ MathBlock

ตัวอย่างส่วนใหญ่ด้านล่างใช้ MathematicalText และวิธี fluent จาก IMathElement เพื่อให้โค้ดสั้นและอ่านง่าย

สำหรับสถานการณ์การส่งออก MathML ดู Export Math Equations from Presentations in .NET

สร้างสมการ

ตัวอย่างนี้ใช้ AddMathShape เพื่อสร้างรูปร่างคณิตศาสตร์และเพิ่มทฤษฎีบทพีทาโกรัส:

The equation c squared equals a squared plus b squared

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

รวมบล็อกคณิตศาสตร์

เมื่อส่วนของสมการสร้างแยกกันเป็นวัตถุ IMathBlock ให้ใช้ JoinBlock เพื่อรวมกัน ตัวอย่างสร้าง c² = ในบล็อกซ้ายและ a² + b² ในบล็อกขวา SetSuperscript สร้างตัวเลขยกกำลังที่แก้ไขได้สำหรับทั้งสามกำลัง

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 คืนค่า leftBlock เอง ซึ่งตอนนี้มีส่วนทั้งสองเรียงลำดับแล้ว และทำให้ rightBlock ว่างเปล่า เพิ่มบล็อกที่รวมแล้วลงใน IMathParagraph เพียงครั้งเดียว หลังจากเปิด equation.pptx อีกครั้ง ย่อหน้าจะมีบล็อกเดียวแทนที่ c² = a² + b² โดยที่ตัวเลขยกกำลังทั้งสามยังคงเป็น superscript

การเรียก Add แบบ leftBlock.Add(rightBlock) จะพยายามฝังบล็อกหนึ่งภายในอีกบล็อกหนึ่งและจะเกิด InvalidOperationException

เพิ่มเศษส่วน

ใช้ Divide เพื่อสร้างเศษส่วน คุณสามารถเลือกสไตล์ของเศษส่วนด้วย MathFractionTypes

A skewed math fraction showing one divided by 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);

สำหรับเศษส่วนแบบ stacked ให้ใช้ MathFractionTypes.Bar

using Aspose.Slides.MathText;

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

เพิ่มราก

ใช้ Radical เพื่อสร้างรากที่สอง, รากที่สาม หรือรากอื่น ๆ สมาชิกปัจจุบันจะเป็นฐาน และอากิวเมนต์จะเป็นดีกรี

An n-th root radical expression with x under the radical sign

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

แสดงหรือซ่อนดีกรีของรากที่สอง

รากที่สองสามารถแสดงเลข 2 ชัดเจนหรือใช้สัญลักษณ์ √x ตามแบบดั้งเดิม คุณสมบัติ HideDegree ของ IMathRadical ควบคุมรูปแบบที่ปรากฏ ทั้งสองรูปแบบยังคงเก็บดีกรี 2 และ radicand x ไว้ในสมการที่แก้ไขได้

The same square root with HideDegree set to false on the left and true on the right

ตัวอย่างบันทึกรากที่สองแบบเดียวกันสองครั้ง ใน visible-degree.pptx ตัวเลข 2 จะมองเห็นได้ ใน hidden-degree.pptx จะถูกซ่อน

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

เพิ่มฟังก์ชันและขีดจำกัด

ใช้ AsArgumentOfFunction หรือ Function สำหรับฟังก์ชันเช่น sin(x), log(x), หรือชื่อฟังก์ชันที่กำหนดเอง สำหรับขีดจำกัด ให้ใส่ lim ลงใน MathLimit หรือใช้ SetLowerLimit

The limit of x as x approaches infinity

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-ary และอินทิกรัล

ใช้ Nary สำหรับผลรวม, ยูเนียน, อินเตอร์เซคชัน, และตัวดำเนินการขนาดใหญ่อื่น ๆ ใช้ Integral สำหรับอินทิกรัล ทั้งสองวิธีให้คุณกำหนดขีดจำกัดล่างและบน

A summation with lower and upper limits

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-ary ใช้สำหรับตัวดำเนินการขนาดใหญ่ที่มีขีดจำกัดแบบเลือกได้ ตัวดำเนินการธรรมดาเช่น +, -, และ = มักเพิ่มเป็น MathematicalText แล้วเชื่อมต่อเข้ากับนิพจน์

สำหรับอินทิกรัล ให้ใช้ Integral

using Aspose.Slides.MathText;

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

สร้างตัวดำเนินการรวม

เพื่อใช้ == เป็นตัวดำเนินการเดียวระหว่างสองตัวดำเนินการ ให้เรียก ToBox บน MathematicalText ที่บรรจุเครื่องหมายเท่าทั้งสอง จากนั้นบน IMathBox ที่ได้ ตั้งค่า OperatorEmulator เป็น true เพื่อให้กลุ่มทำหน้าที่เป็นตัวดำเนินการคณิตศาสตร์ ค่าเริ่มต้นคือ false

NoBreak ควบคุมการตัดบรรทัดภายในกล่อง ตัวอย่างตั้งค่าให้เป็น true ตามค่าเริ่มต้นเพื่อให้เครื่องหมายเท่าทั้งสองอยู่ติดกัน ส่วนที่เหลือของสมการยังคงหักบรรทัดได้

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

เพิ่มเมทริกซ์

ใช้ MathMatrix สำหรับแถวและคอลัมน์ เมทริกซ์โดยปกติจะไม่มีวงเล็บ จึงต้องใส่วงเล็บเองเมื่อจำเป็นต้องใช้วงกลม, วงเหลี่ยม หรือวงโค้ง

A two-row math matrix with one empty cell

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

แสดงหรือซ่อนตัวเติมในเซลล์เมทริกซ์ว่าง

โดยค่าเริ่มต้น IMathMatrix.HidePlaceholders เป็น false PowerPoint จะแสดงกล่องจุดในแต่ละเซลล์ว่างขณะแก้ไขสไลด์ ตั้งค่าเป็น true เพื่อซ่อนกล่องเหล่านั้น

ตัวอย่างต่อไปนี้วางเมทริกซ์ขนาด 2×2 สองอันข้างกัน แต่ละอันมี a และ b บนแนวทแยง ตัวเซลล์ที่เหลือเป็น 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);

เปิด matrix-placeholders.pptx ในมุมมอง Normal ของ PowerPoint คลิกภายในสมการใดสมการหนึ่งเพื่อแก้ไข แล้วคลิกที่พื้นที่ว่างของสไลด์เพื่อเปรียบเทียบเมทริกซ์ทั้งสอง

HidePlaceholders = false HidePlaceholders = true
PowerPoint shows dotted boxes in the two empty cells. PowerPoint shows only the diagonal entries, a and b.

การซ่อนตัวเติมทำให้แถวและคอลัมน์ทุกแถวคงที่เมทริกซ์ทั้งสองยังคงเป็น 2×2 และเซลล์ว่างยังคงเป็น null โดยไม่มีค่าศูนย์ กล่องจุดเป็นเครื่องมือช่วยแก้ไขใน PowerPoint

แก้ไขเมทริกซ์ที่มีอยู่

สมมติว่าบทเรียนพีชคณิตเชิงเส้นเริ่มด้วยเมทริกซ์สัมประสิทธิ์ใน matrix.pptx ซึ่งบันทึกจากตัวอย่างก่อนหน้า สำหรับแบบฝึกหัดต่อไป ให้คงสมการแรกไว้, แทนที่สมการที่สองด้วยสองสมการใหม่, แล้วปรับชุดตัวแปร เปิดเมทริกซ์ที่มีอยู่ผ่าน IMathMatrix เพื่อคงสัมประสิทธิ์ที่ยังใช้ได้

Original two-row coefficient matrix with entries 1, x, and an empty cell in the first row and x, 2, and y in the second

ดัชนีแถวและคอลัมน์เริ่มจากศูนย์และอ้างอิงเมทริกซ์ตามสภาพในแต่ละการเรียก แทรกแถวก่อนดัชนี 1 จะย้ายแถวที่สองเดิมไปเป็นดัชนี 2 แทรกหลังดัชนี 2 จะเพิ่มแถวหนึ่งแถว การลบแถว 2 จะลบสมการที่สองเดิมและย้ายแถวที่เพิ่มเข้ามาขึ้นหนึ่งตำแหน่ง คอลัมน์ก็เลื่อนไปในทิศเดียวกัน

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

// New cells are initially null. Set the nonzero coefficients first.
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 จัดแนวคอลัมน์แรกไปทางซ้าย SetColumnsAlignment จัดแนวสามคอลัมน์ต่อเนื่องไปทางขวา โดยเริ่มที่ดัชนี 1 ใน updated-matrix.pptx เมทริกซ์มีสามสมการและสี่ตัวแปร สัมประสิทธิ์แถวแรกเดิม 1 และ x ปรากฏในคอลัมน์แรกและคอลัมน์ที่สาม

Updated three-row matrix showing the rows 1, 0, x, 0; 12, 1, 0, 0; and 0, 0, 10, 1

ตั้งค่าการเว้นระยะแถวและคอลัมน์ของเมทริกซ์

ตัวอย่างสร้างเมทริกซ์ 2 × 2 ของเศษส่วนพร้อมความกว้างคอลัมน์ขั้นต่ำ 48 pt, ช่องว่างคอลัมน์ 12 pt, และช่องว่างแถว 1.5 บรรทัด

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 กำหนดความกว้างคอลัมน์ขั้นต่ำเป็น twips (20 twips = 1 point) ColumnGap เพิ่มระยะห่างระหว่างคอลัมน์ที่อยู่ติดกัน ทั้งสองค่ามีผลต่อระยะจากจุดเริ่มต้นของคอลัมน์หนึ่งไปยังคอลัมน์ถัดไป

ColumnGapRule และ RowGapRule กำหนดค่า MathSpacingRules สำหรับแต่ละทิศทาง ด้วย Exactly ค่าช่องว่างทั้งสองวัดเป็น twips ด้วย Multiple หน่วยของ ColumnGap แทน 0.5 em, หน่วยของ RowGap แทนครึ่งบรรทัด กฎการเว้นระยะคอลัมน์อื่น ๆ จะละเว้น ColumnGap

การตั้งค่าเหล่านี้ใช้ภายในเมทริกซ์ การเว้นระยะย่อหน้าควบคุมการจัดวางข้อความโดยรอบ สำหรับแถวภายในอาเรย์สมการ ให้ดูที่ Configure Row Spacing

เพิ่มอาเรย์สมการ

ใช้ ToMathArray เมื่อคุณต้องการสมการที่จัดแนวหรือสแต็กแนวตั้งของนิพจน์

A vertical math array with x above 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 pt เพียงการจัดแนวอาเรย์ที่ต่างกัน

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 ข้างแต่ละอาเรย์

Equation arrays in PowerPoint with Top, Center, and Bottom alignment and identical neighboring elements

กำหนดการเว้นระยะแถว

RowSpacingRule เลือกกฎการเว้นระยะสำหรับแถวภายในอาเรย์ RowSpacing ให้ค่าตัวเลขสำหรับกฎที่ระบุอย่างชัดเจนและจะถูกละเว้นโดยกฎตั้งต้น

สำหรับการเว้นระยะที่แม่นยำ ค่าใช้หน่วยเป็น points สำหรับการเว้นระยะเชิงสัดส่วน ค่าใช้เป็นครึ่งบรรทัด: ค่า 3 ในตัวอย่างหมายถึง 1.5 บรรทัด ตั้งค่าทั้งสองคุณสมบัติเมื่อต้องการค่าที่ระบุอย่างชัดเจน; การเปลี่ยนค่าเลขเดี่ยวเท่านั้นจะไม่เลือกกฎการเว้นระยะ

ตัวอย่างต่อไปนี้บันทึกพรีเซนเทชัน 5 ครั้งด้วยสมการ, การจัดแนว, และการตั้งค่าแบบอักษรเดียวกัน 3 ครั้งแรกใช้ค่าตั้งต้น; 2 ครั้งสุดท้ายแสดงค่าที่ระบุอย่างชัดเจน

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 เมื่ออากิวเมนต์เป็นสมาชิกปัจจุบันและชื่อฟังก์ชันรู้จักแล้ว

The trigonometric function cos applied to 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

A capital Y with left-side subscript 1 and superscript 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 scripts = new MathematicalText("Y")
    .SetSubSuperscriptOnTheLeft("1", "n");

mathParagraph.Add(new MathBlock(scripts));

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

จัดแนวตัวล่างและตัวบนทางขวา

เมื่อตัวล่างและตัวบนอ้างอิงฐานเดียวกัน ให้ใช้ MathRightSubSuperscriptElement คุณสมบัติ AlignScripts ควบคุมตำแหน่งแนวนอนของพวกมัน: false (ค่าเริ่มต้น) ให้พวกมันตามโค้งของฐาน, true จัดแนวตัวล่างและตัวบนให้สอดคล้องกันด้านแนวนอน

ตัวอย่างนี้วางสำเนาสองสำเนาของนิพจน์ที่แก้ไขได้เคียงข้างกัน ทั้งสองใช้ตัวอักษรเอียงคณิตศาสตร์ f (U+1D453), ตัวล่าง 1, ตัวบน 2, และ Cambria Math ขนาด 72 pt ฐานเอียงทำให้เห็นความแตกต่างใน PowerPoint ก่อนเรียกใช้ตัวอย่างให้ติดตั้ง Cambria Math

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

Two editable expressions in PowerPoint with AlignScripts set to false on the left and true on the right

ด้านซ้าย ตัวล่างเริ่มอยู่ซ้ายกว่าตัวบนตามโค้งของฐาน ด้านขวาตัวล่างและตัวบนจัดแนวกันด้านแนวนอน

เพิ่มตัวแบ่ง

ใช้ Enclose เพื่อใส่นิพจน์ไว้ในเครื่องหมายแบ่ง คุณยังสามารถกำหนดอักขระคั่นสำหรับนิพจน์ที่มีหลายองค์ประกอบได้

A delimiter expression containing x, y, and z separated by vertical bars

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) อยู่สูงขึ้นรอบนิพจน์

Four equations in PowerPoint with delimiter growth enabled above and disabled below, and Centered on the left and Match on the right

เพิ่มกล่องกรอบ

เรียก 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);

Six editable fractions in PowerPoint. Top row: full border, bottom and left sides, rising diagonal. Bottom row: falling diagonal, horizontal strike, vertical strike with a full border.

บนเศษส่วนนี้ เส้นขีดแนวนอนทับบาร์เศษส่วน เส้นขีดแนวตั้งข้ามตัวเศษและส่วนล่างพร้อมกรอบที่ยังคงมองเห็นได้ เส้นเหล่านี้เป็นการประดับนิพจน์ทั้งหมด ข้อความที่มีเส้นขีดธรรมดาใช้กับอักขระในรันข้อความ หลังจากเปิดพรีเซนเทชันใน PowerPoint อีกครั้ง คุณยังสามารถแก้ไขตัวเศษและส่วนล่างแยกกันได้

ควบคุมการมองเห็นและการจัดวางด้วย Phantom Elements

ใส่นิพจน์ใน MathPhantom เพื่อควบคุมการมองเห็นและพื้นที่ที่มันทำให้กับสมการโดยอิสระ คุณสมบัติของ IMathPhantom ควบคุมดังนี้

Property Effect
Show ตั้งเป็น false เพื่อซ่อนนิพจน์ มิติของมันยังคงมีผลต่อเลย์เอาต์ เว้นแต่คุณสมบัติด้านล่างตั้งเป็น true
ZeroWidth ปฏิบัติตามความกว้างแนวนอนของนิพจน์เป็นศูนย์ ทำให้องค์ประกอบข้างเคียงย้ายเข้ามาใกล้กัน
ZeroAsc ปฏิบัติด้านขึ้น (ความสูงเหนือฐาน) เป็นศูนย์ สามารถลดพื้นที่ด้านบนขององค์ประกอบข้างเคียง
ZeroDesc ปฏิบัติด้านลง (ความลึกใต้ฐาน) เป็นศูนย์ สามารถทำให้บาร์เศษส่วนหรือเนื้อหาอื่น ๆ ใต้นิพจน์ย้ายเข้ามาใกล้กัน

เส้นฐานคือเส้นอ้างอิงที่อักขระคณิตศาสตร์ทั่วไปวางอยู่ การซ่อนเนื้อหาไม่ลบมิติของมัน และการทำให้มิติเป็นศูนย์ก็ไม่ลบนิพจน์ออก นิพจน์พื้นฐานยังคงเข้าถึงได้ผ่าน Base ตั้งค่า Show เป็น true เพื่อแสดงอีกครั้ง; การตั้งค่ามิติยังคงอยู่

ตัวอย่างด้านล่างสร้างห_VARIANTS_ห้าตัวเคียงข้าง: เศษส่วนที่มองเห็นได้, เศษส่วนที่ซ่อนแต่ยังคงจองพื้นที่, และเศษส่วนที่ซ่อนสามแบบโดยมิติหนึ่งเป็นศูนย์ แต่ละแบบใช้นิพจน์รอบข้างและ Cambria Math ขนาด 24 pt ทุกรูปร่างสมการมีตำแหน่งบนเดียวกันที่ทำเครื่องหมายด้วยไกด์สีเทา การใส่ phantom ไว้ในตัวเศษของเศษส่วนภายนอกทำให้การเปลี่ยนแปลงการจัดวางแนวตั้งเห็นได้ง่ายกว่า

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;
}

Five equations in PowerPoint comparing a visible fraction, a hidden fraction that reserves space, and hidden fractions with ZeroWidth, ZeroAsc, or ZeroDesc enabled

จัดกลุ่มเทอม

ใช้ Group เพื่อวางอักขระจัดกลุ่มเหนือหรือใต้นิพจน์ เพิ่มขีดจำกัดเพื่อระบุเทอมที่จัดกลุ่ม

The expression x plus y grouped with the label any text below it

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 วางเส้นบาร์เหนือองค์ประกอบคณิตศาสตร์

A math expression ABC with an overbar

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

ใช้ฟอนต์สัญลักษณ์ในสมการ

เพื่อใส่ลูกศร Wingdings ระหว่างสองตัวดำเนินการ ให้สร้าง MathematicalText แยกสำหรับลูกศร ตั้งค่า SymbolFont ผ่าน MathematicalText.Format วิธีนี้ทำให้เฉพาะลูกศรใช้ฟอนต์ Wingdings และคงฟอร์แมตของตัวดำเนินการทั้งสองไว้

เลือกรหัสอักขระและฟอนต์สัญลักษณ์พร้อมกัน Wingdings แสดง U+F0E0 เป็นลูกศรขวา ฟอนต์อื่นอาจแสดง glyph ต่างกันหรือไม่มี 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 แสดงสมการที่บันทึกตามภาพด้านล่าง ทั้งสามองค์ประกอบยังคงแก้ไขได้

Editable equation with a Wingdings right arrow between A and B

LoadOptions.DefaultSymbolFont กำหนดฟอนต์สัญลักษณ์เริ่มต้นเมื่อโหลดพรีเซนเทชัน การกำหนดฟอนต์สัญลักษณ์ให้กับองค์ประกอบเดี่ยวจะไม่ฝังฟอนต์ในพรีเซนเทชัน Wingdings ต้องพร้อมใช้งานในสภาพแวดล้อมการเรนเดอร์

จัดรูปแบบข้อความในสมการที่มีอยู่

เพื่อจัดรูปแบบข้อความทั่วทั้งสมการที่มีอยู่ ให้ไล่ผ่านต้นไม้ขององค์ประกอบแต่ละบล็อกคณิตศาสตร์ IMathElement.GetChildren คืนค่าเฉพาะลูกโดยตรง ดังนั้นต้องเรียกแบบ recursive เพื่อเข้าถึงข้อความภายในเศษส่วน, ตัวล่างบน, ราก, และโครงสร้างซับอื่น ๆ ตรวจสอบสมาชิกปัจจุบันพร้อมลูกของมันเพื่อให้รวมสมาชิกเริ่มต้นเมื่อเป็น MathematicalText

ตัวอย่างนี้สมมติว่าพรีเซนเทชันมีสมการเช่น z = xᵢ / ∛y โดยเศษส่วนมีตัวล่างในตัวเศษและรากในตัวส่วน การประมวลผลเฉพาะสมาชิกระดับบนสุดของบล็อกจะพลาดข้อความในเศษส่วน ตัวอย่างค้นหาส่วนคณิตศาสตร์ในกรอบข้อความสไลด์ เข้าถึงบล็อกผ่าน IMathPortion.MathParagraph แล้วตั้งค่า FontHeight เป็น 28 pt ผ่านคุณสมบัติ 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);
}

พรีเซนเทชันที่บันทึกจะมีการจัดรูปแบบข้อความที่อัปเดตในทุกระดับของแต่ละสมการที่ประมวลผล ขณะเดียวกันคงเนื้อหาและโครงสร้างคณิตศาสตร์ไว้

อ้างอิงอย่างรวดเร็ว

Task Main API
Create math text MathematicalText
Combine elements IMathElement.Join
Create fractions IMathElement.Divide
Add superscript or subscript SetSuperscript, SetSubscript
Add functions Function, AsArgumentOfFunction
Add radicals IMathElement.Radical
Add limits SetLowerLimit, SetUpperLimit
Add left-side scripts SetSubSuperscriptOnTheLeft
Add summations and integrals Nary, Integral
Add matrices MathMatrix
Add equation arrays ToMathArray
Add delimiters Enclose
Add bars and borders Overbar, ToBorderBox
Group terms Group

FAQ

Can I edit an existing PowerPoint equation?

Yes. Open the presentation, find the shape that contains a MathPortion, get its MathParagraph, and update the math blocks in that paragraph.

Are equations saved as editable PowerPoint math?

Yes. When you save to PPTX, Aspose.Slides writes the equation as editable Office math content.

Can I export equations to LaTeX?

Yes. Get the equation’s IMathParagraph from its MathPortion, and call IMathParagraph.ToLatex to export it directly. For a complete example, see Export Math Equations from Presentations in .NET.