Thêm Phương Trình Toán Học vào Bản Trình Chiếu PowerPoint trong PHP
Tổng quan
PowerPoint lưu các phương trình dưới dạng Office Math Markup Language (OMML). Với Aspose.Slides cho PHP thông 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 được định dạng.
Trong PowerPoint, người dùng thường thêm phương trình bằng cách 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 tạo văn bản toán học đó thông qua ba đối tượng chính:
- Một hình dạng toán học, được tạo bằng addMathShape, là hình dạng chứa phương trình.
- MathPortion lưu trữ nội dung toán học bên trong khung văn bản của hình dạng.
- MathParagraph chứa một hoặc nhiều đối tượng MathBlock.
Hầu hết các ví dụ dưới đây sử dụng MathematicalText và các phương thức fluent 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 Export Math Equations from Presentations in PHP via Java.
Tạo phương trình
Ví dụ này tạo một hình dạng 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();
}
}
addMathShape tạo một hình dạng đã chứa sẵn một đoạn toán học. Truy cập MathPortion đầu tiên, lấy MathParagraph của nó 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();
}
}
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 tùy 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:
$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 các 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();
}
}
Các toán tử N-ary dùng cho các toán tử lớn có tùy chọn giới hạn. Các toán tử đơn giả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. Ma trận không bao gồm ngoặc vuông theo mặc định, vì vậy hãy bao quanh ma trận khi bạn cần dấu ngoặc tròn, ngoặc vuông hoặc ngoặc nhọ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 phương trình
Sử dụng toMathArray khi bạn cần các phương trình căn chỉnh hoặc một chồng dọc các biểu thứ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();
}
}
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ỉ mục và chỉ số mũ
Sử dụng các trợ giúp chỉ mục và chỉ số mũ cho các chỉ số và lũy thừa. Khi các chỉ số phải xuất hiện bên 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 delimiters
Sử dụng enclose để đặt một biểu thức bên trong dấu delimiters. Bạn cũng có thể đặt ký tự phân tách cho các biểu thức delimiters 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();
}
}
Thêm khung viền
Sử dụng toBorderBox khi phương trình cần được bao quanh bằng khung.

$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();
$boxedEquation = (new MathematicalText("a"))
- >setSuperscript("2")
- >join("=")
- >join((new MathematicalText("b"))->setSuperscript("2"))
- >join("+")
- >join((new MathematicalText("c"))->setSuperscript("2"))
- >toBorderBox();
$mathParagraph->add(new MathBlock($boxedEquation));
$presentation->save("border-box.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($presentation)) {
$presentation->dispose();
}
}
Nhóm các mục
Sử dụng group để đặt một ký tự nhóm phía trên hoặc dưới một biểu thức. Thêm giới hạn để gắn nhãn cho các mục đã 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();
}
}
Tham chiếu 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ố mũ hoặc chỉ mục | 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 chỉ mục bên trái | setSubSuperscriptOnTheLeft |
| Thêm tổng và tích phân | nary, integral |
| Thêm ma trận | MathMatrix |
| Thêm mảng phương trình | toMathArray |
| Thêm dấu delimiters | enclose |
| Thêm thanh và khung viền | overbar, toBorderBox |
| Nhóm các mục | group |
FAQ
Tôi có thể chỉnh sửa một phương trình PowerPoint hiện có không?
Có. Mở bản thuyết trình, tìm hình dạng chứa một MathPortion, lấy MathParagraph của nó và cập nhật các khối toán học trong đoạn đó.
Các phương trình 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 phương trình dưới dạng nội dung Office math có thể chỉnh sửa.
Tôi có thể xuất các phương trình sang LaTeX không?
Có. Lấy MathParagraph của phương trình từ MathPortion, và gọi MathParagraph::toLatex để xuất trực tiếp. Đối với ví dụ đầy đủ, xem Export Math Equations from Presentations in PHP via Java.