Thêm Các Phương Trình Toán Học Vào Bản Trình Chiếu PowerPoint Trên Android
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 Android 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 chèn các 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 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 fluent từ IMathElement để 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ác phương trình Toán học từ Bản trình chiếu trên Android.
Tạo một 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 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 phần tử toán học vào nó.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ố xếp 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 bậc 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();
}
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.

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→∞")
.function("x");
mathParagraph.add(new MathBlock(limit));
presentation.save("functions-and-limits.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Đối với một 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ó:
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 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 pháp đều cho phép bạn đặt giới hạn trên và dưới.

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à nối vào biểu thức.
Đối với một 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 mặc định không có ngoặc, 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.

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();
}
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 ngăn xếp dọc 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 chỉnh 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 phần tử toán học bên cạnh nó. Ví dụ dưới đây 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ụ sau 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 dùng Cambria Math kích thước 24 điểm. Chỉ có căn chỉnh mảng thay đổi.
import com.aspose.slides.*;
import android.graphics.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 dưới đây cho thấy bản trình chiếu đã 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 một quy tắc khoảng cách cụ thể và bị bỏ qua khi dùng các quy tắc định sẵn.
Đối với khoảng cách chính xác, giá trị đo bằng điểm. Đối với khoảng cách tỷ lệ, đ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; chỉ thay đổi giá trị số không chọn quy tắc khoảng cách.
Ví dụ dưới 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. Ba bản đầu dùng các cài đặt định sẵn; hai bản cuối hiển thị 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 của mảng tách 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 của 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 chỉnh 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ố 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 ở phía trái của cơ sở, 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
Gán true cho setGrowToMatchOperandHeight để cho phép ngoặc mở rộng quanh một toán hạng cao, hoặc gán false để giữ kích thước chuẩn. Sử dụng setDelimiterShape với một trong hai giá trị của MathDelimiterShape. Centered căn giữa 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 chúng với nội dung được bao quanh. Cả hai cài đặt chỉ ảnh hưởng tới 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ố ở mũ của x nên biểu thức kéo dài hơn phía trên đường cơ sở so với phía dưới. Bốn phương trình đều dùng cùng một kiểu ngoặc và cài đặt phông.
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, ngoặc mở rộng để bao quanh mũ và trông giống nhau cho cả hai hình dạng. Ở hàng dưới, tính năng mở rộng tắt. Ngoặc nhỏ hơn bên trái (Centered) căn với dấu bằng, trong khi ngoặc bên phải (Match) nằm cao hơn quanh biểu thức.

Thêm Hộp Đường Viền
Gọi toBorderBox trên một phân số để đóng khung toàn bộ biểu thức. Đối tượng IMathBorderBox trả về mặc định hiển thị bốn mặt. 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ị gạch bỏ, ẩn bốn mặt và gán true cho setStrikethroughBottomLeftToTopRight để tạo đường chéo đi lên (↗) hoặc setStrikethroughTopLeftToBottomRight cho đường chéo đi xuống (↘). Bạn cũng có thể dùng setStrikethroughHorizontal hoặc setStrikethroughVertical với bất kỳ tổ hợp mặt nào 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, đườ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 xung quanh vẫn hiển thị. Các đường này trang trí toàn bộ biểu thức. Gạch bỏ văn bản thông thường chỉ á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 độc lập.
Kiểm soát Hiển thị và Bố cục với Các Phần tử Ảo
Bao quanh một biểu thức bằng MathPhantom để kiểm soát việc hiển thị và không gian mà nó đóng góp cho một phương trình một cách độc lập. Các phương thức của IMathPhantom kiểm soát các mục sau:
| Method | Effect |
|---|---|
| setShow | Gá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 0, cho phép các phần tử lân cận di chuyển lại gần nhau hơn. |
| setZeroAsc | Xem độ lên, chiều cao trên đường cơ sở, như bằng 0. Điều này có thể giảm khoảng trống dự trữ phía trên các phần tử lân cận. |
| setZeroDesc | Xem độ hạ, độ sâu dưới đường cơ sở, như bằng 0. Điều này có thể làm cho thanh phân số hoặc nội dung khác dưới biểu thức gần hơn với các phần tử lân cận. |
Đường cơ sở là đường tham chiếu mà các ký tự toán học thường ngồi trên đó. Ẩn nội dung không loại bỏ kích thước của nó, và xem một kích thước như bằng 0 không xóa bỏ biểu thức. Biểu thức cơ bản vẫn có thể truy cập qua getBase. Gán true cho 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ể song song: 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 xử lý thành 0. Mỗi biến thể 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, đượ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 giúp dễ quan sát các thay đổi bố cục dọc.
import com.aspose.slides.*;
import android.graphics.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.LTGRAY);
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 một ký tự nhóm lên hoặc dưới một biểu thức. Thêm một giới hạn để dán nhãn cho các thành phần đã 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 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.

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 trên toàn bộ một phương trình đã có, duyệt qua cây phần tử của mỗi khối toán học. IMathElement.getChildren chỉ trả về các con trực tiếp, vì vậy cần gọi đệ quy để đến văn bản trong phân số, chỉ số, 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 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ả định rằng input.pptx chứa một phương trình 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ỏ sót vă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 của slide, truy cập các khối thông qua IMathPortion.getMathParagraph, và dùng setFontHeight để đặt kích thước phông 28 điểm qua phương thức getFormat của mỗi phần tử văn bản.
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 chiếu Nhanh
| Task | Main API |
|---|---|
| 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 script 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 một MathPortion, gọi getMathParagraph, 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 IMathParagraph của phương trình từ IMathPortion, và gọi IMathParagraph.toLatex để xuất trực tiếp. Đối với ví dụ đầy đủ, xem Xuất Các Phương trình Toán học từ Bản Trình chiếu trên Android qua Java.