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

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

Aspose.Slides สร้างข้อความคณิตศาสตร์นั้นผ่านสามอ็อบเจ็กต์หลัก:
- รูปคณิตศาสตร์ที่สร้างด้วย addMathShape คือรูปที่บรรจุสมการ
- MathPortion เก็บเนื้อหาคณิตศาสตร์ภายในเฟรมข้อความของรูป
- MathParagraph มีหนึ่งหรือหลายอ็อบเจ็กต์ MathBlock
ตัวอย่างส่วนใหญ่ด้านล่างใช้ MathematicalText และเมธ็อดสไตล์ fluent จาก MathElementBase เพื่อให้โค้ดสั้นและอ่านง่าย
สำหรับสถานการณ์การส่งออก MathML ดูที่ Export Math Equations from Presentations in PHP via 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();
}
}
หมายเหตุ
addMathShape สร้างรูปที่มี MathParagraph อยู่แล้ว เข้าถึง MathPortion ตัวแรก, เรียก getMathParagraph แล้วเพิ่ม MathBlock หรือ MathElement ลงไปเพิ่มเศษส่วน
ใช้ 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();
}
}
แสดงหรือซ่อนดีกรีของรากที่สอง
รากที่สองสามารถแสดงเลข 2 ชัดเจนหรือใช้สัญลักษณ์ √x แบบดั้งเดิม วิธี setHideDegree ของ MathRadical ควบคุมรูปแบบที่แสดง ทั้งสองรูปแบบยังคงมีดีกรี 2 และตัว radicand เป็น x ในสมการที่แก้ไขได้

ตัวอย่างบันทึกรากที่สองสองครั้ง ใน visible-degree.pptx ดีกรี 2 ปรากฏเห็น ส่วนใน hidden-degree.pptx ดีกรีถูกซ่อน
use aspose\slides\MathBlock;
use aspose\slides\MathematicalText;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$radical = (new MathematicalText("x"))->radical("2");
$shape = $slide->getShapes()->addMathShape(20, 20, 200, 100);
$portion = $shape->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
$portion->getMathParagraph()->add(new MathBlock($radical));
$radical->setHideDegree(false);
$presentation->save("visible-degree.pptx", SaveFormat::Pptx);
$radical->setHideDegree(true);
$presentation->save("hidden-degree.pptx", SaveFormat::Pptx);
} finally {
$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 pt มีเพียงการจัดตำแหน่งอาร์เรย์ที่แตกต่างกัน
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();
}

บนเศษส่วนนี้ เส้นขีดแนวนอนทับบาร์ของเศษส่วน ส่วนเส้นขีดแนวตั้งตัด numerator และ denominator ในขณะที่กรอบรอบ ๆ ยังคงมองเห็นได้ เส้นเหล่านี้เป็นการตกแต่งทั้งนิพจน์ ข้อความปกติที่มีการขีดฆ่าใน PowerPoint จะทำกับอักขระในรันข้อความ หลังจากเปิดพรีเซนเทชันอีกครั้งใน PowerPoint คุณยังคงแก้ไข numerator และ denominator แยกกันได้
ควบคุมการมองเห็นและเค้าโครงด้วย Phantom Elements
ห่อหุ้มนิพจน์ด้วย MathPhantom เพื่อควบคุมการมองเห็นและพื้นที่ที่มันเพิ่มให้กับสมการโดยอิสระ วิธีการของ MathPhantom ควบคุมดังนี้
| เมธ็อด | ผล |
|---|---|
| setShow | ส่งค่า false เพื่อซ่อนนิพจน์ มิติกทั้งหมดยังคงมีผลต่อเค้าโครง ยกเว้นเมธ็อดที่กำหนด true ต่อไป |
| setZeroWidth | ปรับความกว้างแนวนอนของนิพจน์เป็นศูนย์ ทำให้อีเลเมนต์ข้างเคียงเคลื่อนเข้าหากัน |
| setZeroAsc | ปรับส่วนที่อยู่เหนือเส้นฐาน (ascent) เป็นศูนย์ ลดพื้นที่ที่สงวนไว้เหนืออีเลเมนต์ข้างเคียง |
| setZeroDesc | ปรับส่วนที่อยู่ใต้เส้นฐาน (descent) เป็นศูนย์ ทำให้บาร์ของเศษส่วนหรือเนื้อหาอื่น ๆ ใต้นิพจน์เข้าใกล้กับเพื่อนบ้านมากขึ้น |
เส้นฐานคือเส้นอ้างอิงที่อักขระคณิตศาสตร์ปกติวางอยู่ การซ่อนเนื้อหาไม่ลบมิติ และการตั้งค่ามิติเป็นศูนย์ไม่ทำให้นิพจน์หายไป นิพจน์พื้นฐานยังคงเข้าถึงได้ผ่าน getBase ส่งค่า true ให้ setShow เพื่อแสดงอีกครั้ง; การตั้งค่ามิติจะคงเดิม
ตัวอย่างด้านล่างสร้างห้าแบบที่วางข้างกัน: เศษส่วนที่มองเห็น, เศษส่วนที่ซ่อนแต่ยังสงวนพื้นที่, และสามเศษส่วนที่ซ่อนพร้อมการตั้งค่ามิติเป็นศูนย์หนึ่งค่าแต่ละแบบ ทุกแบบใช้สูตรรอบข้างเดียวกันและ Cambria Math ขนาด 24 pt ทั้งหมดมีตำแหน่งบนเดียวกันโดยมีไกด์สีเทา การวาง phantom ใน numerator ของเศษส่วนภายนอกทำให้การเปลี่ยนแปลงเค้าโครงแนวตั้งเห็นได้ง่ายขึ้น
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 โดยมีเศษส่วนที่มีดัชนีย่อยใน numerator และรากใน denominator การประมวลผลเฉพาะอีเลเมนต์ระดับบนของบล็อกจะพลาดข้อความในเศษส่วน ตัวอย่างค้นหา MathPortion ในเฟรมข้อความของสไลด์, เข้าถึงบล็อกผ่าน MathPortion::getMathParagraph แล้วใช้ setFontHeight เพื่อกำหนดขนาดฟอนท์เป็น 28 pt ผ่านเมธ็อด 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 แล้วอัปเดต MathBlock ในย่อหนนนั้น
สมการจะถูกบันทึกเป็นคณิตศาสตร์ PowerPoint ที่แก้ไขได้หรือไม่?
ใช่ เมื่อบันทึกเป็น PPTX, Aspose.Slides จะเขียนสมการเป็นเนื้อหา Office Math ที่แก้ไขได้
ฉันสามารถส่งออกสมการเป็น LaTeX ได้หรือไม่?
ได้ เรียก MathParagraph จาก MathPortion ของมัน แล้วใช้ MathParagraph::toLatex เพื่อส่งออกโดยตรง ดูตัวอย่างเต็มที่ Export Math Equations from Presentations in PHP via Java