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:

todo:image_alt_text

This will create a mathematical text in XML that can be displayed in PowerPoint as following: 

todo:image_alt_text

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:

todo:image_alt_text

To add a mathematical expression on the slide, first, add a shape that will contain the mathematical text:

  $pres = new Presentation();
  try {
    $mathShape = $pres->getSlides()->get_Item(0)->getShapes()->addMathShape(0, 0, 720, 150);
  } finally {
    if (!java_is_null($pres)) {
      $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:

  $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:

  $fraction = new MathematicalText("x")->divide("y");
  $mathParagraph->add(new MathBlock($fraction));

Each mathematical element is represented by some class that implements the IMathElement interface. This interface 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:

  $mathBlock = new MathematicalText("c")->setSuperscript("2")->join("=")->join(new MathematicalText("a")->setSuperscript("2"))->join("+")->join(new MathematicalText("b")->setSuperscript("2"));

Operations of the interface IMathElement are implemented in any type of element, including the MathBlock.

The full source code sample:

  $pres = new Presentation();
  try {
    $mathShape = $pres->getSlides()->get_Item(0)->getShapes()->addMathShape(0, 0, 720, 150);
    $mathParagraph = $mathShape->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0)->getMathParagraph();
    $fraction = new MathematicalText("x")->divide("y");
    $mathParagraph->add(new MathBlock($fraction));
    $mathBlock = new MathematicalText("c")->setSuperscript("2")->join("=")->join(new MathematicalText("a")->setSuperscript("2"))->join("+")->join(new MathematicalText("b")->setSuperscript("2"));
    $mathParagraph->add($mathBlock);
    $pres->save("math.pptx", SaveFormat::Pptx);
  } finally {
    if (!java_is_null($pres)) {
      $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 IMathElement interface, 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:

todo:image_alt_text

MathRadical class

MathRadical class specifies the radical function (mathematical root), consisting of a base, and an optional degree.

Example:

todo:image_alt_text

MathFunction class

MathFunction class specifies a function of an argument. Contains properties: getName - function name and getBase - function argument.

Example:

todo:image_alt_text

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:

todo:image_alt_text

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 

todo:image_alt_text

is created using a combination of MathFunction and MathLimit elements this way:

  $funcName = new MathLimit(new MathematicalText("lim"), new MathematicalText("𝑥→∞"));
  $mathFunc = new MathFunction($funcName, new MathematicalText("𝑥"));

MathSubscriptElement, MathSuperscriptElement, MathRightSubSuperscriptElement, MathLeftSubSuperscriptElement classes

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: 

todo:image_alt_text

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 - IMathDelimiter. Null arguments can be used to create gaps in matrices.

Example: 

todo:image_alt_text

MathArray class

MathArray class specifies a vertical array of equations or any mathematical objects.

Example: 

todo:image_alt_text

Formatting Mathematical Elements

  • MathBorderBox class: draws a rectangular or some other border around the IMathElement.

    Example: todo:image_alt_text

  • 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: todo:image_alt_text

  • 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: todo:image_alt_text

  • MathGroupingCharacter class: specifies a grouping symbol above or below an expression, usually to highlight the relationships between elements.

    Example: todo:image_alt_text

Mathematical Operations

Each mathematical element and mathematical expression (via MathBlock) implements the IMathElement interface. It allows you to use operations on the existing structure and form more complex mathematical expressions. All operations have two parameter sets: either IMathElement 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:

  $element1 = new MathematicalText("x");
  $element2 = new MathematicalText("y");
  $block = $element1->join($element2);

Divide method

Creates a fraction of the specified type with this numerator and specified denominator. For example:

  $numerator = new MathematicalText("x");
  $fraction = $numerator->divide("y", MathFractionTypes->Linear);

Enclose method

Encloses the element in specified characters such as parenthesis or another character as framing.


For example:

  $delimiter = new MathematicalText("x")->enclose('[', ']');
  $delimiter2 = new MathematicalText("elem1")->join("elem2")->enclose();

Function method

Takes a function of an argument using the current object as the function name.


For example:

  $func = new MathematicalText("sin")->function("x");

AsArgumentOfFunction method

Takes the specified function using the current instance as the argument. You can:

For example:

  $funcName = new MathLimit(new MathematicalText("lim"), new MathematicalText("𝑛→∞"));
  $func1 = new MathematicalText("2x")->asArgumentOfFunction($funcName);
  $func2 = new MathematicalText("x")->asArgumentOfFunction("sin");
  $func3 = new MathematicalText("x")->asArgumentOfFunction(MathFunctionsOfOneArgument->Sin);
  $func4 = new MathematicalText("x")->asArgumentOfFunction(MathFunctionsOfTwoArguments->Log, "3");

SetSubscript, SetSuperscript, SetSubSuperscriptOnTheRight, SetSubSuperscriptOnTheLeft methods

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:

  $script = new MathematicalText("y")->setSubSuperscriptOnTheLeft("2x", "3z");

Radical method

Specifies the mathematical root of the given degree from the specified argument.

Example:

  $radical = new 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: 

todo:image_alt_text

Such expressions can be created through a combination of classes MathFunction and MathLimit, and operations of the IMathElement as follows:

  $mathExpression = new MathematicalText("lim")->setLowerLimit("x→∞")->function("x");

Nary and Integral methods

Both nary and integral methods create and return the N-ary operator represented by the IMathNaryOperator 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:

  $baseArg = new MathematicalText("x")->join(new MathematicalText("dx")->toBox());
  $integral = $baseArg->integral(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:

  $arrayFunction = new 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:

  $accent = new MathematicalText("x")->accent('̃');
  $bar = new MathematicalText("x")->overbar();
  $groupChr = new MathematicalText("x")->join("y")->join("z")->group('⏡', MathTopBotPositions::Bottom, MathTopBotPositions::Top);
  $borderBox = new MathematicalText("x+y+z")->toBorderBox();
  $boxedOperator = new MathematicalText(":=")->toBox();