Thêm Phương Trình Toán Học vào Bài Thuyết Trình PowerPoint trong Java
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 for 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 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 đó 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 phương trình.
- MathPortion lưu nội dung toán học bên 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 linh hoạt từ IMathElement để giữ 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 Java.
Tạo Phương Trình
Ví dụ này sử dụng addMathShape để tạo một hình toán học và thêm định lý Pythagoras:

import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathBlock 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 {
presentation.dispose();
}
Note
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 toán học hoặc các phần tử toán học vào nó.Kết Hợp Các Khối Toán
Khi các phần của một phương trình được xây dựng độc lập dưới dạng các đối tượng IMathBlock , hãy sử dụng joinBlock để kết hợp chúng. Ví dụ xây dựng c² = trong khối trái và a² + b² trong khối phải. setSuperscript tạo siêu chỉ số toán học có thể chỉnh sửa cho cả ba lũy thừa.
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addMathShape(20, 20, 600, 100);
IMathPortion mathPortion = (IMathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
IMathParagraph mathParagraph = mathPortion.getMathParagraph();
MathBlock leftBlock = new MathBlock();
leftBlock.add(new MathematicalText("c").setSuperscript("2"));
leftBlock.add(new MathematicalText("="));
MathBlock rightBlock = new MathBlock();
rightBlock.add(new MathematicalText("a").setSuperscript("2"));
rightBlock.add(new MathematicalText("+"));
rightBlock.add(new MathematicalText("b").setSuperscript("2"));
IMathBlock combinedBlock = leftBlock.joinBlock(rightBlock);
mathParagraph.add(combinedBlock);
presentation.save("equation.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
joinBlock trả về leftBlock chính nó, bây giờ chứa cả hai phần theo thứ tự, và để lại rightBlock trống. Thêm khối đã kết hợp vào IMathParagraph chỉ một lần. Sau khi mở lại equation.pptx, đoạn văn chứa một khối đại diện cho c² = a² + b², với cả ba lũy thừa được giữ lại dưới dạng siêu chỉ số toán học.
Gọi add dưới dạng leftBlock.add(rightBlock) thay vào đó sẽ cố gắng lồng một khối trực tiếp bên trong khối khác và gây ra lỗi InvalidOperationException.
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.

import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathFraction fraction = new MathematicalText("1")
.divide("x", MathFractionTypes.Skewed);
mathParagraph.add(new MathBlock(fraction));
presentation.save("fraction.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Đối với phân số chồng, sử dụng MathFractionTypes.Bar:
import com.aspose.slides.*;
IMathFraction stackedFraction = new MathematicalText("x + 1").divide("y - 1", MathFractionTypes.Bar);
Thêm Căn Bậc
Sử dụng radical để tạo căn bậc hai, căn bậc ba hoặc các căn khác. Phần tử hiện tại trở thành cơ số, và đối số trở thành bậc.

import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathRadical radical = new MathematicalText("x")
.radical("n");
mathParagraph.add(new MathBlock(radical));
presentation.save("radical.pptx", SaveFormat.Pptx);
} finally {
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 √x truyền thống. Phương thức setHideDegree của IMathRadical điều khiển dạng hiển thị. Cả hai dạng đều giữ lại bậc 2 và radicand x trong phương trình 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ó được ẩn.
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IMathRadical radical = new MathematicalText("x").radical("2");
IAutoShape shape = slide.getShapes().addMathShape(20, 20, 200, 100);
IMathPortion portion = (IMathPortion) 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 tùy chỉnh. Đối với giới hạn, đặt lim trong một MathLimit hoặc dùng setLowerLimit.

import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathFunction limit = new MathematicalText("lim")
.setLowerLimit("x\u2192\u221E")
.function("x");
mathParagraph.add(new MathBlock(limit));
presentation.save("functions-and-limits.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Đối với tên hàm tùy chỉnh, làm cho tên hàm là phần tử hiện tại và gọi function với đối số của nó:
import com.aspose.slides.*;
IMathFunction 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 phép cộ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.

import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathBlock summationBase = new MathematicalText("x")
.setSuperscript("k")
.join(new MathematicalText("a").setSuperscript("n-k"));
IMathNaryOperator summation = summationBase.nary(MathNaryOperatorTypes.Summation, "k=0", "n");
mathParagraph.add(new MathBlock(summation));
presentation.save("nary-operators.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Toán tử N-ary dùng cho các toán tử lớn có giới hạn tùy chọn. Các toán tử đơn giản như +, -, và = thường được thêm dưới dạng MathematicalText và ghép vào biểu thức.
Đối với tích phân, sử dụng integral:
import com.aspose.slides.*;
IMathBlock integralBase = new MathematicalText("x").join(new MathematicalText("dx").toBox());
IMathNaryOperator 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 mặc định, vì vậy hãy bao quanh ma trận khi bạn cần dấu ngoặc tròn, vuông hoặc ngoặc nhọn.

import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
MathMatrix 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 {
presentation.dispose();
}
Chỉnh Sửa Ma Trận Đã Tồn Tại
Giả sử một bài học đại số tuyến tính bắt đầu với ma trận hệ số trong matrix.pptx, được lưu bởi ví dụ trước. Đối với bài tập tiếp theo, giữ phương trình đầu tiên, thay thế phương trình thứ hai bằng hai phương trình mới, và sửa đổi tập biến. Mở ma trận hiện có qua IMathMatrix để giữ các hệ số vẫn áp dụng.

Chỉ số hàng và cột bắt đầu từ zero và tham chiếu tới ma trận ở trạng thái tại mỗi lời gọi. Chèn một hàng trước chỉ số 1 sẽ di chuyển hàng thứ hai gốc sang chỉ số 2. Chèn sau chỉ số 2 sau đó sẽ thêm một hàng. Xóa hàng 2 sẽ loại bỏ phương trình thứ hai gốc và di chuyển hàng đã thêm lên một vị trí. Các cột cũng dịch chuyển tương tự.
import com.aspose.slides.*;
Presentation presentation = new Presentation("matrix.pptx");
try {
IAutoShape shape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IMathPortion portion = (IMathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
IMathMatrix matrix = (IMathMatrix) portion.getMathParagraph().get_Item(0).get_Item(0);
matrix.insertRowBefore(1);
matrix.insertRowAfter(2);
matrix.deleteRow(2);
matrix.insertColumnBefore(1);
matrix.insertColumnAfter(3);
matrix.deleteColumn(3);
// Các ô mới ban đầu là null. Đặt các hệ số khác không trước.
matrix.set_Item(1, 0, new MathematicalText("12"));
matrix.set_Item(1, 1, new MathematicalText("1"));
matrix.set_Item(2, 2, new MathematicalText("10"));
matrix.set_Item(2, 3, new MathematicalText("1"));
for (int row = 0; row < matrix.getRowCount(); row++) {
for (int column = 0; column < matrix.getColumnCount(); column++) {
if (matrix.get_Item(row, column) == null) {
matrix.set_Item(row, column, new MathematicalText("0"));
}
}
}
matrix.setColumnAlignment(0, MathHorizontalAlignment.Left);
matrix.setColumnsAlignment(1, 3, MathHorizontalAlignment.Right);
presentation.save("updated-matrix.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
setColumnAlignment căn chỉnh cột đầu tiên sang bên trái. setColumnsAlignment căn ba cột liên tiếp sang phải, bắt đầu từ chỉ số 1. Trong updated-matrix.pptx, ma trận có ba phương trình và bốn biến. Các hệ số hàng đầu 1 và x bây giờ xuất hiện ở cột thứ nhất và thứ ba.

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 stack dọc của các biểu thức.

import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 140);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathArray equationArray = new MathematicalText("x")
.join("y")
.toMathArray();
mathParagraph.add(new MathBlock(equationArray));
presentation.save("equation-array.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Căn Mảng Với Các Phần Tử Xung Quanh
setBaseJustification kiểm soát căn chỉnh dọc của một mảng so với các yếu tố toán học bên cạnh nó. Ví dụ dưới so sánh căn chỉnh với hàng đầu tiên, trung tâm của mảng và hàng cuối cùng.
Ví dụ tạo ba mảng giống hệt nhau, mỗi mảng có ba hàng và các phần tử lân cận A= và =B. Tất cả văn bản toán học sử dụng Cambria Math kích thước 24 điểm. Chỉ thay đổi việc căn chỉnh mảng.
import com.aspose.slides.*;
import java.awt.Color;
class MathTextFactory {
MathematicalText createText(String value) {
MathematicalText text = new MathematicalText(value);
text.getFormat().setFontHeight(24);
text.getFormat().setLatinFont(new FontData("Cambria Math"));
return text;
}
}
MathTextFactory factory = new MathTextFactory();
Presentation presentation = new Presentation();
try {
presentation.getSlideSize().setSize(780, 200, SlideSizeScaleType.DoNotScale);
ISlide slide = presentation.getSlides().get_Item(0);
int[] alignments = { MathVerticalAlignment.Top, MathVerticalAlignment.Center, MathVerticalAlignment.Bottom };
String[] alignmentNames = { "Top", "Center", "Bottom" };
for (int i = 0; i < alignments.length; i++) {
IAutoShape label = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20 + i * 260, 10, 240, 30);
label.getFillFormat().setFillType(FillType.NoFill);
label.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
label.getTextFrame().setText(alignmentNames[i]);
IPortionFormat labelFormat = label.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
labelFormat.setFontHeight(16);
labelFormat.getFillFormat().setFillType(FillType.Solid);
labelFormat.getFillFormat().getSolidFillColor().setColor(Color.BLACK);
IAutoShape mathShape = slide.getShapes().addMathShape(20 + i * 260, 60, 240, 130);
mathShape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Top);
IMathParagraph mathParagraph = ((IMathPortion) mathShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)).getMathParagraph();
mathParagraph.setJustification(MathJustification.LeftJustified);
IMathElement[] rows = { factory.createText("x+y=3"), factory.createText("x-y=1"), factory.createText("x=2") };
MathArray equationArray = new MathArray(rows[0]);
equationArray.getArguments().add(rows[1]);
equationArray.getArguments().add(rows[2]);
equationArray.setBaseJustification(alignments[i]);
MathematicalText leftText = factory.createText("A=");
MathematicalText rightText = factory.createText("=B");
IMathBlock equation = new MathBlock(leftText).join(equationArray).join(rightText);
mathParagraph.add(equation);
}
presentation.save("equation-array-alignment.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
So sánh sau cho thấy bản trình chiếu đã lưu trong PowerPoint. Lưu ý vị trí của A= và =B 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 một giá trị số cho quy tắc khoảng cách cụ thể và bị bỏ qua bởi các quy tắc mặ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. Đặt cả hai thuộc tính khi chỉ định một giá trị cụ thể; chỉ thay đổi giá trị số không chọn quy tắc khoảng cách.
Ví dụ lưu năm bản trình chiếu với cùng các phương trình, căn chỉnh và cài đặt phông chữ. Ba bản đầu sử dụng các thiết lập mặc định; hai bản cuối hiển thị các giá trị cụ thể.
import com.aspose.slides.*;
class MathTextFactory {
MathematicalText createText(String value) {
MathematicalText text = new MathematicalText(value);
text.getFormat().setFontHeight(24);
text.getFormat().setLatinFont(new FontData("Cambria Math"));
return text;
}
}
MathTextFactory factory = new MathTextFactory();
int[] rules = { MathRowSpacingRule.SingleLineGap, MathRowSpacingRule.OneAndAHalfLineGap, MathRowSpacingRule.TwoLineGap, MathRowSpacingRule.Exactly, MathRowSpacingRule.Multiple };
int[] spacingValues = { 0, 0, 0, 20, 3 };
String[] ruleNames = { "SingleLineGap", "OneAndAHalfLineGap", "TwoLineGap", "Exactly", "Multiple" };
for (int i = 0; i < rules.length; i++) {
String fileName = "equation-array-" + ruleNames[i] + ".pptx";
Presentation presentation = new Presentation();
try {
IAutoShape mathShape = presentation.getSlides().get_Item(0).getShapes().addMathShape(20, 20, 600, 300);
IParagraph paragraph = mathShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getParagraphFormat().setSpaceBefore(0);
paragraph.getParagraphFormat().setSpaceAfter(0);
IMathParagraph mathParagraph = ((IMathPortion) paragraph.getPortions().get_Item(0)).getMathParagraph();
mathParagraph.setJustification(MathJustification.LeftJustified);
IMathElement[] rows = { factory.createText("x+y=3"), factory.createText("x-y=1"), factory.createText("x=2") };
MathArray equationArray = new MathArray(rows[0]);
equationArray.getArguments().add(rows[1]);
equationArray.getArguments().add(rows[2]);
equationArray.setBaseJustification(MathVerticalAlignment.Center);
equationArray.setRowSpacingRule(rules[i]);
equationArray.setRowSpacing(spacingValues[i]);
MathematicalText leftText = factory.createText("A=");
MathematicalText rightText = factory.createText("=B");
IMathBlock equation = new MathBlock(leftText).join(equationArray).join(rightText);
mathParagraph.add(equation);
presentation.save(fileName, SaveFormat.Pptx);
} finally {
presentation.dispose();
}
}
Khoảng cách hàng trong mảng riêng biệt với khoảng cách đoạn văn. setSpaceBefore và setSpaceAfter thêm khoảng trống xung quanh đoạn văn chứa, không phải giữa các hàng mảng. Tương tự, IMathParagraph.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.

import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathFunction cosine = new MathematicalText("2x")
.asArgumentOfFunction(MathFunctionsOfOneArgument.Cos);
mathParagraph.add(new MathBlock(cosine));
presentation.save("trigonometric-function.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Thêm Chỉ Số Dưới và Chỉ Số Trên
Sử dụng các trợ giúp chỉ số dưới 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 bên trái của cơ số, dùng setSubSuperscriptOnTheLeft.

import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathLeftSubSuperscriptElement scripts = new MathematicalText("Y")
.setSubSuperscriptOnTheLeft("1", "n");
mathParagraph.add(new MathBlock(scripts));
presentation.save("subscript-superscript.pptx", SaveFormat.Pptx);
} finally {
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ử.

import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathDelimiter delimiter = new MathematicalText("x")
.join("y")
.join("z")
.enclose('<', '>');
delimiter.setSeparatorCharacter('|');
mathParagraph.add(new MathBlock(delimiter));
presentation.save("delimiters.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kích Thước và Cân Chỉnh Dấu Phân Cách
Chuyển true cho setGrowToMatchOperandHeight để cho dấu ngoặc mở rộng phù hợp với chiều cao toán hạng, hoặc chuyển false để giữ kích thước bình thường. Sử 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 chạy qua giữa dấu bằng. Match căn chúng với nội dung bên trong. Cả hai cài đặt chỉ ảnh hưởng đến dấu phân cách. Biểu thức bên trong vẫn giữ nguyên.
Ví dụ đặt một phân số trong lũy thừa của x để biểu thức mở rộng cao hơn dưới đường cơ sở hơn so với phía dưới. Bốn phương trình đều sử dụng cùng một dấu ngoặc và cài đặt phông chữ.
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
for (int row = 0; row < 2; row++) {
for (int column = 0; column < 2; column++) {
IMathFraction fraction = new MathematicalText("a + b").divide("c + d");
IMathSuperscriptElement power = new MathematicalText("x").setSuperscript(fraction);
IMathDelimiter delimiter = power.enclose('(', ')');
delimiter.setGrowToMatchOperandHeight(row == 0);
delimiter.setDelimiterShape(column == 0 ? MathDelimiterShape.Centered : MathDelimiterShape.Match);
IAutoShape mathShape = slide.getShapes().addMathShape(20 + 330 * column, 20 + 120 * row, 300, 100);
MathPortion portion = (MathPortion) mathShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
portion.getPortionFormat().setFontHeight(32);
IMathBlock 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ố để bao khung toàn bộ biểu thức. IMathBorderBox trả về hiển thị cả bốn mặt theo mặc định. Sử dụng setHideTop, setHideBottom, setHideLeft, và setHideRight để loại bỏ các mặt 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ị việc bãi bỏ, ẩn cả bốn mặt và chuyển true cho 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 với bất kỳ sự kết hợp nào của các mặt hiển thị.
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
presentation.getSlideSize().setSize(540, 220, SlideSizeScaleType.DoNotScale);
ISlide slide = presentation.getSlides().get_Item(0);
class FractionBuilder {
IMathBorderBox addFraction(float x, float y, boolean hideBorder) {
IMathFraction fraction = new MathematicalText("x+1").divide("y+2");
IMathBorderBox box = fraction.toBorderBox();
box.setHideTop(hideBorder);
box.setHideBottom(hideBorder);
box.setHideLeft(hideBorder);
box.setHideRight(hideBorder);
IAutoShape shape = slide.getShapes().addMathShape(x, y, 160, 90);
MathPortion portion = (MathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
portion.getPortionFormat().setFontHeight(28);
portion.getMathParagraph().add(new MathBlock(box));
return box;
}
}
FractionBuilder builder = new FractionBuilder();
builder.addFraction(10, 10, false);
IMathBorderBox partial = builder.addFraction(190, 10, false);
partial.setHideTop(true);
partial.setHideRight(true);
builder.addFraction(370, 10, true).setStrikethroughBottomLeftToTopRight(true);
builder.addFraction(10, 120, true).setStrikethroughTopLeftToBottomRight(true);
builder.addFraction(190, 120, true).setStrikethroughHorizontal(true);
builder.addFraction(370, 120, false).setStrikethroughVertical(true);
presentation.save("border-box.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

Trên phân số này, gạch ngang chồng lên thanh phân số. Gạch dọc cắt cả tử và mẫu trong khi khung xung quanh vẫn hiển thị. Các đường này trang trí toàn bộ biểu thức. Gạch chéo 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 chiếu trong PowerPoint, bạn vẫn có thể chỉnh sửa tử và mẫu một cách độc lập.
Kiểm Soát Khả Năng Hiển Thị và Bố Cục Với Các Phần Tử Phantom
Bao một biểu thức trong MathPhantom để kiểm soát khả năng hiển thị và không gian nó đóng góp vào một phương trình một cách độc lập. Các phương thức của IMathPhantom điều khiển các mục sau:
| Phương Thức | Hiệu Ứng |
|---|---|
| setShow | Chuyển 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 | Xem chiều rộng ngang của biểu thức như bằng zero, cho phép các phần tử lân cận di chuyển gần nhau hơn. |
| setZeroAsc | Xem độ lên (ascent), chiều cao trên đường cơ bản, như 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 | Xem độ hạ (descent), độ sâu dưới đường cơ bản, như zero. Điều này có thể đưa 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. |
Đường cơ bản là đường tham chiếu mà các ký tự toán học thông thường đặt trên. Ẩn nội dung không loại bỏ kích thước của nó, và coi một kích thước là zero không xóa bỏ biểu thức. Biểu thức nền vẫn có sẵn thông qua getBase. Chuyển true cho setShow để hiển thị lại; các cài đặt kích thước vẫn không đổi.
Ví dụ dưới tạo năm biến thể đặt cạnh nhau: một phân số hiển thị, một phân số ẩn vẫn giữ không gian, và ba phân số ẩn với một kích thước được coi là zero. Mỗi biến thể sử dụng cùng một biểu thức bao quanh và Cambria Math kích thước 24 điểm. Tất cả các hình phương trình có cùng vị trí trên cùng, đá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 giúp dễ quan sát các thay đổi bố cục dọc.
import com.aspose.slides.*;
import java.awt.Color;
class MathTextFactory {
MathematicalText createText(String value) {
MathematicalText text = new MathematicalText(value);
text.getFormat().setFontHeight(24);
text.getFormat().setLatinFont(new FontData("Cambria Math"));
return text;
}
}
MathTextFactory factory = new MathTextFactory();
Presentation presentation = new Presentation();
try {
presentation.getSlideSize().setSize(1000, 180, SlideSizeScaleType.DoNotScale);
ISlide slide = presentation.getSlides().get_Item(0);
String[] labels = { "Visible", "Hidden: reserves space", "Hidden: ZeroWidth", "Hidden: ZeroAsc", "Hidden: ZeroDesc" };
IAutoShape guide = slide.getShapes().addAutoShape(ShapeType.Line, 10, 60, 980, 0);
guide.getLineFormat().getFillFormat().setFillType(FillType.Solid);
guide.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.LIGHT_GRAY);
for (int i = 0; i < labels.length; i++) {
int left = 10 + i * 200;
IAutoShape label = slide.getShapes().addAutoShape(ShapeType.Rectangle, left, 10, 190, 30);
label.getFillFormat().setFillType(FillType.NoFill);
label.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
label.getTextFrame().setText(labels[i]);
IPortionFormat labelFormat = label.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat();
labelFormat.setFontHeight(14);
labelFormat.getFillFormat().setFillType(FillType.Solid);
labelFormat.getFillFormat().getSolidFillColor().setColor(Color.BLACK);
MathematicalText fractionNumerator = factory.createText("a+b");
MathematicalText fractionDenominator = factory.createText("c+d");
MathFraction fraction = new MathFraction(fractionNumerator, fractionDenominator);
MathPhantom phantom = new MathPhantom(fraction);
phantom.setShow(i == 0);
phantom.setZeroWidth(i == 2);
phantom.setZeroAsc(i == 3);
phantom.setZeroDesc(i == 4);
MathematicalText leftText = factory.createText("x+");
MathematicalText rightText = factory.createText("+y");
IMathBlock numerator = new MathBlock(leftText).join(phantom).join(rightText);
MathematicalText denominator = factory.createText("z");
MathFraction equation = new MathFraction(numerator, denominator);
IAutoShape shape = slide.getShapes().addMathShape(left, 60, 190, 100);
shape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Top);
IMathPortion mathPortion = (IMathPortion) shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
IMathParagraph mathParagraph = mathPortion.getMathParagraph();
mathParagraph.add(new MathBlock(equation));
}
presentation.save("math-phantom.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}

Nhóm Các Thuật Ngữ
Sử dụng group để đặt ký tự nhóm lên 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.

import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathLimit grouped = new MathematicalText("x + y")
.group('\u23DF', MathTopBotPositions.Bottom, MathTopBotPositions.Top)
.setLowerLimit("any text");
mathParagraph.add(new MathBlock(grouped));
presentation.save("grouped-terms.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Định Dạng Các Phần Tử Toán
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 phía trên một phần tử toán học.

import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
IMathParagraph mathParagraph = ((MathPortion) mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0)).getMathParagraph();
IMathBar overbar = new MathematicalText("ABC").overbar();
mathParagraph.add(new MathBlock(overbar));
presentation.save("overbar.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Định Dạng Văn Bản Trong Một Phương Trình Đã Tồn Tại
Để định dạng văn bản trong toàn bộ một phương trình đã tồn tại, duyệt cây phần tử của mỗi khối toán. IMathElement.getChildren chỉ trả về các con trực tiếp, vì vậy hãy gọi đệ quy để tới văn bản bên trong phân số, chỉ số, căn và các cấu trúc lồng nhau khác. Kiểm tra cả phần tử hiện tại và các con của nó để bao gồm phần tử bắt đầu khi nó là MathematicalText.
Ví dụ này giả sử input.pptx chứa một phương trình như z = xᵢ / ∛y, với một phân số chứa chỉ số dưới trong tử và một căn trong mẫu. Chỉ xử lý các phần tử cấp cao nhất 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 học trong khung văn bản slide, truy cập các khối qua IMathPortion.getMathParagraph, và dùng setFontHeight để đặt kích thước phông 28 điểm cho mỗi phần tử văn bản thông qua phương thức getFormat.
import com.aspose.slides.*;
class MathTextFormatter {
void formatMathText(IMathElement element) {
if (element instanceof MathematicalText) {
MathematicalText text = (MathematicalText) element;
text.getFormat().setFontHeight(28);
}
for (IMathElement child : element.getChildren()) {
formatMathText(child);
}
}
}
MathTextFormatter formatter = new MathTextFormatter();
Presentation presentation = new Presentation("input.pptx");
try {
for (ISlide slide : presentation.getSlides()) {
for (IShape shape : slide.getShapes()) {
if (!(shape instanceof IAutoShape)) {
continue;
}
IAutoShape autoShape = (IAutoShape) shape;
if (autoShape.getTextFrame() == null) {
continue;
}
for (IParagraph paragraph : autoShape.getTextFrame().getParagraphs()) {
for (IPortion portion : paragraph.getPortions()) {
if (portion instanceof IMathPortion) {
IMathPortion mathPortion = (IMathPortion) portion;
for (IMathBlock block : mathPortion.getMathParagraph()) {
formatter.formatMathText(block);
}
}
}
}
}
}
presentation.save("formatted-equations.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Bản trình chiếu đã lưu chứa định dạng văn bản đã cập nhật ở mọi cấp độ của mỗi phương trình đã xử lý, đồng thời giữ nguyê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ử | IMathElement.join |
| Tạo phân số | IMathElement.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 | IMathElement.radical |
| Thêm giới hạn | setLowerLimit, setUpperLimit |
| Thêm chỉ số 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 phân cách | enclose |
| Thêm thanh và viền | overbar, toBorderBox |
| Nhóm các thuật ngữ | group |
Câu Hỏi Thường Gặp
Tôi có thể chỉnh sửa một phương trình PowerPoint hiện có không?
Có. Mở bản trình chiếu, tìm hình chứa MathPortion, gọi getMathParagraph, và cập nhật các khối toán học trong đoạn vă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 ra LaTeX không?
Có. Lấy IMathParagraph của phương trình từ IMathPortion, và gọi IMathParagraph.toLatex để xuất trực tiếp. Đối với một ví dụ đầy đủ, xem Export Math Equations from Presentations in Java.