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

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

Aspose.Slides สร้างข้อความคณิตศาสตร์นั้นผ่านสามวัตถุหลัก:
- รูปร่างคณิตศาสตร์ที่สร้างด้วย addMathShape คือรูปร่างที่บรรจุสมการ
- MathPortion เก็บเนื้อหาคณิตศาสตร์ภายในกรอบข้อความของรูปร่าง
- MathParagraph ประกอบด้วยหนึ่งหรือหลายอ็อบเจกต์ MathBlock
ส่วนใหญ่ของตัวอย่างด้านล่างใช้ MathematicalText และเมธอดแบบ fluent จาก MathElementBase เพื่อให้โค้ดสั้นและอ่านง่าย
สำหรับสถานการณ์การส่งออก MathML ดูที่ ส่งออกสมการคณิตศาสตร์จากการนำเสนอใน PHP ผ่าน Java
สร้างสมการ
ตัวอย่างนี้ใช้ addMathShape เพื่อสร้างรูปร่างคณิตศาสตร์และเพิ่มสูตรพีทาโกรัส:

$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 120);
$mathParagraph = $mathShape->getTextFrame()->getParagraphs()
- >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();
$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);
} finally {
if (!java_is_null($presentation)) {
$presentation->dispose();
}
}
Note
addMathShape สร้างรูปร่างที่มี MathParagraph อยู่แล้ว เข้าถึง MathPortion ตัวแรก, เรียก getMathParagraph และเพิ่มบล็อกคณิตศาสตร์หรือองค์ประกอบคณิตศาสตร์ลงไปเพิ่มเศษส่วน
ใช้ divide เพื่อสร้างเศษส่วน คุณสามารถเลือกสไตล์ของเศษส่วนด้วย MathFractionTypes

$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 100);
$mathParagraph = $mathShape->getTextFrame()->getParagraphs()
- >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();
$fraction = (new MathematicalText("1"))
- >divide("x", MathFractionTypes::Skewed);
$mathParagraph->add(new MathBlock($fraction));
$presentation->save("fraction.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($presentation)) {
$presentation->dispose();
}
}
สำหรับเศษส่วนที่ซ้อนกัน ใช้ MathFractionTypes::Bar:
$stackedFraction = (new MathematicalText("x + 1"))->divide("y - 1", MathFractionTypes::Bar);
เพิ่มราก
ใช้ radical เพื่อสร้างรากกำลังสอง, รากกำลังสาม หรือรากอื่น ๆ องค์ประกอบปัจจุบันจะเป็นฐานและอาร์กิวเมนต์จะเป็นดีกรี

$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 100);
$mathParagraph = $mathShape->getTextFrame()->getParagraphs()
- >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();
$radical = (new MathematicalText("x"))
- >radical("n");
$mathParagraph->add(new MathBlock($radical));
$presentation->save("radical.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($presentation)) {
$presentation->dispose();
}
}
เพิ่มฟังก์ชันและขีดจำกัด
ใช้ asArgumentOfFunction หรือ function สำหรับฟังก์ชันเช่น sin(x), log(x) หรือชื่อฟังก์ชันที่กำหนดเอง สำหรับขีดจำกัด ใส่ lim ใน MathLimit หรือใช้ setLowerLimit

$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 100);
$mathParagraph = $mathShape->getTextFrame()->getParagraphs()
- >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();
$limit = (new MathematicalText("lim"))
- >setLowerLimit("x\u{2192}\u{221E}")
- >function("x");
$mathParagraph->add(new MathBlock($limit));
$presentation->save("functions-and-limits.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($presentation)) {
$presentation->dispose();
}
}
สำหรับชื่อฟังก์ชันที่กำหนดเอง ทำให้ชื่อฟังก์ชันเป็นองค์ประกอบปัจจุบันและเรียก function พร้อมอาร์กิวเมนต์ของมัน:
$customFunction = (new MathematicalText("f"))->function("x + 1");
เพิ่มตัวดำเนินการ N-ary และอินทิกรัล
ใช้ nary สำหรับผลบวก, ยูเนียน, อินเตอร์เซคชัน, และตัวดำเนินการขนาดใหญ่อื่น ๆ ใช้ integral สำหรับอินทิกรัล ทั้งสองเมธอดให้คุณตั้งค่าขีดจำกัดล่างและบน

$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 120);
$mathParagraph = $mathShape->getTextFrame()->getParagraphs()
- >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();
$summationBase = (new MathematicalText("x"))
- >setSuperscript("k")
- >join((new MathematicalText("a"))->setSuperscript("n-k"));
$summation = $summationBase->nary(MathNaryOperatorTypes::Summation, "k=0", "n");
$mathParagraph->add(new MathBlock($summation));
$presentation->save("nary-operators.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($presentation)) {
$presentation->dispose();
}
}
ตัวดำเนินการ N-ary ใช้กับตัวดำเนินการขนาดใหญ่ที่มีขีดจำกัดเป็นตัวเลือก ตัวดำเนินการแบบธรรมดาเช่น +, -, และ = มักจะเพิ่มเป็น MathematicalText แล้วต่อเข้ากับนิพจน์
สำหรับอินทิกรัล ใช้ integral:
$integralBase = (new MathematicalText("x"))->join((new MathematicalText("dx"))->toBox());
$integral = $integralBase->integral(MathIntegralTypes::Simple, "0", "1");
เพิ่มเมทริกซ์
ใช้ MathMatrix สำหรับแถวและคอลัมน์ เมทริกซ์ไม่รวมวงเล็บโดยค่าเริ่มต้น ดังนั้นให้ล้อมเมทริกซ์เมื่อคุณต้องการวงเล็บ, วงกลม, หรือเครื่องหมายปีกกา

$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 120);
$mathParagraph = $mathShape->getTextFrame()->getParagraphs()
- >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();
$matrix = new MathMatrix(2, 3);
$matrix->set_Item(0, 0, new MathematicalText("1"));
$matrix->set_Item(0, 1, new MathematicalText("x"));
$matrix->set_Item(1, 0, new MathematicalText("x"));
$matrix->set_Item(1, 1, new MathematicalText("2"));
$matrix->set_Item(1, 2, new MathematicalText("y"));
$mathParagraph->add(new MathBlock($matrix));
$presentation->save("matrix.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($presentation)) {
$presentation->dispose();
}
}
เพิ่มอาเรย์สมการ
ใช้ toMathArray เมื่อคุณต้องการสมการจัดแนวหรือสแต็กแนวตั้งของนิพจน์

$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 140);
$mathParagraph = $mathShape->getTextFrame()->getParagraphs()
- >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();
$equationArray = (new MathematicalText("x"))
- >join("y")
- >toMathArray();
$mathParagraph->add(new MathBlock($equationArray));
$presentation->save("equation-array.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($presentation)) {
$presentation->dispose();
}
}
จัดแนวอาเรย์กับองค์ประกอบรอบข้าง
setBaseJustification ควบคุมการจัดตำแหน่งแนวตั้งของอาเรย์เทียบกับองค์ประกอบคณิตศาสตร์ข้างเคียง ตัวอย่างด้านล่างเปรียบเทียบการจัดแนวกับแถวแรก, ศูนย์กลางของอาเรย์, และแถวสุดท้าย
ตัวอย่างต่อไปนี้สร้างอาเรย์สามชุดที่เหมือนกัน แต่ละชุดมีสามแถวและองค์ประกอบข้างเคียง A= และ =B ข้อความคณิตศาสตร์ทั้งหมดใช้ Cambria Math ขนาด 24 จุด มีเพียงการจัดแนวอาเรย์ที่แตกต่างกัน
use aspose\slides\FillType;
use aspose\slides\FontData;
use aspose\slides\MathArray;
use aspose\slides\MathBlock;
use aspose\slides\MathJustification;
use aspose\slides\MathVerticalAlignment;
use aspose\slides\MathematicalText;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
use aspose\slides\SlideSizeScaleType;
use aspose\slides\TextAnchorType;
$createText = static function (string $value) {
$text = new MathematicalText($value);
$text->getFormat()->setFontHeight(24);
$text->getFormat()->setLatinFont(new FontData("Cambria Math"));
return $text;
};
$alignments = ["Top" => MathVerticalAlignment::Top, "Center" => MathVerticalAlignment::Center, "Bottom" => MathVerticalAlignment::Bottom];
$presentation = new Presentation();
try {
$presentation->getSlideSize()->setSize(780, 200, SlideSizeScaleType::DoNotScale);
$slide = $presentation->getSlides()->get_Item(0);
$x = 20;
foreach ($alignments as $name => $alignment) {
$label = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, $x, 10, 240, 30);
$label->getFillFormat()->setFillType(FillType::NoFill);
$label->getLineFormat()->getFillFormat()->setFillType(FillType::NoFill);
$label->getTextFrame()->setText($name);
$labelFormat = $label->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0)->getPortionFormat();
$labelFormat->setFontHeight(16);
$labelFormat->getFillFormat()->setFillType(FillType::Solid);
$labelFormat->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$mathShape = $slide->getShapes()->addMathShape($x, 60, 240, 130);
$mathShape->getTextFrame()->getTextFrameFormat()->setAnchoringType(TextAnchorType::Top);
$paragraph = $mathShape->getTextFrame()->getParagraphs()->get_Item(0);
$mathParagraph = $paragraph->getPortions()->get_Item(0)->getMathParagraph();
$mathParagraph->setJustification(MathJustification::LeftJustified);
$firstRow = $createText("x+y=3");
$equationArray = new MathArray($firstRow);
foreach (["x-y=1", "x=2"] as $value) {
$row = $createText($value);
$equationArray->getArguments()->add($row);
}
$equationArray->setBaseJustification($alignment);
$leftText = $createText("A=");
$rightText = $createText("=B");
$equation = (new MathBlock($leftText))->join($equationArray)->join($rightText);
$mathParagraph->add($equation);
$x += 260;
}
$presentation->save("equation-array-alignment.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
การเปรียบเทียบต่อไปนี้แสดงงานนำเสนอที่บันทึกไว้ใน PowerPoint ให้สังเกตตำแหน่งของ A= และ =B ข้างอาเรย์แต่ละอัน

กำหนดระยะห่างแถว
setRowSpacingRule เลือกกฎการเว้นระยะห่างสำหรับแถวภายในอาเรย์ setRowSpacing ให้ค่าตัวเลขสำหรับกฎการเว้นระยะห่างแบบเจาะจงและจะถูกละเว้นโดยกฎที่ตั้งไว้ล่วงหน้า
สำหรับการเว้นระยะห่างที่แน่นอน ค่าจะวัดเป็นจุด สำหรับการเว้นระยะห่างแบบสัดส่วนจะวัดเป็นครึ่งบรรทัด: ค่า 3 ในตัวอย่างหมายถึง 1.5 บรรทัด ตั้งค่าทั้งสองคุณสมบัติเมื่อต้องการกำหนดค่าตัวเลขแบบเจาะจง; การเปลี่ยนค่าเชิงตัวเลขอย่างเดียวจะไม่เลือกกฎการเว้นระยะห่าง
ตัวอย่างด้านล่างบันทึกงานนำเสนอห้าชุดที่มีสมการ, การจัดแนวและการตั้งค่าฟอนต์เดียวกัน ชุดแรกสามชุดใช้ค่าตั้งล่วงหน้า; สองชุดสุดท้ายแสดงค่าที่กำหนดเอง
use aspose\slides\FontData;
use aspose\slides\MathArray;
use aspose\slides\MathBlock;
use aspose\slides\MathJustification;
use aspose\slides\MathRowSpacingRule;
use aspose\slides\MathVerticalAlignment;
use aspose\slides\MathematicalText;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$createText = static function (string $value) {
$text = new MathematicalText($value);
$text->getFormat()->setFontHeight(24);
$text->getFormat()->setLatinFont(new FontData("Cambria Math"));
return $text;
};
$settings = [
"SingleLineGap" => [MathRowSpacingRule::SingleLineGap, 0],
"OneAndAHalfLineGap" => [MathRowSpacingRule::OneAndAHalfLineGap, 0],
"TwoLineGap" => [MathRowSpacingRule::TwoLineGap, 0],
"Exactly" => [MathRowSpacingRule::Exactly, 20],
"Multiple" => [MathRowSpacingRule::Multiple, 3],
];
foreach ($settings as $name => [$rule, $spacing]) {
$presentation = new Presentation();
try {
$mathShape = $presentation->getSlides()->get_Item(0)->getShapes()->addMathShape(20, 20, 600, 300);
$paragraph = $mathShape->getTextFrame()->getParagraphs()->get_Item(0);
$paragraph->getParagraphFormat()->setSpaceBefore(0);
$paragraph->getParagraphFormat()->setSpaceAfter(0);
$mathParagraph = $paragraph->getPortions()->get_Item(0)->getMathParagraph();
$mathParagraph->setJustification(MathJustification::LeftJustified);
$firstRow = $createText("x+y=3");
$equationArray = new MathArray($firstRow);
foreach (["x-y=1", "x=2"] as $value) {
$row = $createText($value);
$equationArray->getArguments()->add($row);
}
$equationArray->setBaseJustification(MathVerticalAlignment::Center);
$equationArray->setRowSpacingRule($rule);
$equationArray->setRowSpacing($spacing);
$leftText = $createText("A=");
$rightText = $createText("=B");
$equation = (new MathBlock($leftText))->join($equationArray)->join($rightText);
$mathParagraph->add($equation);
$presentation->save("equation-array-$name.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
}
ระยะห่างแถวของอาเรย์เป็นเรื่องแยกจากระยะห่างของย่อหน้า setSpaceBefore และ setSpaceAfter จะเพิ่มช่องว่างรอบย่อหน้าที่บรรจุ ไม่ได้เพิ่มระหว่างแถวของอาเรย์ เช่นเดียวกับ MathParagraph::setJustification จะจัดตำแหน่งแนวนอนของ MathParagraph ทั้งหมด; ไม่ได้แทนที่การตั้งค่าการจัดแนวแนวตั้งของอาเรย์
เพิ่มฟังก์ชันตรีโกณมิติ
ใช้ asArgumentOfFunction เมื่ออาร์กิวเมนต์เป็นองค์ประกอบปัจจุบันและชื่อฟังก์ชันทราบแล้ว

$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 100);
$mathParagraph = $mathShape->getTextFrame()->getParagraphs()
- >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();
$cosine = (new MathematicalText("2x"))
- >asArgumentOfFunction(MathFunctionsOfOneArgument::Cos);
$mathParagraph->add(new MathBlock($cosine));
$presentation->save("trigonometric-function.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($presentation)) {
$presentation->dispose();
}
}
เพิ่มตัวห้อยซับสคริปต์และซูเปอร์สคริปต์
ใช้ตัวช่วยซับสคริปต์และซูเปอร์สคริปต์สำหรับดัชนีและกำลัง เมื่อดัชนีต้องปรากฏด้านซ้ายของฐาน ให้ใช้ setSubSuperscriptOnTheLeft

$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 100);
$mathParagraph = $mathShape->getTextFrame()->getParagraphs()
- >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();
$scripts = (new MathematicalText("Y"))
- >setSubSuperscriptOnTheLeft("1", "n");
$mathParagraph->add(new MathBlock($scripts));
$presentation->save("subscript-superscript.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($presentation)) {
$presentation->dispose();
}
}
เพิ่มตัวคั่น
ใช้ enclose เพื่อใส่นิพจน์ภายในตัวคั่น คุณยังสามารถตั้งค่าตัวอักษรคั่นสำหรับนิพจน์ที่มีหลายองค์ประกอบได้

$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 100);
$mathParagraph = $mathShape->getTextFrame()->getParagraphs()
- >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();
$delimiter = (new MathematicalText("x"))
- >join("y")
- >join("z")
- >enclose(new Java("java.lang.Character", "<"), new Java("java.lang.Character", ">"));
$delimiter->setSeparatorCharacter(new Java("java.lang.Character", "|"));
$mathParagraph->add(new MathBlock($delimiter));
$presentation->save("delimiters.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($presentation)) {
$presentation->dispose();
}
}
ขนาดและการจัดแนวตัวคั่น
ส่งค่า true ไปยัง setGrowToMatchOperandHeight เพื่อให้วงเล็บขยายรอบออเปอแรงด์ที่สูง, หรือส่งค่า false เพื่อให้คงขนาดปกติ ใช้ setDelimiterShape พร้อมค่าหนึ่งในสองค่า MathDelimiterShape Centered จะวางวงเล็บกึ่งกลางบนแกนคณิตศาสตร์ซึ่งผ่านกลางเครื่องหมายเท่ากับ ส่วน Match จะจัดตำแหน่งให้ตรงกับเนื้อหาที่ล้อมรอบ ทั้งสองการตั้งค่าเอาไว้เฉพาะตัวคั่น นิพจน์ภายในคงเดิม
ตัวอย่างวางเศษส่วนในเลขชี้กำลังของ x ทำให้นิพจน์ยืดออกเหนือบรรทัดฐานมากกว่าต่ำ ทั้งสี่สมการใช้วงเล็บและการตั้งค่าฟอนต์เดียวกัน
use aspose\slides\MathDelimiterShape;
use aspose\slides\MathematicalText;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$opening = new Java("java.lang.Character", "(");
$closing = new Java("java.lang.Character", ")");
for ($row = 0; $row < 2; $row++) {
for ($column = 0; $column < 2; $column++) {
$fraction = (new MathematicalText("a + b"))->divide("c + d");
$power = (new MathematicalText("x"))->setSuperscript($fraction);
$delimiter = $power->enclose($opening, $closing);
$delimiter->setGrowToMatchOperandHeight($row === 0);
$delimiter->setDelimiterShape($column === 0 ? MathDelimiterShape::Centered : MathDelimiterShape::Match);
$mathShape = $slide->getShapes()->addMathShape(20 + 330 * $column, 20 + 120 * $row, 300, 100);
$portion = $mathShape->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
$portion->getPortionFormat()->setFontHeight(32);
$equation = (new MathematicalText("y = "))->join($delimiter);
$portion->getMathParagraph()->add($equation);
}
}
$presentation->save("delimiter-sizing.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
ใน PowerPoint ให้เปรียบเทียบแถวเพื่อดูผลของการขยาย ในแถวบน วงเล็บจะขยายเพื่อคลุมเลขชี้กำลังและดูเหมือนกันสำหรับทั้งสองรูปแบบ ในแถวล่าง การขยายถูกปิด วงเล็บเล็กด้านซ้าย (Centered) จะจัดตำแหน่งกับเครื่องหมายเท่ากับ, ส่วนด้านขวา (Match) จะอยู่สูงกว่ารอบนิพจน์

เพิ่มกล่องขอบ
เรียก toBorderBox บนเศษส่วนเพื่อกรอบนิพจน์ทั้งหมด ผลลัพธ์คือ MathBorderBox ที่แสดงสี่ด้านโดยค่าเริ่มต้น ใช้ setHideTop, setHideBottom, setHideLeft, และ setHideRight เพื่อลบด้านเฉพาะ
ตัวอย่างวางสำเนาเศษส่วนเดียวกันหกชุดบนสไลด์เดียว เพื่อแสดงการยกเลิก ให้ซ่อนสี่ด้านทั้งหมดและส่งค่า true ไปยัง setStrikethroughBottomLeftToTopRight เพื่อให้ได้เส้นทแยงขึ้น (↗) หรือ setStrikethroughTopLeftToBottomRight เพื่อให้ได้เส้นทแยงลง (↘) คุณยังสามารถใช้ setStrikethroughHorizontal หรือ setStrikethroughVertical พร้อมการรวมกันของด้านที่มองเห็นได้
use aspose\slides\MathBlock;
use aspose\slides\MathematicalText;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\SlideSizeScaleType;
$presentation = new Presentation();
try {
$presentation->getSlideSize()->setSize(540, 220, SlideSizeScaleType::DoNotScale);
$slide = $presentation->getSlides()->get_Item(0);
$addFraction = function ($x, $y, $hideBorder) use ($slide) {
$fraction = (new MathematicalText("x+1"))->divide("y+2");
$box = $fraction->toBorderBox();
$box->setHideTop($hideBorder);
$box->setHideBottom($hideBorder);
$box->setHideLeft($hideBorder);
$box->setHideRight($hideBorder);
$shape = $slide->getShapes()->addMathShape($x, $y, 160, 90);
$portion = $shape->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
$portion->getPortionFormat()->setFontHeight(28);
$mathBlock = new MathBlock($box);
$portion->getMathParagraph()->add($mathBlock);
return $box;
};
$addFraction(10, 10, false);
$partial = $addFraction(190, 10, false);
$partial->setHideTop(true);
$partial->setHideRight(true);
$addFraction(370, 10, true)->setStrikethroughBottomLeftToTopRight(true);
$addFraction(10, 120, true)->setStrikethroughTopLeftToBottomRight(true);
$addFraction(190, 120, true)->setStrikethroughHorizontal(true);
$addFraction(370, 120, false)->setStrikethroughVertical(true);
$presentation->save("border-box.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}

บนเศษส่วนนี้ เส้นดิ่งแนวนอนทับบาร์ของเศษส่วน เส้นตั้งตรงทแยงผ่านทั้งตัวเศษและส่วนล่างขณะกรอบโดยรอบยังคงมองเห็นได้ เส้นเหล่านี้ตกแต่งนิพจน์ทั้งหมด การขีดข่วนข้อความทั่วไปใช้กับอักขระในรันข้อความ หลังจากเปิดงานนำเสนอใน PowerPoint อีกครั้ง คุณยังคงแก้ไขตัวเศษและส่วนล่างแยกกันได้
ควบคุมการมองเห็นและการจัดวางด้วย Phantom Elements
หุ้มนิพจน์ด้วย MathPhantom เพื่อควบคุมการมองเห็นและพื้นที่ที่มันทำให้กับสมการอย่างอิสระ วิธีการของ MathPhantom ควบคุมดังต่อไปนี้
| Method | Effect |
|---|---|
| setShow | ส่งค่า false เพื่อซ่อนนิพจน์ มิติของมันยังคงมีผลต่อการจัดวาง เว้นแต่เมธอดที่เกี่ยวข้องด้านล่างได้รับค่า true |
| setZeroWidth | ปฏิบัติตรงกับความกว้างแนวนอนของนิพจน์เป็นศูนย์ ทำให้ส่วนที่อยู่ข้างเคียงเคลื่อนเข้ามาใกล้กัน |
| setZeroAsc | ปฏิบัติกับความสูงเหนือบรรทัดฐาน (ascent) ของมันเป็นศูนย์ ซึ่งสามารถลดช่องว่างด้านบนของส่วนที่อยู่ข้างเคียง |
| setZeroDesc | ปฏิบัติกับความสูงต่ำกว่าบรรทัดฐาน (descent) ของมันเป็นศูนย์ ซึ่งสามารถทำให้บาร์ของเศษส่วนหรือเนื้อหาอื่น ๆ ใต้นิพจน์อยู่ใกล้ส่วนข้างเคียงมากขึ้น |
Baseline คือเส้นอ้างอิงที่อักษรคณิตศาสตร์ทั่วไปวางบนมัน การซ่อนเนื้อหาไม่ได้ลบมิติของมันออก และการถือมิติเป็นศูนย์ไม่ได้ลบนิพจน์ออก นิพจน์พื้นฐานยังคงเข้าถึงได้ผ่าน getBase ส่งค่า true ไปยัง setShow เพื่อแสดงอีกครั้ง การตั้งค่ามิติก่อนหน้านั้นยังคงไม่มีการเปลี่ยนแปลง
ตัวอย่างด้านล่างสร้างห้าตัวแปรข้างเคียงกัน: เศษส่วนที่มองเห็นได้, เศษส่วนที่ซ่อนแต่รักษาพื้นที่, และสามเศษส่วนที่ซ่อนพร้อมมิติหนึ่งถูกตั้งเป็นศูนย์ แต่ละตัวแปรใช้นิพจน์รอบข้างเดียวกันและ Cambria Math ขนาด 24 จุด ทั้งหมดมีตำแหน่งบนเดียวกันที่ทำเครื่องหมายด้วยไกด์สีเทา การวาง Phantom ใส่ในตัวเศษของเศษส่วนภายนอกทำให้การเปลี่ยนแปลงการจัดเรียงแนวตั้งมองเห็นได้ง่ายขึ้น
use aspose\slides\FillType;
use aspose\slides\FontData;
use aspose\slides\MathBlock;
use aspose\slides\MathFraction;
use aspose\slides\MathPhantom;
use aspose\slides\MathematicalText;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
use aspose\slides\SlideSizeScaleType;
use aspose\slides\TextAnchorType;
$createText = static function (string $value) {
$text = new MathematicalText($value);
$text->getFormat()->setFontHeight(24);
$text->getFormat()->setLatinFont(new FontData("Cambria Math"));
return $text;
};
$presentation = new Presentation();
try {
$presentation->getSlideSize()->setSize(1000, 180, SlideSizeScaleType::DoNotScale);
$slide = $presentation->getSlides()->get_Item(0);
$labels = ["Visible", "Hidden: reserves space", "Hidden: ZeroWidth", "Hidden: ZeroAsc", "Hidden: ZeroDesc"];
$guide = $slide->getShapes()->addAutoShape(ShapeType::Line, 10, 60, 980, 0);
$guide->getLineFormat()->getFillFormat()->setFillType(FillType::Solid);
$guide->getLineFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->LIGHT_GRAY);
foreach ($labels as $i => $labelText) {
$left = 10 + $i * 200;
$label = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, $left, 10, 190, 30);
$label->getFillFormat()->setFillType(FillType::NoFill);
$label->getLineFormat()->getFillFormat()->setFillType(FillType::NoFill);
$label->getTextFrame()->setText($labelText);
$labelFormat = $label->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0)->getPortionFormat();
$labelFormat->setFontHeight(14);
$labelFormat->getFillFormat()->setFillType(FillType::Solid);
$labelFormat->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$fractionNumerator = $createText("a+b");
$fractionDenominator = $createText("c+d");
$fraction = new MathFraction($fractionNumerator, $fractionDenominator);
$phantom = new MathPhantom($fraction);
$phantom->setShow($i === 0);
$phantom->setZeroWidth($i === 2);
$phantom->setZeroAsc($i === 3);
$phantom->setZeroDesc($i === 4);
$leftText = $createText("x+");
$rightText = $createText("+y");
$numerator = (new MathBlock($leftText))->join($phantom)->join($rightText);
$denominator = $createText("z");
$equation = new MathFraction($numerator, $denominator);
$shape = $slide->getShapes()->addMathShape($left, 60, 190, 100);
$shape->getTextFrame()->getTextFrameFormat()->setAnchoringType(TextAnchorType::Top);
$mathPortion = $shape->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
$mathParagraph = $mathPortion->getMathParagraph();
$mathBlock = new MathBlock($equation);
$mathParagraph->add($mathBlock);
}
$presentation->save("math-phantom.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}

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

$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 120);
$mathParagraph = $mathShape->getTextFrame()->getParagraphs()
- >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();
$grouped = (new MathematicalText("x + y"))
- >group(new Java("java.lang.Character", "\u{23DF}"), MathTopBotPositions::Bottom, MathTopBotPositions::Top)
- >setLowerLimit("any text");
$mathParagraph->add(new MathBlock($grouped));
$presentation->save("grouped-terms.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($presentation)) {
$presentation->dispose();
}
}
จัดรูปแบบองค์ประกอบคณิตศาสตร์
ใช้ตัวช่วยจัดรูปแบบเฉพาะที่ทำให้สูตรชัดเจนขึ้น ตัวอย่างเช่น overbar วางเส้นบาร์เหนือองค์ประกอบคณิตศาสตร์

$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$mathShape = $slide->getShapes()->addMathShape(20, 20, 700, 100);
$mathParagraph = $mathShape->getTextFrame()->getParagraphs()
- >get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();
$overbar = (new MathematicalText("ABC"))->overbar();
$mathParagraph->add(new MathBlock($overbar));
$presentation->save("overbar.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($presentation)) {
$presentation->dispose();
}
}
จัดรูปแบบข้อความในสมการที่มีอยู่แล้ว
เพื่อจัดรูปแบบข้อความทั่วทั้งสมการที่มีอยู่ ให้เดินสำรวจต้นไม้ขององค์ประกอบในแต่ละ MathBlock MathElementBase::getChildren คืนค่าลูกแท้โดยตรง ดังนั้นจึงต้องเรียกแบบรีเคอร์ซีฟเพื่อเข้าถึงข้อความภายในเศษส่วน, สคริปต์, ราก, และโครงสร้างซ้อนอื่น ๆ ตรวจสอบองค์ประกอบปัจจุบันพร้อมกับลูกเพื่อรวมองค์ประกอบเริ่มต้นเมื่อเป็น MathematicalText
ตัวอย่างนี้สมมติว่า input.pptx มีสมการเช่น z = xᵢ / ∛y โดยมีเศษส่วนที่มีซับสคริปต์ในตัวเศษและรากในตัวส่วน การประมวลผลเฉพาะองค์ประกอบระดับบนของบล็อกจะพลาดข้อความในเศษส่วน ตัวอย่างค้นหาส่วนคณิตศาสตร์ในกรอบข้อความของสไลด์, เข้าถึงบล็อกผ่าน MathPortion::getMathParagraph, และใช้ setFontHeight เพื่อตั้งค่าขนาดฟอนต์เป็น 28 จุดผ่านเมธอด getFormat ของแต่ละข้อความ
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
function formatMathText($element, $mathematicalTextClass) {
if (java_instanceof($element, $mathematicalTextClass)) {
$element->getFormat()->setFontHeight(28);
}
foreach ($element->getChildren() as $child) {
formatMathText($child, $mathematicalTextClass);
}
}
$mathematicalTextClass = new JavaClass("com.aspose.slides.MathematicalText");
$autoShapeClass = new JavaClass("com.aspose.slides.IAutoShape");
$mathPortionClass = new JavaClass("com.aspose.slides.IMathPortion");
$presentation = new Presentation("input.pptx");
try {
for ($slideIndex = 0; $slideIndex < java_values($presentation->getSlides()->size()); $slideIndex++) {
$slide = $presentation->getSlides()->get_Item($slideIndex);
for ($shapeIndex = 0; $shapeIndex < java_values($slide->getShapes()->size()); $shapeIndex++) {
$shape = $slide->getShapes()->get_Item($shapeIndex);
if (!java_instanceof($shape, $autoShapeClass)) {
continue;
}
$textFrame = $shape->getTextFrame();
if (java_is_null($textFrame)) {
continue;
}
for ($paragraphIndex = 0; $paragraphIndex < java_values($textFrame->getParagraphs()->getCount()); $paragraphIndex++) {
$paragraph = $textFrame->getParagraphs()->get_Item($paragraphIndex);
for ($portionIndex = 0; $portionIndex < java_values($paragraph->getPortions()->getCount()); $portionIndex++) {
$portion = $paragraph->getPortions()->get_Item($portionIndex);
if (java_instanceof($portion, $mathPortionClass)) {
for ($blockIndex = 0; $blockIndex < java_values($portion->getMathParagraph()->getCount()); $blockIndex++) {
$block = $portion->getMathParagraph()->get_Item($blockIndex);
formatMathText($block, $mathematicalTextClass);
}
}
}
}
}
}
$presentation->save("formatted-equations.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
งานนำเสนอที่บันทึกไว้มีการจัดรูปแบบข้อความที่อัปเดตที่ทุกระดับของแต่ละสมการที่ประมวลผลไว้ ในขณะเดียวกันยังคงรักษาเนื้อหาข้อความและโครงสร้างคณิตศาสตร์เดิม
อ้างอิงอย่างรวดเร็ว
| งาน | API หลัก |
|---|---|
| สร้างข้อความคณิตศาสตร์ | MathematicalText |
| รวมองค์ประกอบ | join |
| สร้างเศษส่วน | divide |
| เพิ่มซูเปอร์สคริปต์หรือซับสคริปต์ | setSuperscript, setSubscript |
| เพิ่มฟังก์ชัน | function, asArgumentOfFunction |
| เพิ่มราก | radical |
| เพิ่มขีดจำกัด | setLowerLimit, setUpperLimit |
| เพิ่มสคริปต์ด้านซ้าย | setSubSuperscriptOnTheLeft |
| เพิ่มผลบวกและอินทิกรัล | nary, integral |
| เพิ่มเมทริกซ์ | MathMatrix |
| เพิ่มอาเรย์สมการ | toMathArray |
| เพิ่มตัวคั่น | enclose |
| เพิ่มแถบและเส้นขอบ | overbar, toBorderBox |
| จัดกลุ่มเทอม | group |
คำถามที่พบบ่อย
ฉันสามารถแก้ไขสมการ PowerPoint ที่มีอยู่ได้หรือไม่?
ใช่. เปิดงานนำเสนอ, ค้นหารูปร่างที่บรรจุ MathPortion, เรียก getMathParagraph, และอัปเดตบล็อกคณิตศาสตร์ในพารากราฟนั้น
สมการถูกบันทึกเป็นคณิตศาสตร์ PowerPoint ที่แก้ไขได้หรือไม่?
ใช่. เมื่อบันทึกเป็น PPTX, Aspose.Slides จะเขียนสมการเป็นเนื้อหา Office Math ที่แก้ไขได้
ฉันสามารถส่งออกสมการเป็น LaTeX ได้หรือไม่?
ใช่. รับ MathParagraph ของสมการจาก MathPortion, แล้วเรียก MathParagraph::toLatex เพื่อส่งออกโดยตรง สำหรับตัวอย่างที่ครบถ้วน ให้ดูที่ ส่งออกสมการคณิตศาสตร์จากการนำเสนอใน PHP ผ่าน Java