When compiling the mathematics test papers or making mechanical design drawings, you may often need to add some mathematical equations to the documents. This article will share how to add Latex Math equation and MathMLCode to a Word document using a free Java API.

Import JAR Dependency

Method 1: Download the free API (Free Spire.Doc for Java) and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.
Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<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.free</artifactId>
      <version>5.2.0</version>
   </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Sample Code

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.omath.OfficeMath;

public class AddEquation {
    public static void main(String[] args) {
        //Create a Document instance
        Document doc = new Document();

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

        //Add 2 paragraphs and then add Latex Math code to them
        Paragraph paragraph1 = section.addParagraph();
        OfficeMath officeMath1 = new OfficeMath(doc);
        paragraph1.getItems().add(officeMath1);
        officeMath1.fromLatexMathCode("$f(x, y) = 100 * \\lbrace[(x + y) * 3] - 5\\rbrace$");

        Paragraph paragraph2 = section.addParagraph();
        OfficeMath officeMath2 = new OfficeMath(doc);
        paragraph2.getItems().add(officeMath2);
        officeMath2.fromLatexMathCode("$S=a_{1}^2+a_{2}^2+a_{3}^2$");

        //Add another paragraph and then add MathMLCode to it
        Paragraph paragraph3 = section.addParagraph();
        OfficeMath officeMath3 = new OfficeMath(doc);
        paragraph3.getItems().add(officeMath3);
        officeMath3.fromMathMLCode("<mml:math xmlns:mml=\"http://www.w3.org/1998/Math/MathML\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\"><mml:msup><mml:mrow><mml:mi>x</mml:mi></mml:mrow><mml:mrow><mml:mn>2</mml:mn></mml:mrow></mml:msup><mml:mo>+</mml:mo><mml:msqrt><mml:msup><mml:mrow><mml:mi>x</mml:mi></mml:mrow><mml:mrow><mml:mn>2</mml:mn></mml:mrow></mml:msup><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:msqrt><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:math>");

        //Save the document
        doc.saveToFile("addMathEquation.docx", FileFormat.Docx_2013);
        doc.dispose();
    }
}
Enter fullscreen mode Exit fullscreen mode

The output Word document

MathEquation

Learn more about the free API:
https://www.e-iceblue.com/Tutorials/Java/Spire.Doc-for-Java/Program-Guide/Spire.Doc-Program-Guide-Content-for-Java.html

Logo

ModelScope旨在打造下一代开源的模型即服务共享平台,为泛AI开发者提供灵活、易用、低成本的一站式模型服务产品,让模型应用更简单!

更多推荐