Thêm Công Thức Toán Học vào Bản Trình Bày PowerPoint trong PHP
Tổng quan
PowerPoint lưu các công thức dưới dạng Office Math Markup Language (OMML). Với Aspose.Slides cho PHP qua Java, bạn có thể tạo cùng loại nội dung toán học một cách lập trình: phân số, căn bậc, hàm, giới hạn, toán tử N-ary, ma trận, mảng và các khối toán học đã định dạng.
Trong PowerPoint, người dùng thường thêm công thức từ Insert > Equation:

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

Aspose.Slides xây dựng văn bản toán học này thông qua ba đối tượng chính:
- Một hình toán học, được tạo bằng addMathShape, là hình chứa công thức.
- MathPortion lưu trữ nội dung toán học trong khung văn bản của hình.
- MathParagraph chứa một hoặc nhiều đối tượng MathBlock.
Hầu hết các ví dụ bên dưới sử dụng MathematicalText và các phương thức fluently từ MathElementBase để giữ cho mã ngắn gọn và dễ đọc.
Đối với các kịch bản xuất MathML, xem Xuất công thức toán học từ bản trình bày trong PHP qua Java.
Tạo một Công thức
Ví dụ này sử dụng addMathShape để tạo một hình toán học và thêm định lý Pythagoras:

$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();
}
}
Lưu ý
addMathShape tạo một hình đã chứa sẵn một đoạn toán học. Truy cập MathPortion đầu tiên, gọi getMathParagraph, và thêm các khối hoặc phần tử toán học vào đó.Thêm Phân Số
Sử dụng divide để tạo một phân số. Bạn có thể chọn kiểu phân số bằng MathFractionTypes.

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

$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();
}
}
Hiển Thị hoặc Ẩn bậc của Căn Bậc Hai
Căn bậc hai có thể hiển thị số 2 rõ ràng hoặc sử dụng ký hiệu truyền thống √x. Phương thức setHideDegree của MathRadical kiểm soát dạng nào xuất hiện. Cả hai dạng đều giữ bậc 2 và radicand x trong công thức có thể chỉnh sửa.

Ví dụ lưu cùng một căn bậc hai hai lần. Trong visible-degree.pptx, số 2 hiển thị. Trong hidden-degree.pptx, nó bị ẩn.
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();
}
Thêm Hàm và Giới Hạn
Sử dụng asArgumentOfFunction hoặc function cho các hàm như sin(x), log(x) hoặc tên hàm tuỳ chỉnh. Đối với giới hạn, đặt lim trong một MathLimit hoặc sử dụng 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();
}
}
Đối với tên hàm tùy chỉnh, đặt tên hàm làm phần tử hiện tại và gọi function với đối số của nó:
$customFunction = (new MathematicalText("f"))->function("x + 1");
Thêm Toán Tử N-ary và Tích Phân
Sử dụng nary cho các tổng, hợp, giao và các toán tử lớn khác. Sử dụng integral cho tích phân. Cả hai phương thức cho phép bạn đặt giới hạn dưới và trên.

$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();
}
}
Toán tử N-ary dùng cho các toán tử lớn có thể có hoặc không có giới hạn. Các toán tử đơn như +, -, và = thường được thêm dưới dạng MathematicalText và nối vào biểu thức.
Đối với tích phân, sử dụng integral:
$integralBase = (new MathematicalText("x"))->join((new MathematicalText("dx"))->toBox());
$integral = $integralBase->integral(MathIntegralTypes::Simple, "0", "1");
Thêm Ma Trận
Sử dụng MathMatrix cho các hàng và cột. Mặc định ma trận không bao gồm dấu ngoặc, vì vậy khi cần dấu ngoặc tròn, vuông hoặc nhọn, hãy bao quanh ma trận.

$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();
}
}
Thêm Mảng Công Thức
Sử dụng toMathArray khi bạn cần các công thức căn chỉnh hoặc một dải biểu thức dọc.

$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();
}
}
Căn chỉnh Mảng với Các Phần tử Xung quanh
setBaseJustification kiểm soát căn dọc của một mảng so với các phần tử toán học bên cạnh. Ví dụ dưới so sánh căn chỉnh với hàng đầu tiên, trung tâm mảng và hàng cuối cùng.
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();
}
So sánh dưới đây cho thấy bản trình bày đã lưu trong PowerPoint. Lưu ý vị trí của A= và =B bên cạnh mỗi mảng.

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

$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();
}
}
Thêm Chỉ số và Chỉ số trên
Sử dụng các trợ giúp chỉ số và chỉ số trên cho các chỉ mục và lũy thừa. Khi các chỉ mục phải xuất hiện phía trái của cơ sở, sử dụng 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();
}
}
Thêm Dấu Phân Cách
Sử dụng enclose để đặt một biểu thức bên trong dấu phân cách. Bạn cũng có thể đặt ký tự phân tách cho các biểu thức dấu phân cách chứa nhiều phần tử.

$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();
}
}
Kích Thước và Căn Dấu Phân Cách
Truyền true vào setGrowToMatchOperandHeight để cho dấu ngoặc mở rộng quanh một toán hạng cao, hoặc truyền false để giữ nguyên kích thước. Dùng setDelimiterShape với một trong hai giá trị của MathDelimiterShape. Centered căn giữa dấu ngoặc trên trục toán học, trục này chạy qua giữa dấu bằng. Match căn dấu ngoặc với nội dung bao quanh. Cả hai thiết lập chỉ ảnh hưởng đến dấu phân cách; biểu thức bên trong không thay đổi.
Ví dụ đặt một phân số làm lũy thừa của x để biểu thức mở rộng hơn phía trên chuẩn so với phía dưới. Bốn công thức đều dùng cùng dấu ngoặc và cài đặt phông chữ.
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();
}
Trong PowerPoint, so sánh các hàng để thấy hiệu ứng mở rộng. Ở hàng trên, dấu ngoặc mở rộng bao quanh lũy thừa và trông giống nhau cho cả hai dạng. Ở hàng dưới, tính năng mở rộng tắt. Dấu ngoặc nhỏ hơn bên trái (Centered) căn với dấu bằng, trong khi dấu ngoặc bên phải (Match) nằm cao hơn quanh biểu thức.

Thêm Khung Viền
Gọi toBorderBox trên một phân số để đóng khung toàn bộ biểu thức. MathBorderBox trả về mặc định hiển thị bốn phía. Sử dụng setHideTop, setHideBottom, setHideLeft và setHideRight để loại bỏ các phía riêng lẻ.
Ví dụ đặt sáu bản sao của cùng một phân số trên một slide. Để hiển thị gạch bỏ, ẩn bốn phía và truyền true vào setStrikethroughBottomLeftToTopRight cho đường chéo lên (↗) hoặc setStrikethroughTopLeftToBottomRight cho đường chéo xuống (↘). Bạn cũng có thể dùng setStrikethroughHorizontal hoặc setStrikethroughVertical kết hợp bất kỳ các phía hiển thị nào.
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();
}

Ở phân số này, đường gạch ngang chồng lên thanh phân số. Đường gạch dọc cắt cả tử và mẫu trong khi viền bao quanh vẫn hiển thị. Những đường này trang trí toàn bộ biểu thức. Gạch bỏ thông thường áp dụng cho các ký tự trong một đoạn văn bản. Sau khi mở lại bản trình bày trong PowerPoint, bạn vẫn có thể chỉnh sửa tử và mẫu độc lập.
Kiểm Soát Khả Kiểm và Bố cục với Phần tử Phantom
Bao bọc một biểu thức trong MathPhantom để kiểm soát khả năng hiển thị và không gian mà nó đóng góp vào công thức một cách độc lập. Các phương thức của MathPhantom kiểm soát các điều sau:
| Phương thức | Mô tả |
|---|---|
| setShow | Đặt false để ẩn biểu thức. Kích thước của nó vẫn đóng góp vào bố cục trừ khi các phương thức tương ứng bên dưới nhận true. |
| setZeroWidth | Xét chiều rộng ngang của biểu thức là zero, cho phép các phần tử lân cận di chuyển lại gần nhau hơn. |
| setZeroAsc | Xét phần lên của nó, chiều cao trên baseline, là zero. Điều này có thể giảm không gian dành cho phía trên các phần tử lân cận. |
| setZeroDesc | Xét phần xuống của nó, độ sâu dưới baseline, là zero. Điều này có thể di chuyển thanh phân số hoặc nội dung khác dưới biểu thức gần hơn tới các phần tử lân cận. |
Baseline là đường tham chiếu mà các ký tự toán học thường dựa vào. Ẩn nội dung không loại bỏ kích thước của nó, và xem một kích thước là zero không xóa biểu thức. Biểu thức gốc vẫn có thể truy cập qua getBase. Đặt true vào setShow để hiển thị lại; các cài đặt kích thước vẫn giữ nguyên.
Ví dụ dưới tạo năm biến thể bên cạnh nhau: một phân số hiển thị, một phân số ẩn nhưng vẫn giữ không gian, và ba phân số ẩn với một kích thước được xem là zero. Mỗi biến thể dùng cùng một biểu thức bao quanh và Cambria Math 24pt. Tất cả các hình công thức có cùng vị trí trên, được đánh dấu bằng hướng dẫn màu xám. Đặt phantom trong tử của một phân số bên ngoài làm cho thay đổi bố cục dọc dễ nhìn hơn.
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()->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();
}

Nhóm Các Thuật Ngữ
Sử dụng group để đặt một ký tự nhóm ở trên hoặc dưới một biểu thức. Thêm một giới hạn để gắn nhãn cho các thuật ngữ đã nhóm.

$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();
}
}
Định dạng Các Phần tử Toán học
Sử dụng các trợ giúp định dạng chỉ khi chúng làm rõ công thức. Ví dụ, overbar đặt một thanh trên một phần tử toán học.

$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();
}
}
Định dạng Văn bản trong Công Thức hiện có
Để định dạng văn bản trong toàn bộ công thức hiện có, duyệt cây phần tử của mỗi khối toán. MathElementBase::getChildren chỉ trả về các con trực tiếp, vì vậy gọi đệ quy để tới văn bản trong phân số, script, căn bậc và các cấu trúc lồng nhau khác. Kiểm tra phần tử hiện tại cũng như các con của nó để bao gồm phần tử khởi đầu khi nó là MathematicalText.
Ví dụ này giả định rằng input.pptx chứa một công thức như z = xᵢ / ∛y, với một phân số có chỉ số dưới ở tử và một căn bậc ở mẫu. Xử lý chỉ các phần tử cấp cao của khối sẽ bỏ lỡ văn bản bên trong phân số. Ví dụ tìm các phần toán trong khung văn bản slide, truy cập các khối qua MathPortion::getMathParagraph, và dùng setFontHeight để đặt kích thước phông chữ 28pt cho mỗi phần tử văn bản qua phương thức 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();
}
Bản trình bày đã lưu chứa định dạng văn bản đã cập nhật ở mọi cấp của mỗi công thức đã xử lý, đồng thời bảo toàn nội dung văn bản và cấu trúc toán học.
Tham khảo nhanh
| Nhiệm vụ | API chính |
|---|---|
| Tạo văn bản toán học | MathematicalText |
| Kết hợp các phần tử | join |
| Tạo phân số | divide |
| Thêm chỉ số trên hoặc chỉ số dưới | setSuperscript, setSubscript |
| Thêm hàm | function, asArgumentOfFunction |
| Thêm căn bậc | radical |
| Thêm giới hạn | setLowerLimit, setUpperLimit |
| Thêm script phía trái | setSubSuperscriptOnTheLeft |
| Thêm tổng và tích phân | nary, integral |
| Thêm ma trận | MathMatrix |
| Thêm mảng công thức | toMathArray |
| Thêm dấu phân cách | enclose |
| Thêm thanh và viền | overbar, toBorderBox |
| Nhóm các thuật ngữ | group |
FAQ
Tôi có thể chỉnh sửa một công thức PowerPoint hiện có không?
Có. Mở bản trình bày, tìm hình chứa một MathPortion, gọi getMathParagraph, và cập nhật các khối toán học trong đoạn đó.
Các công thức có được lưu dưới dạng toán học PowerPoint có thể chỉnh sửa không?
Có. Khi lưu dưới dạng PPTX, Aspose.Slides ghi công thức dưới dạng nội dung Office math có thể chỉnh sửa.
Tôi có thể xuất các công thức sang LaTeX không?
Có. Lấy MathParagraph của MathPortion, và gọi MathParagraph::toLatex để xuất trực tiếp. Đối với ví dụ đầy đủ, xem Xuất công thức toán học từ bản trình bày trong PHP qua Java.