# Working with Mathematical Equation

25 Mar 202124 minutes to read

Equations in Word document are combination of mathematical symbols or text. For example, you can create a Fourier series equation in Word document.

## Types of equation

The following different structures of equation can be created by using the Essential DocIO.

• Accent
• Bar
• Box
• Border box
• Delimiter
• Equation array
• Fraction
• Function
• Group character
• Limit
• Matrix
• N-Array
• Phantom
• SubSuperscript
• Left SubSuperscript
• Right SubSuperscript

### Accent

You can add an accent mark to the equation. The following code example shows how to add an accent mark to the equation.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.ensureMinimal();
//Appends a new mathematical equation  to the paragraph
WMath math = document.getLastParagraph().appendMath();
//Sets the accent character
mathAccent.setAccentCharacter("̆");
//Adds the run element for accent
officeMathRunElement.setItem(new WTextRange(document));
WTextRange textRange =(WTextRange) officeMathRunElement.getItem();
//Sets text for accent equation
textRange.setText("a");
//Applies character formatting for text range
textRange.getCharacterFormat().setBold(true);
textRange.getCharacterFormat().setItalic(true);
//Saves the Word document
document.save("Sample.docx", FormatType.Docx);
//Closes the document
document.close();

### Bar

You can add a bar (which adds horizontal line on top or bottom) to the equation. The following code example shows how to add a bar to the equation.

//Creates a new Word document
WordDocument document = new WordDocument();
//Add a section and a paragraph in the empty document
document.ensureMinimal();
//Appends a new mathematical equation to the paragraph
WMath math = document.getLastParagraph().appendMath();
IOfficeMathBar mathBar = (IOfficeMathBar) officeMath.getFunctions().add(0, MathFunctionType.Bar);
//Sets the position of bar
mathBar.setBarTop(true);
//Adds the run element for bar
IOfficeMathRunElement officeMathRunElement = (IOfficeMathRunElement) mathBar.getEquation().getFunctions().add(0, MathFunctionType.RunElement);
officeMathRunElement.setItem(new WTextRange(document));
//Sets text for bar equation
((WTextRange)officeMathRunElement.getItem()).setText("a");
//Saves the Word document
document.save("Sample.docx", FormatType.Docx);
//Closes the document
document.close();

### Box

You can add a box to the equation. The following code example shows how to add a box to the equation.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.ensureMinimal();
//Appends a new mathematical equation to the paragraph
WMath math = document.getLastParagraph().appendMath();
IOfficeMathBox mathBox = (IOfficeMathBox) officeMath.getFunctions().add(0, MathFunctionType.Box);
//Adds the run element for box
officeMathRunElement.setItem(new WTextRange(document));
//Sets text for math
((WTextRange)officeMathRunElement.getItem()).setText("a+b");
//Enables the flag, to behave the box and its contents as a single operator
mathBox.setOperatorEmulator(true);
//Enables the flag, to act box as the mathematical differential
mathBox.setEnableDifferential(true);
//Adds a break in box equation
//Adds the run element for box
officeMathRunElement.setItem( new WTextRange(document));
//Sets text for box equation
((WTextRange)officeMathRunElement.getItem()).setText( "==");
//Adds the run element for box
officeMathRunElement.setItem( new WTextRange(document));
//Sets text for box equation
//Saves the Word document
document.save("Sample.docx", FormatType.Docx);
//Closes the document
document.close();

### Border box

You can add a box with the borders on four sides and strikethrough on horizontal, vertical, and diagonal directions to the equation. The following code example shows how to add a border box to the equation.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.ensureMinimal();
//Appends a new mathematical equation to the paragraph
WMath math = document.getLastParagraph().appendMath();
//Sets the diagonal strikethrough from lower left to upper right
mathBorderBox.setStrikeDiagonalUp(true);
//Sets the diagonal strikethrough from upper left to lower right
mathBorderBox.setStrikeDiagonalDown(true);
//Sets the horizontal strikethrough
mathBorderBox.setStrikeHorizontal(true);
//Sets the vertical strikethrough
mathBorderBox.setStrikeVertical(true);
//Enables the flag, to hide the bottom border of an equation
mathBorderBox.setHideBottom(true);
//Enables the flag, to hide the left border of an equation
mathBorderBox.setHideLeft(true);
//Sets false to show the right border of an equation
mathBorderBox.setHideRight(false);
//Sets false to show the top border of an equation
mathBorderBox.setHideTop(false);
//Adds the run element for border box
officeMathRunElement.setItem(new WTextRange(document));
//Sets text for border box equation
((WTextRange)officeMathRunElement.getItem()).setText("a+b-c");
//Saves the Word document
document.save("Sample.docx", FormatType.Docx);
//Closes the document
document.close();

### Delimiter

You can add a delimiter (parenthesis, square brackets and other characters) to the equation. The following code example shows how to a add delimiter to the equation.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.ensureMinimal();
//Appends a new mathematical equation to the paragraph
WMath math = document.getLastParagraph().appendMath();
//Sets the begin character
mathDelimiter.setBeginCharacter("[");
//Sets the end character
mathDelimiter.setEndCharacter("]");
//Enables the flag, to grow delimiter characters to full height of the arguments
mathDelimiter.setIsGrow(true);
//Sets the appearance of delimiters
mathDelimiter.setDelimiterShape(MathDelimiterShapeType.Match);
//Adds the run element for delimiter
officeMathRunElement.setItem(new WTextRange(document));
//Sets text for delimiter equation
((WTextRange)officeMathRunElement.getItem()).setText("a+b");
//Saves the Word document
document.save("Sample.docx", FormatType.Docx);
//Closes the document
document.close();

### Equation array

You can create a one dimensional array of equations in Word document. The following code example shows how to create an array of equations.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.ensureMinimal();
//Appends a new mathematical equation to the paragraph
WMath math = document.getLastParagraph().appendMath();
//Sets the vertical alignment for equation array
mathEquationArray.setVerticalAlignment(MathVerticalAlignment.Center);
//Enables the flag, to distribute the equation array equally within the container
mathEquationArray.setExpandEquationContainer(true);
//Enables the flag, to expand the equations in an equation array to the maximum width
mathEquationArray.setExpandEquationContainer(true);
//Sets the row spacing rule
mathEquationArray.setRowSpacingRule(SpacingRule.Multiple);
//Adds the run element for equation array
officeMathRunElement.setItem(new WTextRange(document));
//Sets text for equation) array
((WTextRange)officeMathRunElement.getItem()).setText("x+y+z=0");
//Adds the run element for equation array
officeMathRunElement.setItem(new WTextRange(document));
//Sets text for equation array
((WTextRange)officeMathRunElement.getItem()).setText("x+y-z=1");
//Saves the Word document
document.save("Sample.docx", FormatType.Docx);
//Closes the document
document.close();

### Fraction

You can create a fraction equation with a numerator and denominator in Word document. The following code example shows how to create a fraction equation.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.ensureMinimal();
//Appends a new mathematical equation to the paragraph
WMath math = document.getLastParagraph().appendMath();
//Sets the denominator for fraction
officeMathRunElement.setItem(new WTextRange(document));
((WTextRange)officeMathRunElement.getItem()).setText("a");
//Sets the Numerator for fraction
officeMathRunElement.setItem(new WTextRange(document));
((WTextRange)officeMathRunElement.getItem()).setText("b");
//Sets the fraction type
mathFraction.setFractionType(MathFractionType.NormalFractionBar);
//Saves the Word document
document.save("Sample.docx", FormatType.Docx);
//Closes the document
document.close();

### Function

You can create trigonometric functions in a Word document. The following code example shows how to create a function.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.ensureMinimal();
//Appends a new mathematical equation to the paragraph
WMath math = document.getLastParagraph().appendMath();
//Sets the function name
officeMathRunElement.setItem(new WTextRange(document));
((WTextRange)officeMathRunElement.getItem()).setText("sin");
//Adds the run element for function
officeMathRunElement.setItem(new WTextRange(document));
//Sets text for function
((WTextRange)officeMathRunElement.getItem()).setText("90");
//Saves the Word document
document.save("Sample.docx", FormatType.Docx);
//Closes the document
document.close();

### Group character

You can group mathematical equations by adding a grouping character at above or below to the corresponding equations. The following code example shows how to create an equation with grouping character.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.ensureMinimal();
//Appends a new mathematical equation to the paragraph
WMath math = document.getLastParagraph().appendMath();
//Sets the group character
officeMathGroupCharacter.setGroupCharacter("⏞");
//Enables the flag to align group character at top
officeMathGroupCharacter.setHasAlignTop(true);
//Enables the flag to align the text and group character
officeMathGroupCharacter.setHasCharacterTop(true);
//Adds the run element for group character
officeMathRunElement.setItem(new WTextRange(document));
//Sets text for group character equation
((WTextRange)officeMathRunElement.getItem()).setText("a-b");
//Saves the Word document
document.save("Sample.docx", FormatType.Docx);
//Closes the document
document.close();

### Limit

You can add upper limit or lower limit to the mathematical equation. The following code example shows how to create limit equation.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.ensureMinimal();
//Appends a new mathematical equation to the paragraph
WMath wMath = document.getLastParagraph().appendMath();
officeMathRunElement.setItem( new WTextRange(document));
//Sets text for limit equation.
((WTextRange)officeMathRunElement.getItem()).setText("lim");
//Sets the type of the limit.
officeMathLimit.setLimitType(MathLimitType.LowerLimit);
officeMathRunElement_limit.setItem(new WTextRange(document));
//Sets the Limit value.
((WTextRange)officeMathRunElement_limit.getItem()).setText("n=0");
officeMathLimit.setLimitType( MathLimitType.LowerLimit);
officeMathRunElement.setItem( new WTextRange(document));
//Sets text for base of the specified equation
((WTextRange)officeMathRunElement.getItem()).setText("x");
//Saves the Word document
document.save("Sample.docx");
//Closes the document
document.close();

### Matrix

You can create a matrix equation in a Word document. The following code example shows how to create a matrix equation.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.ensureMinimal();
//Appends a new mathematical equation to the paragraph
WMath wmath = document.getLastParagraph().appendMath();
//Sets vertical alignment for matrix
mathMatrix.setVerticalAlignment(MathVerticalAlignment.Center);
//Sets width for matrix columns
mathMatrix.setColumnWidth(1);
//Sets column spacing rule
mathMatrix.setColumnSpacingRule(SpacingRule.OneAndHalf);
//Sets column spacing value
mathMatrix.setColumnSpacing(3);
//Enables the flag to hide place holders
mathMatrix.setHidePlaceHolders(true);
//Sets row spacing rule.
mathMatrix.setRowSpacingRule(SpacingRule.Double);
//Sets row spacing value.
mathMatrix.setRowSpacing(2);

//Sets horizontal alignment for column
mathMatrix.getColumns().get(0).setHorizontalAlignment(MathHorizontalAlignment.Left);

//Gets an argument in first cell in first row
officeMath = mathMatrix.getRows().get(0).getArguments().get(0);
//Sets text for argument in first cell in first row
officeMathRunElement.setItem( new WTextRange(document));
((WTextRange)officeMathRunElement.getItem()).setText("1");

//Gets an argument in second cell in first row
officeMath = mathMatrix.getRows().get(0).getArguments().get(1);
//Sets text for argument in second cell in first row
officeMathRunElement.setItem(new WTextRange(document));
((WTextRange)officeMathRunElement.getItem()).setText("2");

//Gets an argument in first cell in second row
officeMath = mathMatrix.getRows().get(1).getArguments().get(0);
//Sets text for argument in first cell in second row
officeMathRunElement.setItem(new WTextRange(document));
((WTextRange)officeMathRunElement.getItem()).setText("3");

//Gets an argument in second cell in second row
officeMath = mathMatrix.getRows().get(1).getArguments().get(1);
//Sets text for argument in second cell in second row
officeMathRunElement.setItem(new WTextRange(document));
((WTextRange)officeMathRunElement.getItem()).setText("4");
//Saves the Word document.
document.save("Sample.docx");
//Closes the document
document.close();

### N-Array

You can create an equation with common large operators such as summation, integrals, union, intersection, logical OR, logical AND, products and co-products. The following code example shows how to create a summation with limits.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.ensureMinimal();
//Appends a new mathematical equation to the paragraph
WMath wMath = document.getLastParagraph().appendMath();
IOfficeMathNArray officeMathNArray = (IOfficeMathNArray) officeMath.getFunctions().add(0, MathFunctionType.NArray);
//Sets N-Array character.
officeMathNArray.setNArrayCharacter("∑");
//Enables the flag, to grow N-array character to full height of the arguments
officeMathNArray.setHasGrow(false);
//Enables the flag to hide lower limit
officeMathNArray.setHideLowerLimit(false);
//Enables the flag to hide upper limit
officeMathNArray.setHideUpperLimit(false);
//Sets false to set limit position on above the summation
officeMathNArray.setSubSuperscriptLimit(false);
officeMathRunElement.setItem(new WTextRange(document));
//Sets text for superscript property of NArray equation
((WTextRange)officeMathRunElement.getItem()).setText("n=1");
officeMathRunElement.setItem(new WTextRange(document));
((WTextRange)officeMathRunElement.getItem()).setText("10");
officeMathRunElement.setItem(new WTextRange(document));
//Sets text for NArray equation
((WTextRange)officeMathRunElement.getItem()).setText("x");
//Saves the Word document.
document.save("Sample.docx");
//Closes the document
document.close();

You can create a radical equation in Word document. The following example shows how to create a radical equation.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.ensureMinimal();
//Appends a new mathematical equation to the paragraph
WMath wmath = document.getLastParagraph().appendMath();
//Sets false to show degree in radical
officeMathRunElement.setItem( new WTextRange(document));
((WTextRange)officeMathRunElement.getItem()).setText("2");
officeMathRunElement.setItem(new WTextRange(document));
((WTextRange)officeMathRunElement.getItem()).setText("x");
//Saves the Word document
document.save("Sample.docx", FormatType.Docx);
//Closes the document
document.close();

### Phantom

You can create a phantom equation to add the spacing of the phantom
without displaying that base and suppressing part of the glyph from spacing considerations. The following code example shows how to create a phantom equation.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.ensureMinimal();
//Appends a new mathematical equation to the paragraph
WMath wmath = document.getLastParagraph().appendMath();
officeMathRunElement.setItem( new WTextRange(document));
((WTextRange)officeMathRunElement.getItem()).setText("2");
//Enables the flag, to show the contents of phantom
officeMathPhantom.setShow(true);
//Enables the flag, to transparent the phantom
officeMathPhantom.setTransparent(true);
//Enables the flag, to ignore the ascent of the phantom contents in spacing
officeMathPhantom.setZeroAscent(true);
//Enables the flag, to ignore the descent of the phantom contents in spacing
officeMathPhantom.setZeroDescent(true);
//Enables the flag, to ignore the width of a phantom contents in spacing
officeMathPhantom.setZeroWidth(true);
//Adds a run element for phantom
officeMathRunElement.setItem(new WTextRange(document));
//Sets text for phantom equation
((WTextRange)officeMathRunElement.getItem()).setText("a-b");
//Saves the Word document
document.save("Sample.docx", FormatType.Docx);
//Closes the document
document.close();

### SubSuperscript

You can add a superscript or subscript equation in a Word document. The following code shows how to create a superscript equation.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.ensureMinimal();
//Appends a new mathematical equation to the paragraph
WMath wmath = document.getLastParagraph().appendMath();
IOfficeMathScript officeMathScript = (IOfficeMathScript) officeMath.getFunctions().add(0, MathFunctionType.SubSuperscript);
//Sets the type of the script as superscript.
officeMathScript.setScriptType(MathScriptType.Superscript);
//Adds a run element for superscript.
officeMathRunElement.setItem( new WTextRange(document));
WTextRange textRange = (WTextRange) officeMathRunElement.getItem();
//Sets text for superscript.
textRange.setText("2");
//Sets text for equation
((WTextRange)officeMathRunElement.getItem()).setText("x");
//Saves the Word document
document.save("Sample.docx", FormatType.Docx);
//Closes the document
document.close();

### Left SubSuperscript

You can add superscript and subscript on the left side of mathematical equation. The following code example shows how to add superscript and subscript on the left side of the equation.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.ensureMinimal();
//Appends a new mathematical equation to the paragraph
WMath wmath = document.getLastParagraph().appendMath();
IOfficeMathLeftScript officeMathLeftSubScript = (IOfficeMathLeftScript) officeMath.getFunctions().add(0, MathFunctionType.LeftSubSuperscript);
//Adds run element for left subscript
officeMathRunElement.setItem(new WTextRange(document));
//Sets text for subscript
((WTextRange)officeMathRunElement.getItem()).setText("1");
//Adds a run element for left superscript
officeMathRunElement.setItem(new WTextRange(document));
//Sets text for left superscript.
((WTextRange)officeMathRunElement.getItem()).setText("n");
officeMathRunElement.setItem( new WTextRange(document));
//Sets text for equation
((WTextRange)officeMathRunElement.getItem()).setText("Y");
//Saves the Word document
document.save("Sample.docx", FormatType.Docx);
//Closes the document
document.close();

### Right SubSuperscript

You can add superscript and subscript on the right side of mathematical equation. The following code example shows how to add superscript and subscript on the right side of the equation.

//Creates a new Word document
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document
document.ensureMinimal();
//Appends a new mathematical equation to the paragraph
WMath wmath = document.getLastParagraph().appendMath();
IOfficeMathRightScript officeMathRightScript = (IOfficeMathRightScript) officeMath.getFunctions().add(0, MathFunctionType.RightSubSuperscript);
//Sets false to align subscript and superscript horizontally
officeMathRightScript.setIsSkipAlign(true);
//Adds run element for right subscript.
officeMathRunElement.setItem(new WTextRange(document));
//Sets text for right subscript
((WTextRange)officeMathRunElement.getItem()).setText("1");
//Adds run element for right superscript
officeMathRunElement.setItem(new WTextRange(document));
//Sets text for right superscript.
((WTextRange)officeMathRunElement.getItem()).setText("n");
officeMathRunElement.setItem(new WTextRange(document));
//Sets text for equation
((WTextRange)officeMathRunElement.getItem()).setText("Y");
//Saves the Word document
document.save("Sample.docx", FormatType.Docx);
//Closes the document
document.close();

## Modify existing equation

You can add or modify the text and formatting of existing mathematical equation in Word document. The following screenshots shows an existing mathematical equation in the input Word document.

The following code example shows how to modify an existing mathematical equation in the Word document.

//Opens an existing Word document
WordDocument document = new WordDocument("Template.docx");
//Access the paragraph from Word document
WParagraph paragraph = (WParagraph) document.getLastSection().getBody().getChildEntities().get(0);
//Access the mathematical equation from the paragraph
WMath math = (WMath) paragraph.getChildEntities().get(0);
//Access the fraction equation in radical
//Access the n-array equation in fraction
IOfficeMathNArray mathNAry = (IOfficeMathNArray) mathFraction.getNumerator().getFunctions().get(0);
//Access the math script in n-array
IOfficeMathScript mathScript = (IOfficeMathScript) mathNAry.getEquation().getFunctions().get(0);
//Access the delimiter in math script
IOfficeMathDelimiter mathDelimiter = (IOfficeMathDelimiter) mathScript.getEquation().getFunctions().get(0);
//Removes the delimiter
mathScript.getEquation().getFunctions().remove(mathDelimiter);
//Modifies the run element in math script
MathParagraphItem.setItem( new WTextRange(document));
//Sets the text value
((WTextRange)MathParagraphItem.getItem()).setText("x");
//Applies character format to the text
((WTextRange)MathParagraphItem.getItem()).getCharacterFormat().setItalic(true);
((WTextRange)MathParagraphItem.getItem()).getCharacterFormat().setFontSize(20);
//Applies math format to the text
MathParagraphItem.getMathFormat().setStyle(MathStyleType.Italic);
//Saves the word document
document.save("Sample.docx");
//close the word document
document.close();

By executing the above code example, it generates output Word document as follows.