Aggiungi equazioni matematiche alle presentazioni PowerPoint in .NET
Panoramica
PowerPoint salva le equazioni come Office Math Markup Language (OMML). Con Aspose.Slides per .NET, è possibile creare lo stesso tipo di contenuto matematico programmaticamente: frazioni, radici, funzioni, limiti, operatori N-ari, matrici, array e blocchi matematici formattati.
In PowerPoint, gli utenti normalmente aggiungono le equazioni da Insert > Equation:

Il risultato è testo matematico modificabile nella diapositiva:

Aspose.Slides genera quel testo matematico attraverso tre oggetti principali:
- Una forma matematica, creata con AddMathShape, è la forma che contiene l’equazione.
- MathPortion memorizza il contenuto matematico all’interno del frame di testo della forma.
- MathParagraph contiene uno o più oggetti MathBlock.
La maggior parte degli esempi seguenti utilizza MathematicalText e i metodi fluenti da IMathElement per mantenere il codice breve e leggibile.
Per scenari di esportazione MathML, vedere Esporta equazioni matematiche dalle presentazioni in .NET.
Crea un’equazione
Questo esempio crea una forma matematica e aggiunge il teorema di Pitagora:

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 120);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var 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);
AddMathShape crea una forma che contiene già un paragrafo matematico. Accedi al primo MathPortion, ottieni il suo MathParagraph e aggiungi blocchi o elementi matematici.
Aggiungi frazioni
Usa Divide per creare una frazione. Puoi scegliere uno stile di frazione con MathFractionTypes.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var fraction = new MathematicalText("1")
.Divide("x", MathFractionTypes.Skewed);
mathParagraph.Add(new MathBlock(fraction));
presentation.Save("fraction.pptx", SaveFormat.Pptx);
Per una frazione impilata, usa MathFractionTypes.Bar:
var stackedFraction = new MathematicalText("x + 1").Divide("y - 1", MathFractionTypes.Bar);
Aggiungi radicali
Usa Radical per creare una radice quadrata, cubica o altre radici. L’elemento corrente diventa la base e l’argomento il grado.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var radical = new MathematicalText("x")
.Radical("n");
mathParagraph.Add(new MathBlock(radical));
presentation.Save("radical.pptx", SaveFormat.Pptx);
Aggiungi funzioni e limiti
Usa AsArgumentOfFunction o Function per funzioni come sin(x), log(x) o nomi di funzioni personalizzate. Per i limiti, inserisci lim in un MathLimit o usa SetLowerLimit.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var limit = new MathematicalText("lim")
.SetLowerLimit("x→∞")
.Function("x");
mathParagraph.Add(new MathBlock(limit));
presentation.Save("functions-and-limits.pptx", SaveFormat.Pptx);
Per un nome di funzione personalizzato, rendi il nome della funzione l’elemento corrente:
var customFunction = new MathematicalText("f").Function("x + 1");
Aggiungi operatori N-ari e integrali
Usa Nary per sommatorie, unioni, intersezioni e altri operatori grandi. Usa Integral per gli integrali. Entrambi i metodi consentono di impostare i limiti inferiore e superiore.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 120);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var summationBase = new MathematicalText("x")
.SetSuperscript("k")
.Join(new MathematicalText("a").SetSuperscript("n-k"));
var summation = summationBase.Nary(MathNaryOperatorTypes.Summation, "k=0", "n");
mathParagraph.Add(new MathBlock(summation));
presentation.Save("nary-operators.pptx", SaveFormat.Pptx);
Gli operatori N-ari sono per operatori grandi con limiti opzionali. Gli operatori semplici come +, - e = sono solitamente aggiunti come MathematicalText e uniti nell’espressione.
Per un integrale, usa Integral:
var integralBase = new MathematicalText("x").Join(new MathematicalText("dx").ToBox());
var integral = integralBase.Integral(MathIntegralTypes.Simple, "0", "1");
Aggiungi matrici
Usa MathMatrix per righe e colonne. Le matrici non includono parentes per impostazione predefinita, quindi racchiudi la matrice quando ti servono parentesi tonde, quadre o graffe.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 120);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var matrix = new MathMatrix(2, 3);
matrix[0, 0] = new MathematicalText("1");
matrix[0, 1] = new MathematicalText("x");
matrix[1, 0] = new MathematicalText("x");
matrix[1, 1] = new MathematicalText("2");
matrix[1, 2] = new MathematicalText("y");
mathParagraph.Add(new MathBlock(matrix));
presentation.Save("matrix.pptx", SaveFormat.Pptx);
Aggiungi array di equazioni
Usa ToMathArray quando ti servono equazioni allineate o una pila verticale di espressioni.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 140);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var equationArray = new MathematicalText("x")
.Join("y")
.ToMathArray();
mathParagraph.Add(new MathBlock(equationArray));
presentation.Save("equation-array.pptx", SaveFormat.Pptx);
Aggiungi funzioni trigonometriche
Usa AsArgumentOfFunction quando l’argomento è l’elemento corrente e il nome della funzione è noto.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var cosine = new MathematicalText("2x")
.AsArgumentOfFunction(MathFunctionsOfOneArgument.Cos);
mathParagraph.Add(new MathBlock(cosine));
presentation.Save("trigonometric-function.pptx", SaveFormat.Pptx);
Aggiungi pedici e apici
Usa gli assistenti per pedice e apice per indici e potenze. Quando gli indici devono apparire sul lato sinistro della base, usa SetSubSuperscriptOnTheLeft.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var scripts = new MathematicalText("Y")
.SetSubSuperscriptOnTheLeft("1", "n");
mathParagraph.Add(new MathBlock(scripts));
presentation.Save("subscript-superscript.pptx", SaveFormat.Pptx);
Aggiungi delimitatori
Usa Enclose per inserire un’espressione all’interno di delimitatori. Puoi anche impostare un carattere separatore per espressioni delimitate che contengono più elementi.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var delimiter = new MathematicalText("x")
.Join("y")
.Join("z")
.Enclose('<', '>');
delimiter.SeparatorCharacter = '|';
mathParagraph.Add(new MathBlock(delimiter));
presentation.Save("delimiters.pptx", SaveFormat.Pptx);
Aggiungi un riquadro bordato
Usa ToBorderBox quando l’equazione stessa deve essere racchiusa in un riquadro.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var 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);
Raggruppa termini
Usa Group per posizionare un carattere di raggruppamento sopra o sotto un’espressione. Aggiungi un limite per etichettare i termini raggruppati.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 120);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var 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);
Formatta elementi matematici
Usa gli assistenti di formattazione solo dove chiariscono la formula. Per esempio, Overbar posiziona una barra sopra un elemento matematico.

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var mathShape = slide.Shapes.AddMathShape(20, 20, 700, 100);
var mathParagraph = ((MathPortion)mathShape.TextFrame.Paragraphs[0].Portions[0]).MathParagraph;
var overbar = new MathematicalText("ABC").Overbar();
mathParagraph.Add(new MathBlock(overbar));
presentation.Save("overbar.pptx", SaveFormat.Pptx);
Riferimento rapido
| Attività | API principale |
|---|---|
| Crea testo matematico | MathematicalText |
| Combina elementi | IMathElement.Join |
| Crea frazioni | IMathElement.Divide |
| Aggiungi apice o pedice | SetSuperscript, SetSubscript |
| Aggiungi funzioni | Function, AsArgumentOfFunction |
| Aggiungi radicali | IMathElement.Radical |
| Aggiungi limiti | SetLowerLimit, SetUpperLimit |
| Aggiungi script a sinistra | SetSubSuperscriptOnTheLeft |
| Aggiungi sommatorie e integrali | Nary, Integral |
| Aggiungi matrici | MathMatrix |
| Aggiungi array di equazioni | ToMathArray |
| Aggiungi delimitatori | Enclose |
| Aggiungi barre e bordi | Overbar, ToBorderBox |
| Raggruppa termini | Group |
FAQ
Posso modificare un’equazione PowerPoint esistente?
Sì. Apri la presentazione, individua la forma che contiene un MathPortion, ottieni il suo MathParagraph e aggiorna i blocchi matematici in quel paragrafo.
Le equazioni vengono salvate come matematica PowerPoint modificabile?
Sì. Quando salvi in PPTX, Aspose.Slides scrive l’equazione come contenuto matematico Office modificabile.
Posso esportare le equazioni in LaTeX?
Sì. Ottieni l'IMathParagraph dell’equazione dal suo MathPortion, e chiama IMathParagraph.ToLatex per esportarla direttamente. Per un esempio completo, vedere Esporta equazioni matematiche dalle presentazioni in .NET.