Añadir ecuaciones matemáticas a presentaciones de PowerPoint en .NET
Visión general
PowerPoint almacena las ecuaciones como Office Math Markup Language (OMML). Con Aspose.Slides para .NET, puedes crear el mismo tipo de contenido matemático de forma programática: fracciones, radicales, funciones, límites, operadores N-arios, matrices, arreglos y bloques matemáticos con formato.
En PowerPoint, los usuarios normalmente añaden ecuaciones desde Insertar > Ecuación:

El resultado es texto matemático editable en la diapositiva:

Aspose.Slides genera ese texto matemático a través de tres objetos principales:
- Una forma matemática, creada con AddMathShape, es la forma que contiene la ecuación.
- MathPortion almacena el contenido matemático dentro del marco de texto de la forma.
- MathParagraph contiene uno o más objetos MathBlock.
La mayoría de los ejemplos a continuación utilizan MathematicalText y los métodos fluidos de IMathElement para mantener el código corto y legible.
Para escenarios de exportación a MathML, consulta Export Math Equations from Presentations in .NET.
Crear una ecuación
Este ejemplo crea una forma matemática y añade el teorema de Pitágoras:

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 que ya contiene un párrafo matemático. Accede al primer MathPortion, obtén su MathParagraph y añade bloques o elementos matemáticos a él.
Añadir fracciones
Utiliza Divide para crear una fracción. Puedes elegir un estilo de fracción 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);
Para una fracción apilada, usa MathFractionTypes.Bar:
var stackedFraction = new MathematicalText("x + 1").Divide("y - 1", MathFractionTypes.Bar);
Añadir radicales
Utiliza Radical para crear una raíz cuadrada, cúbica u otro tipo de raíz. El elemento actual se convierte en la base y el argumento en el 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);
Añadir funciones y límites
Utiliza AsArgumentOfFunction o Function para funciones como sin(x), log(x) o nombres de funciones personalizados. Para límites, coloca lim en 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);
Para un nombre de función personalizado, haz que el nombre de la función sea el elemento actual:
var customFunction = new MathematicalText("f").Function("x + 1");
Añadir operadores N-arios e integrales
Utiliza Nary para sumas, uniones, intersecciones y otros operadores grandes. Usa Integral para integrales. Ambos métodos permiten establecer límites inferior y superior.

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);
Los operadores N-arios son para operadores grandes con límites opcionales. Operadores simples como +, - y = suelen añadirse como MathematicalText y combinarse en la expresión.
Para una integral, usa Integral:
var integralBase = new MathematicalText("x").Join(new MathematicalText("dx").ToBox());
var integral = integralBase.Integral(MathIntegralTypes.Simple, "0", "1");
Añadir matrices
Utiliza MathMatrix para filas y columnas. Las matrices no incluyen corchetes de forma predeterminada, así que encierra la matriz cuando necesites paréntesis, corchetes o llaves.

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);
Añadir arreglos de ecuaciones
Utiliza ToMathArray cuando necesites ecuaciones alineadas o una pila vertical de expresiones.

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);
Añadir funciones trigonométricas
Utiliza AsArgumentOfFunction cuando el argumento es el elemento actual y el nombre de la función es conocido.

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);
Añadir subíndices y superíndices
Utiliza los asistentes de subíndice y superíndice para índices y potencias. Cuando los índices deben aparecer a la izquierda de la 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);
Añadir delimitadores
Utiliza Enclose para colocar una expresión dentro de delimitadores. También puedes establecer un carácter separador para expresiones delimitadas que contengan varios elementos.

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);
Añadir un cuadro con borde
Utiliza ToBorderBox cuando la ecuación misma debe estar enmarcada.

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);
Agrupar términos
Utiliza Group para colocar un carácter de agrupación encima o debajo de una expresión. Añade un límite para etiquetar los términos agrupados.

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);
Formatear elementos matemáticos
Utiliza los asistentes de formato solo donde clarifiquen la fórmula. Por ejemplo, Overbar coloca una barra sobre un elemento matemático.

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);
Referencia rápida
| Tarea | API principal |
|---|---|
| Crear texto matemático | MathematicalText |
| Combinar elementos | IMathElement.Join |
| Crear fracciones | IMathElement.Divide |
| Añadir superíndice o subíndice | SetSuperscript, SetSubscript |
| Añadir funciones | Function, AsArgumentOfFunction |
| Añadir radicales | IMathElement.Radical |
| Añadir límites | SetLowerLimit, SetUpperLimit |
| Añadir scripts del lado izquierdo | SetSubSuperscriptOnTheLeft |
| Añadir sumas e integrales | Nary, Integral |
| Añadir matrices | MathMatrix |
| Añadir arreglos de ecuaciones | ToMathArray |
| Añadir delimitadores | Enclose |
| Añadir barras y bordes | Overbar, ToBorderBox |
| Agrupar términos | Group |
Preguntas frecuentes
¿Puedo editar una ecuación existente de PowerPoint?
Sí. Abre la presentación, encuentra la forma que contiene un MathPortion, obtén su MathParagraph y actualiza los bloques matemáticos en ese párrafo.
¿Se guardan las ecuaciones como matemáticas editables de PowerPoint?
Sí. Cuando guardas en PPTX, Aspose.Slides escribe la ecuación como contenido matemático de Office editable.
¿Puedo exportar ecuaciones a LaTeX?
Sí. Obtén el IMathParagraph de su MathPortion, y llama a IMathParagraph.ToLatex para exportarla directamente. Para un ejemplo completo, consulta Export Math Equations from Presentations in .NET.