Dodaj równania matematyczne do prezentacji PowerPoint w JavaScript
Przegląd
PowerPoint przechowuje równania jako Office Math Markup Language (OMML). Za pomocą Aspose.Slides for Node.js via Java możesz programowo tworzyć taki sam rodzaj treści matematycznej: ułamki, pierwiastki, funkcje, granice, operatory N-arny, macierze, tablice i sformatowane bloki matematyczne.
W programie PowerPoint użytkownicy zazwyczaj dodają równania z Wstaw > Równanie:

Wynikiem jest edytowalny tekst matematyczny na slajdzie:

Aspose.Slides tworzy ten tekst matematyczny za pomocą trzech głównych obiektów:
- Kształt matematyczny, tworzony za pomocą addMathShape, jest kształtem zawierającym równanie.
- MathPortion przechowuje treść matematyczną wewnątrz ramki tekstowej kształtu.
- MathParagraph zawiera jeden lub więcej obiektów MathBlock.
Większość przykładów poniżej używa MathematicalText oraz metod płynnych z MathElementBase, aby kod był krótki i czytelny.
W scenariuszach eksportu MathML zobacz Export Math Equations from Presentations in Node.js via Java.
Utwórz równanie
Ten przykład tworzy kształt matematyczny i dodaje twierdzenie Pitagorasa:

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let equation = new aspose.slides.MathematicalText("c")
.setSuperscript("2")
.join("=")
.join(new aspose.slides.MathematicalText("a").setSuperscript("2"))
.join("+")
.join(new aspose.slides.MathematicalText("b").setSuperscript("2"));
mathParagraph.add(equation);
presentation.save("pytago theorem.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
addMathShape tworzy kształt, który już zawiera paragraf matematyczny. Uzyskaj dostęp do pierwszego MathPortion, pobierz jego MathParagraph i dodaj bloki matematyczne lub elementy matematyczne.
Dodaj ułamki
Użyj divide aby utworzyć ułamek. Możesz wybrać styl ułamka za pomocą MathFractionTypes.

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let fraction = new aspose.slides.MathematicalText("1")
.divide("x", aspose.slides.MathFractionTypes.Skewed);
mathParagraph.add(new aspose.slides.MathBlock(fraction));
presentation.save("fraction.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Aby uzyskać ułamek w postaci stosu, użyj MathFractionTypes.Bar:
let stackedFraction = new aspose.slides.MathematicalText("x + 1").divide("y - 1", aspose.slides.MathFractionTypes.Bar);
Dodaj pierwiastki
Użyj radical aby utworzyć pierwiastek kwadratowy, sześcienny lub inny. Aktualny element staje się podstawą, a argument określa stopień.

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let radical = new aspose.slides.MathematicalText("x")
.radical("n");
mathParagraph.add(new aspose.slides.MathBlock(radical));
presentation.save("radical.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Dodaj funkcje i granice
Użyj asArgumentOfFunction lub function aby dodać funkcje takie jak sin(x), log(x) lub niestandardowe nazwy funkcji. Dla granic umieść lim w obiekcie MathLimit lub użyj setLowerLimit.

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let limit = new aspose.slides.MathematicalText("lim")
.setLowerLimit("x\u2192\u221E")
.function("x");
mathParagraph.add(new aspose.slides.MathBlock(limit));
presentation.save("functions-and-limits.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Aby użyć niestandardowej nazwy funkcji, ustaw nazwę funkcji jako bieżący element:
let customFunction = new aspose.slides.MathematicalText("f").function("x + 1");
Dodaj operatory N‑arny i całki
Użyj nary aby dodać sumy, sumowania, przecięcia i inne duże operatory. Użyj integral aby dodać całki. Obie metody pozwalają ustawić dolne i górne granice.

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let summationBase = new aspose.slides.MathematicalText("x")
.setSuperscript("k")
.join(new aspose.slides.MathematicalText("a").setSuperscript("n-k"));
let summation = summationBase.nary(aspose.slides.MathNaryOperatorTypes.Summation, "k=0", "n");
mathParagraph.add(new aspose.slides.MathBlock(summation));
presentation.save("nary-operators.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Operatory N‑arny służą do dużych operatorów z opcjonalnymi granicami. Proste operatory takie jak +, -, i = są zazwyczaj dodawane jako MathematicalText i łączone w wyrażenie.
Aby dodać całkę, użyj integral:
let integralBase = new aspose.slides.MathematicalText("x").join(new aspose.slides.MathematicalText("dx").toBox());
let integral = integralBase.integral(aspose.slides.MathIntegralTypes.Simple, "0", "1");
Dodaj macierze
Użyj MathMatrix aby definiować wiersze i kolumny. Macierze domyślnie nie zawierają nawiasów, więc otocz macierz, gdy potrzebujesz nawiasów okrągłych, kwadratowych lub klamrowych.

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let matrix = new aspose.slides.MathMatrix(2, 3);
matrix.set_Item(0, 0, new aspose.slides.MathematicalText("1"));
matrix.set_Item(0, 1, new aspose.slides.MathematicalText("x"));
matrix.set_Item(1, 0, new aspose.slides.MathematicalText("x"));
matrix.set_Item(1, 1, new aspose.slides.MathematicalText("2"));
matrix.set_Item(1, 2, new aspose.slides.MathematicalText("y"));
mathParagraph.add(new aspose.slides.MathBlock(matrix));
presentation.save("matrix.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Dodaj tablice równań
Użyj toMathArray gdy potrzebujesz wyrównanych równań lub pionowego stosu wyrażeń.

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 140);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let equationArray = new aspose.slides.MathematicalText("x")
.join("y")
.toMathArray();
mathParagraph.add(new aspose.slides.MathBlock(equationArray));
presentation.save("equation-array.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Dodaj funkcje trygonometryczne
Użyj asArgumentOfFunction gdy argument jest bieżącym elementem, a nazwa funkcji jest znana.

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let cosine = new aspose.slides.MathematicalText("2x")
.asArgumentOfFunction(aspose.slides.MathFunctionsOfOneArgument.Cos);
mathParagraph.add(new aspose.slides.MathBlock(cosine));
presentation.save("trigonometric-function.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Dodaj indeksy dolne i górne
Użyj pomocników indeksów dolnych i górnych dla indeksów i potęg. Gdy indeksy mają pojawiać się po lewej stronie podstawy, użyj setSubSuperscriptOnTheLeft.

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let scripts = new aspose.slides.MathematicalText("Y")
.setSubSuperscriptOnTheLeft("1", "n");
mathParagraph.add(new aspose.slides.MathBlock(scripts));
presentation.save("subscript-superscript.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Dodaj delimitery
Użyj enclose aby umieścić wyrażenie w delimitatorach. Możesz także ustawić znak separatora dla wyrażeń z wieloma elementami.

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let delimiter = new aspose.slides.MathematicalText("x")
.join("y")
.join("z")
.enclose(java.newChar('<'), java.newChar('>'));
delimiter.setSeparatorCharacter(java.newChar('|'));
mathParagraph.add(new aspose.slides.MathBlock(delimiter));
presentation.save("delimiters.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Dodaj ramkę otoczenia
Użyj toBorderBox gdy równanie ma być otoczone ramką.

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let boxedEquation = new aspose.slides.MathematicalText("a")
.setSuperscript("2")
.join("=")
.join(new aspose.slides.MathematicalText("b").setSuperscript("2"))
.join("+")
.join(new aspose.slides.MathematicalText("c").setSuperscript("2"))
.toBorderBox();
mathParagraph.add(new aspose.slides.MathBlock(boxedEquation));
presentation.save("border-box.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Grupuj wyrażenia
Użyj group aby umieścić znak grupujący nad lub pod wyrażeniem. Dodaj limit, aby oznaczyć grupowane wyrażenia.

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 120);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let grouped = new aspose.slides.MathematicalText("x + y")
.group(java.newChar('\u23DF'), aspose.slides.MathTopBotPositions.Bottom, aspose.slides.MathTopBotPositions.Top)
.setLowerLimit("any text");
mathParagraph.add(new aspose.slides.MathBlock(grouped));
presentation.save("grouped-terms.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Formatuj elementy matematyczne
Używaj pomocników formatowania tylko tam, gdzie wyjaśniają formułę. Na przykład, overbar umieszcza pasek nad elementem matematycznym.

let presentation = new aspose.slides.Presentation();
try {
let slide = presentation.getSlides().get_Item(0);
let mathShape = slide.getShapes().addMathShape(20, 20, 700, 100);
let mathParagraph = mathShape.getTextFrame().getParagraphs()
.get_Item(0).getPortions().get_Item(0).getMathParagraph();
let overbar = new aspose.slides.MathematicalText("ABC").overbar();
mathParagraph.add(new aspose.slides.MathBlock(overbar));
presentation.save("overbar.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Szybkie odniesienie
| Zadanie | Główne API |
|---|---|
| Utwórz tekst matematyczny | MathematicalText |
| Połącz elementy | join |
| Utwórz ułamki | divide |
| Dodaj indeks górny lub dolny | setSuperscript, setSubscript |
| Dodaj funkcje | function, asArgumentOfFunction |
| Dodaj pierwiastki | radical |
| Dodaj granice | setLowerLimit, setUpperLimit |
| Dodaj skrypty po lewej stronie | setSubSuperscriptOnTheLeft |
| Dodaj sumy i całki | nary, integral |
| Dodaj macierze | MathMatrix |
| Dodaj tablice równań | toMathArray |
| Dodaj delimitery | enclose |
| Dodaj kreski i ramki | overbar, toBorderBox |
| Grupuj wyrażenia | group |
FAQ
Czy mogę edytować istniejące równanie w PowerPoint?
Tak. Otwórz prezentację, znajdź kształt zawierający MathPortion, pobierz jego MathParagraph i zaktualizuj bloki matematyczne w tym paragrafie.
Czy równania są zapisywane jako edytowalna matematyka PowerPoint?
Tak. Gdy zapisujesz do PPTX, Aspose.Slides zapisuje równanie jako edytowalną treść Office Math.
Czy mogę wyeksportować równania do LaTeX?
Tak. Pobierz MathParagraph równania z jego MathPortion, a następnie wywołaj MathParagraph.toLatex aby wyeksportować go bezpośrednio. Pełny przykład znajdziesz w Export Math Equations from Presentations in Node.js via Java.