PowerPoint Math Equations
Overview
In PowerPoint, it is possible to write a math equation or formula and display it in the presentation. To do that, various mathematical symbols are represented in PowerPoint and can be added to the text or equation. For that, the math equations constructor is used in PowerPoint, which helps to create complex formulas like:
- Math Fraction
- Math Radical
- Math Function
- Limits and log functions
- N-ary operations
- Matrix
- Large operators
- Sin, cos functions
To add a mathematical equation in PowerPoint, the Insert -> Equation menu is used:
This will create a mathematical text in XML that can be displayed in PowerPoint as following:
PowerPoint supports plenty of mathematical symbols to create math equations. However, creating complicated math equations in PowerPoint often does not bring a good and professional-looking result. Users, who need to create mathematical presentations frequently, resort to the use of third-party solutions to create good-looking math formulas.
Using Aspose.Slide API, you can work with math equations in the PowerPoint presentations programmatically in C#. Create new math expressions or edit previously created ones. The export of mathematical structures into images is also partially supported.
How to Create a Mathematical Equation
Mathematical elements are used for building any mathematical constructions with any level of nesting. A linear collection of mathematical elements forms a mathematical block represented by the MathBlock class. MathBlock class essentially is a separated mathematical expression, formula, or equation. MathPortion is a mathematical portion, used to hold mathematical text (do not mix with Portion). MathParagraph allows manipulating a set of math blocks. The abovementioned classes are the key to work with PowerPoint math equations via Aspose.Slides API.
Let’s see how we can create the following mathematical equation via Aspose.Slides API:
To add a mathematical expression on the slide, first, add a shape that will contain the mathematical text:
var pres = new aspose.slides.Presentation();
try {
var mathShape = pres.getSlides().get_Item(0).getShapes().addMathShape(0, 0, 720, 150);
} finally {
if (pres != null) {
pres.dispose();
}
}
After creating, the shape will already contain one paragraph with a mathematical portion by default. The MathPortion class is a portion that contains a mathematical text inside. To access mathematical content inside MathPortion, refer to the MathParagraph variable:
var mathParagraph = mathShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph();
The MathParagraph class allows to read, add, edit and delete math blocks (MathBlock), that consist of a combination of mathematical elements. For example, create a fraction and place it in the presentation:
var fraction = new aspose.slides.MathematicalText("x").divide("y");
mathParagraph.add(new aspose.slides.MathBlock(fraction));
Each mathematical element is represented by some class that implements the MathElement class. This class provides a lot of methods for easily creating mathematical expressions. You can create a fairly complex mathematical expression with a single line of code. For example, the Pythagorean theorem would look like this:
var mathBlock = 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"));
Operations of the class MathElement are implemented in any type of element, including the MathBlock.
The full source code sample:
var pres = new aspose.slides.Presentation();
try {
var mathShape = pres.getSlides().get_Item(0).getShapes().addMathShape(0, 0, 720, 150);
var mathParagraph = mathShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph();
var fraction = new aspose.slides.MathematicalText("x").divide("y");
mathParagraph.add(new aspose.slides.MathBlock(fraction));
var mathBlock = 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(mathBlock);
pres.save("math.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Mathematical Element Types
Mathematical expressions are formed from sequences of mathematical elements. The sequence of mathematical elements is represented by a mathematical block, and arguments of mathematical elements form a tree-like nesting.
There are a lot of mathematical element types that can be used to construct a mathematical block. Each of these elements can be included (aggregated) in another element. That is, elements are actually containers for others, forming a tree-like structure. The simplest type of element that does not contain other elements of the mathematical text.
Each type of math element implements the MathElement class, allowing the use of the common set of math operations on different types of math elements.
MathematicalText class
The MathematicalText class represents a mathematical text - the underlying element of all mathematical constructions. Mathematical text may represent operands and operators, variables, and any other linear text.
Example: 𝑎=𝑏+𝑐
MathFraction class
MathFraction class specifies the fraction object, consisting of a numerator and denominator separated by a fraction bar. The fraction bar can be horizontal or diagonal, depending on the fraction properties. The fraction object is also used to represent the stack function, which places one element above another, with no fraction bar.
Example:
MathRadical class
MathRadical class specifies the radical function (mathematical root), consisting of a base, and an optional degree.
Example:
MathFunction class
MathFunction class specifies a function of an argument. Contains properties: getName - function name and getBase - function argument.
Example:
MathNaryOperator class
MathNaryOperator class specifies an N-ary mathematical object, such as Summation and Integral. It consists of an operator, a base (or operand), and optional upper and lower limits. Examples of N-ary operators are Summation, Union, Intersection, Integral.
This class does not include simple operators such as addition, subtraction, and so on. They are represented by a single text element - MathematicalText.
Example:
MathLimit class
MathLimit class creates the upper or lower limit. It specifies the limit object, consisting of text on the baseline and reduced-size text immediately above or below it. This element does not include the word “lim", but allows you to place text at the top or at the bottom of the expression. So, the expression
is created using a combination of MathFunction and MathLimit elements this way:
var funcName = new aspose.slides.MathLimit(new aspose.slides.MathematicalText("lim"), new aspose.slides.MathematicalText("𝑥→∞"));
var mathFunc = new aspose.slides.MathFunction(funcName, new aspose.slides.MathematicalText("𝑥"));
MathSubscriptElement, MathSuperscriptElement, MathRightSubSuperscriptElement, MathLeftSubSuperscriptElement classes
- MathSubscriptElement
- MathSuperscriptElement
- MathRightSubSuperscriptElement
- MathLeftSubSuperscriptElement
The following classes specify a lower index or an upper index. You can set subscript and superscript at the same time on the left or on the right side of an argument, but single subscript or superscript is supported on the right side only. The MathSubscriptElement can also be used to set the mathematical degree of a number.
Example:
MathMatrix class
MathMatrix class specifies the Matrix object, consisting of child elements laid out in one or more rows and columns. It is important to note that matrixes do not have built-in delimiters. To place the matrix in the brackets you should use the delimiter object - MathDelimiter. Null arguments can be used to create gaps in matrices.
Example:
MathArray class
MathArray class specifies a vertical array of equations or any mathematical objects.
Example:
Formatting Mathematical Elements
-
MathBorderBox class: draws a rectangular or some other border around the MathElement.
Example:
-
MathBox class: specifies the logical boxing (packaging) of the mathematical element. For example, a boxed object can serve as an operator emulator with or without an alignment point, serve as a line breakpoint, or be grouped such as not to allow line breaks within. For example, the “==” operator should be boxed to prevent line breaks.
-
MathDelimiter class: specifies the delimiter object, consisting of opening and closing characters (such as parentheses, braces, brackets, and vertical bars), and one or more mathematical elements inside, separated by a specified character. Examples: (𝑥2); [𝑥2|𝑦2].
Example:
-
MathAccent class: specifies the accent function, consisting of a base and a combining diacritical mark.
Example: 𝑎́.
-
MathBar class: specifies the bar function, consisting of a base argument and an overbar or underbar.
Example:
-
MathGroupingCharacter class: specifies a grouping symbol above or below an expression, usually to highlight the relationships between elements.
Example:
Mathematical Operations
Each mathematical element and mathematical expression (via MathBlock) implements the MathElement class. It allows you to use operations on the existing structure and form more complex mathematical expressions. All operations have two parameter sets: either MathElement or string as arguments. Instances of the MathematicalText class are implicitly created from specified strings when string arguments are used. Math operations available in Aspose.Slides are listed below.
Join method
Joins a mathematical element and forms a mathematical block. For example:
var element1 = new aspose.slides.MathematicalText("x");
var element2 = new aspose.slides.MathematicalText("y");
var block = element1.join(element2);
Divide method
- divide(String)
- divide(IMathElement)
- divide(String, MathFractionTypes)
- divide(IMathElement, MathFractionTypes)
Creates a fraction of the specified type with this numerator and specified denominator. For example:
var numerator = new aspose.slides.MathematicalText("x");
var fraction = numerator.divide("y", aspose.slides.MathFractionTypes.Linear);
Enclose method
Encloses the element in specified characters such as parenthesis or another character as framing.
/**
* <p>
* Enclose a math element in parenthesis
* </p>
*/
public IMathDelimiter enclose();
/**
* <p>
* Encloses this element in specified characters such as parenthesis or another characters as framing
* </p>
*/
public IMathDelimiter enclose(char beginningCharacter, char endingCharacter);
For example:
var delimiter = new aspose.slides.MathematicalText("x").enclose('[', ']');
var delimiter2 = new aspose.slides.MathematicalText("elem1").join("elem2").enclose();
Function method
Takes a function of an argument using the current object as the function name.
/**
* <p>
* Takes a function of an argument using this instance as the function name
* </p>
*/
public IMathFunction function(IMathElement functionArgument);
/**
* <p>
* Takes a function of an argument using this instance as the function name
* </p>
*/
public IMathFunction function(String functionArgument);
For example:
var func = new aspose.slides.MathematicalText("sin").function("x");
AsArgumentOfFunction method
- asArgumentOfFunction(String)
- asArgumentOfFunction(IMathElement)
- asArgumentOfFunction(MathFunctionsOfOneArgument)
- asArgumentOfFunction(MathFunctionsOfTwoArguments, IMathElement)
- asArgumentOfFunction(MathFunctionsOfTwoArguments, String)
Takes the specified function using the current instance as the argument. You can:
- specify a string as the function name, for example “cos”.
- select one of the predefined values of the enumerations MathFunctionsOfOneArgument or MathFunctionsOfTwoArguments, for example MathFunctionsOfOneArgument.ArcSin.
- select the instance of the MathElement.
For example:
var funcName = new aspose.slides.MathLimit(new aspose.slides.MathematicalText("lim"), new aspose.slides.MathematicalText("𝑛→∞"));
var func1 = new aspose.slides.MathematicalText("2x").asArgumentOfFunction(funcName);
var func2 = new aspose.slides.MathematicalText("x").asArgumentOfFunction("sin");
var func3 = new aspose.slides.MathematicalText("x").asArgumentOfFunction(aspose.slides.MathFunctionsOfOneArgument.Sin);
var func4 = new aspose.slides.MathematicalText("x").asArgumentOfFunction(aspose.slides.MathFunctionsOfTwoArguments.Log, "3");
SetSubscript, SetSuperscript, SetSubSuperscriptOnTheRight, SetSubSuperscriptOnTheLeft methods
- setSubscript(String)
- setSubscript(IMathElement)
- setSuperscript(String)
- setSuperscript(IMathElement)
- setSubSuperscriptOnTheRight(String, String)
- setSubSuperscriptOnTheRight(IMathElement, IMathElement)
- setSubSuperscriptOnTheLeft(String, String)
- setSubSuperscriptOnTheLeft(IMathElement, IMathElement)
Sets subscript and superscript. You can set subscript and superscript at the same time on the left or on the right side of the argument, but single subscript or superscript is supported only on the right side. The Superscript can also be used to set the mathematical degree of a number.
Example:
var script = new aspose.slides.MathematicalText("y").setSubSuperscriptOnTheLeft("2x", "3z");
Radical method
Specifies the mathematical root of the given degree from the specified argument.
Example:
var radical = new aspose.slides.MathematicalText("x").radical("3");
SetUpperLimit and SetLowerLimit methods
Takes the upper or lower limit. Here, the upper and bottom simply indicate the location of the argument relative to the base.
Let’s consider an expression:
Such expressions can be created through a combination of classes MathFunction and MathLimit, and operations of the MathElement as follows:
var mathExpression = new aspose.slides.MathematicalText("lim").setLowerLimit("x→∞").function("x");
Nary and Integral methods
- nary(MathNaryOperatorTypes, IMathElement, IMathElement)
- nary(MathNaryOperatorTypes, String, String)
- integral(MathIntegralTypes)
- integral(MathIntegralTypes, IMathElement, IMathElement)
- integral(MathIntegralTypes, String, String)
- integral(MathIntegralTypes, IMathElement, IMathElement, MathLimitLocations)
- integral(MathIntegralTypes, String, String, MathLimitLocations)
Both nary and integral methods create and return the N-ary operator represented by the MathNaryOperator type. In nary method, the MathNaryOperatorTypes enumeration specifies the type of operator: summation, union, etc., not including integrals. In Integral method, there is the specialized operation Integral with the enumeration of integral types MathIntegralTypes.
Example:
var baseArg = new aspose.slides.MathematicalText("x").join(new aspose.slides.MathematicalText("dx").toBox());
var integral = baseArg.integral(aspose.slides.MathIntegralTypes.Simple, "0", "1");
ToMathArray method
toMathArray puts elements in a vertical array. If this operation is called for a MathBlock instance, all child elements will be placed in the returned array.
Example:
var arrayFunction = new aspose.slides.MathematicalText("x").join("y").toMathArray();
Formatting operations: Accent, Overbar, Underbar, Group, ToBorderBox, ToBox
- accent method sets an accent mark (a character on the top of the element).
- overbar and underbar methods set a bar on the top or bottom.
- group method places in a group using a grouping character such as a bottom curly bracket or another.
- toBorderBox method places in a border-box.
- toBox method places in a non-visual box (logical grouping).
Examples:
var accent = new aspose.slides.MathematicalText("x").accent('̃');
var bar = new aspose.slides.MathematicalText("x").overbar();
var groupChr = new aspose.slides.MathematicalText("x").join("y").join("z").group('⏡', aspose.slides.MathTopBotPositions.Bottom, aspose.slides.MathTopBotPositions.Top);
var borderBox = new aspose.slides.MathematicalText("x+y+z").toBorderBox();
var boxedOperator = new aspose.slides.MathematicalText(":=").toBox();