Java: Insert Math Equations in Word

Math equations are mathematical expressions commonly used in physics, engineering, computer science, and economics fields. When creating a professional Word document, you may sometimes need to include math equations to explain complex concepts, solve problems, or support specific arguments. In this article, you will learn how to insert math equations into Word documents in Java using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.10.3</version>
    </dependency>
</dependencies>
    

Insert Math Equations into a Word Document in Java

Spire.Doc for Java allows generating math equations from LaTeX code and MathML code using OfficeMath.fromLatexMathCode(String latexMathCode) and OfficeMath.fromMathMLCode(String mathMLCode) methods. The following are the detailed steps.

  • Create two string arrays from LaTeX code and MathML code.
  • Create a Document instance and add a section to it using Document.addSection() method.
  • Iterate through each LaTeX code in the string array.
  • Create a math equation from the LaTeX code using OfficeMath.fromLatexMathCode() method.
  • Add a paragraph to the section, then add the math equation to the paragraph using Paragraph.getItems().add() method.
  • Iterate through each MathML code in the string array.
  • Create a math equation from the MathML code using OfficeMath.fromMathMLCode() method.
  • Add a paragraph to the section, then add the math equation to the paragraph using Paragraph.getItems().add() method.
  • Save the result document using Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.omath.*;

public class AddMathEquations {
    public static void main(String[] args){

        //Create a string array from LaTeX code
        String[] latexMathCode = {
                "x^{2}+\\sqrt{x^{2}+1}=2",
                "\\cos (2\\theta) = \\cos^2 \\theta - \\sin^2 \\theta",
                "k_{n+1} = n^2 + k_n^2 - k_{n-1}",
                "\\frac {\\frac {1}{x}+ \\frac {1}{y}}{y-z}",
                "\\int_0^ \\infty \\mathrm {e}^{-x} \\, \\mathrm {d}x",
                "\\forall x \\in X, \\quad \\exists y \\leq \\epsilon",
                "\\alpha, \\beta, \\gamma, \\Gamma, \\pi, \\Pi, \\phi, \\varphi, \\mu, \\Phi",
                "A_{m,n} = \\begin{pmatrix} a_{1,1} & a_{1,2} & \\cdots & a_{1,n} \\\\ a_{2,1} & a_{2,2} & \\cdots & a_{2,n} \\\\ \\vdots  & \\vdots  & \\ddots & \\vdots  \\\\ a_{m,1} & a_{m,2} & \\cdots & a_{m,n} \\end{pmatrix}",
        };

        //Create a string array from MathML code
        String[] mathMLCode = {
                "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mi>a</mi><mo>≠</mo><mn>0</mn></math>",
                "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mi>a</mi><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mi>b</mi><mi>x</mi><mo>+</mo><mi>c</mi><mo>=</mo><mn>0</mn></math>",
                "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mi>x</mi><mo>=</mo><mrow><mfrac><mrow><mo>−</mo><mi>b</mi><mo>±</mo><msqrt><msup><mi>b</mi><mn>2</mn></msup><mo>−</mo><mn>4</mn><mi>a</mi><mi>c</mi></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></mrow></math>",
        };

        //Create a Document instance
        Document doc = new Document();

        //Add a section
        Section section = doc.addSection();

        //Add a paragraph to the section
        Paragraph textPara = section.addParagraph();
        textPara.appendText("Creating Equations from LaTeX Code");
        textPara.applyStyle(BuiltinStyle.Heading_1);
        textPara.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        //Iterate through each LaTeX code in the string array
        for (int i = 0; i < latexMathCode.length; i++)
        {
            //Create a math equation from the LaTeX code
            OfficeMath officeMath = new OfficeMath(doc);
            officeMath.fromLatexMathCode(latexMathCode[i]);
            
            //Add the math equation to the section
            Paragraph paragraph = section.addParagraph();
            paragraph.getItems().add(officeMath);
            section.addParagraph();
        }
        
        //Add a paragraph to the section
        textPara = section.addParagraph();
        textPara.appendText("Creating Equations from MathML Code");
        textPara.applyStyle(BuiltinStyle.Heading_1);
        textPara.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        //Iterate through each MathML code in the string array
        for (int j = 0; j < mathMLCode.length; j++)
        {
            //Create a math equation from the MathML code
            OfficeMath officeMath = new OfficeMath(doc);
            officeMath.fromMathMLCode(mathMLCode[j]);
            
            //Add the math equation to the section
            Paragraph paragraph = section.addParagraph();
            paragraph.getItems().add(officeMath);
            section.addParagraph();
        }

        //Save the result document
        doc.saveToFile("AddMathEquations.docx", FileFormat.Docx_2016);
        doc.dispose();
    }
}

Java: Insert Math Equations in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.