Create Equation using LaTeX

30 Jul 202617 minutes to read

The Java Word library allows you to create mathematical equations in a Word document using LaTeX.

Accent

Add an accent equation to a Word document using LaTeX through the appendMath API.

The following code example illustrates how to create an accent equation using LaTeX in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends an accent equation using LaTeX.
document.getLastParagraph().appendMath("\\dot{a}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

The following table demonstrates the LaTeX equivalent to professional format accent equations.

S.No Professional LaTeX
1. Accent equation \dot{a}
2. Accent equation \ddot{a}
3. Accent equation \dddot{a}
4. Accent equation \hat{a}
5. Accent equation \check{a}
6. Accent equation \acute{a}
7. Accent equation \grave{a}
8. Accent equation \breve{a}
9. Accent equation \widetilde{a}
10. Accent equation \bar{a}
11. Accent equation \bar{\bar{a}}
12. Accent equation \vec{a}
13. Accent equation \hvec{a}
14. Accent equation \widehat{AAA}

Bar

Add a bar equation to a Word document using LaTeX through the appendMath API.

The following code example illustrates how to create a bar equation using LaTeX in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends a bar equation using LaTeX.
document.getLastParagraph().appendMath("\\overline{a}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

The following table demonstrates the LaTeX equivalent to professional format bar equations.

S.No Professional LaTeX
1. Bar equation \overline{a}
2. Bar equation \underline{abc}

Box

Add a box equation to a Word document using LaTeX through the appendMath API.

The following code example illustrates how to create a box equation using LaTeX in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends a box equation using LaTeX.
document.getLastParagraph().appendMath("\\box{a}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

The following table demonstrates the LaTeX equivalent to professional format box equations.

S.No Professional LaTeX
1. Box equation \box{a}

Border Box

Add a border box equation to a Word document using LaTeX through the appendMath API.

The following code example illustrates how to create a border box equation using LaTeX in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends a border box equation using LaTeX.
document.getLastParagraph().appendMath("\\boxed{{x}^{2}+{y}^{2}={z}^{2}}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

The following table demonstrates the LaTeX equivalent to professional format border box equations.

S.No Professional LaTeX
1. Border Box equation \boxed{{x}^{2}+{y}^{2}={z}^{2}}

Delimiter

Add a delimiter equation to a Word document using LaTeX through the appendMath API.

The following code example illustrates how to create a delimiter equation using LaTeX in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends a delimiter equation using LaTeX.
document.getLastParagraph().appendMath("\\left(a\\right)");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

The following table demonstrates the LaTeX equivalent to professional format delimiter equations.

S.No Professional LaTeX
1. Delimiter equation \left(a\right)
2. Delimiter equation \left[a\right]
3. Delimiter equation \left\{a\right\}
4. Delimiter equation \left\lfloor a\right\rfloor
5. Delimiter equation \left\lceil a\right\rceil
6. Delimiter equation \left|a\right|
7. Delimiter equation \left[a\right[
8. Delimiter equation \left]a\right]
9. Delimiter equation \left]a\right[
10. Delimiter equation \left(a\middle|b\right)
11. Delimiter equation \left\{a\middle|b\right\}
12. Delimiter equation \left\langle a\middle|b\right\rangle
13. Delimiter equation \left\langle a\middle|b\middle|c\right\rangle
14. Delimiter equation \left(a\right.
15. Delimiter equation \left. a\right)
16. Delimiter equation \left[a\right.
17. Delimiter equation \left. a\right]
18. Delimiter equation \left\{a\right.
19. Delimiter equation \left. a\right\}
20. Delimiter equation \left\langle a\right.
21. Delimiter equation \left. a\right\rangle
22. Delimiter equation \left\lfloor a\right.
23. Delimiter equation \left. a\right\rfloor
24. Delimiter equation \left\lceil a\right.
25. Delimiter equation \left. a\right\rceil
26. Delimiter equation \left|a\right.
27. Delimiter equation \left. a\right|
28. Delimiter equation \binom{a}{b}

Equation Array

Add an equation array to a Word document using LaTeX through the appendMath API.

The following code example illustrates how to create an equation array using LaTeX in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends an equation array using LaTeX.
document.getLastParagraph().appendMath("\\eqarray{a@&b}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

The following table demonstrates the LaTeX equivalent to the professional format equation array.

S.No Professional LaTeX
1. Equation array \eqarray{a@&b}

Fraction

Add a fraction equation to a Word document using LaTeX through the appendMath API.

The following code example illustrates how to create a fraction equation using LaTeX in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends a fraction equation using LaTeX.
document.getLastParagraph().appendMath("{\\frac{dy}{dx}}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

The following table demonstrates the LaTeX equivalent to the professional format fraction equations.

S.No Professional LaTeX
1. Fraction equation \frac{\mathbit{dy}}{\mathbit{dx}}
2. Fraction equation \sfrac{dy}{dx}
3. Fraction equation {\frac{dy}{dx}}

Function

Add a function equation to a Word document using LaTeX through the appendMath API.

The following code example illustrates how to create a function equation using LaTeX in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends a function equation using LaTeX.
document.getLastParagraph().appendMath("\\sin{\\theta}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

The following table demonstrates the LaTeX equivalent to the professional format function equations.

S.No Professional LaTeX
1. Function equation \sin{\theta}
2. Function equation \cos{\theta}
3. Function equation \tan{\theta}
4. Function equation \csc{\theta}
5. Function equation \sec{\theta}
6. Function equation \cot{\theta}
7. Function equation \sin^{-1}{\theta}
8. Function equation \cos^{-1}{\theta}
9. Function equation \tan^{-1}{\theta}
10. Function equation \csc^{-1}{\theta}
11. Function equation \sec^{-1}{\theta}
12. Function equation \cot^{-1}{\theta}
13. Function equation \sinh{\theta}
14. Function equation \cosh{\theta}
15. Function equation \tanh{\theta}
16. Function equation \csch{\theta}
17. Function equation \sech{\theta}
18. Function equation \coth{\theta}
19. Function equation \sinh^{-1}{\theta}
20. Function equation \cosh^{-1}{\theta}
21. Function equation \tanh^{-1}{\theta}
22. Function equation \csch^{-1}{\theta}
23. Function equation \sech^{-1}{\theta}
24. Function equation \coth^{-1}{\theta}
25. Function equation \arcsin{\theta}
26. Function equation \arccos{\theta}
27. Function equation \arctan{\theta}
28. Function equation \arccsc{\theta}
29. Function equation \arcsec{\theta}
30. Function equation \arccot{\theta}

Group character

Add a group character equation to a Word document using LaTeX through the appendMath API.

The following code example illustrates how to create a group character equation using LaTeX in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends a group character equation using LaTeX.
document.getLastParagraph().appendMath("\\overbrace{a-b}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

The following table demonstrates the LaTeX equivalent to the professional format group character equations.

S.No Professional LaTeX
1. Group Character equation \overbrace{a-b}
2. Group Character equation \underbrace{a}

Limit

Add a limit equation to a Word document using LaTeX through the appendMath API.

The following code example illustrates how to create a limit equation using LaTeX in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends a limit equation using LaTeX.
document.getLastParagraph().appendMath("\\lim\\below{b}{a}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

The following table demonstrates the LaTeX equivalent to the professional format limit equations.

S.No Professional LaTeX
1. Limit equation \lim\below{b}{a}
2. Limit equation \min\below{b}{a}
3. Limit equation \max\below{b}{a}

Matrix

Add a matrix equation to a Word document using LaTeX through the appendMath API.

The following code example illustrates how to create a matrix equation using LaTeX in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends a matrix equation using LaTeX.
document.getLastParagraph().appendMath("\\begin{matrix}a&b\\\\\\end{matrix}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

The following table demonstrates the LaTeX equivalent to the professional format matrix equations.

S.No Professional LaTeX
1. Matrix equation \begin{matrix}\mathbit{a}&\mathbit{b}\\\end{matrix}

N-array

Add an N-array equation to a Word document using LaTeX through the appendMath API.

The following code example illustrates how to create an N-array equation using LaTeX in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends an N-array equation using LaTeX.
document.getLastParagraph().appendMath("\\sum{a}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

The following table demonstrates the LaTeX equivalent to the professional format N-array equations.

S.No Professional LaTeX
1. N-Array equation \sum{a}
2. N-Array equation \sum_{l}^{u}a
3. N-Array equation \sum_{l}^{u}a
4. N-Array equation \sum_{l}a
5. N-Array equation \sum_{b}a
6. N-Array equation \prod{a}
7. N-Array equation \amalg{a}
8. N-Array equation \bigcup{a}
9. N-Array equation \bigcap{a}
10. N-Array equation \bigvee{a}
11. N-Array equation \bigwedge{a}

Radical

Add a radical equation to a Word document using LaTeX through the appendMath API.

The following code example illustrates how to create a radical equation using LaTeX in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends a radical equation using LaTeX.
document.getLastParagraph().appendMath("\\sqrt{a}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

The following table demonstrates the LaTeX equivalent to the professional format radical equations.

S.No Professional LaTeX
1. Radical equation \sqrt{a}
2. Radical equation \sqrt[b]{a}

SubSuperScript

Add a SubSuperScript equation to a Word document using LaTeX through the appendMath API.

The following code example illustrates how to create a SubSuperScript equation using LaTeX in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends a SubSuperScript equation using LaTeX.
document.getLastParagraph().appendMath("{a}^{b}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

The following table demonstrates the LaTeX equivalent to the professional format SubSuperScript equations.

S.No Professional LaTeX
1. SubSuperScript equation {\mathbit{a}}^{\mathbit{b}}
2. SubSuperScript equation {\mathbit{a}}_{\mathbit{b}}

Left SubSuperScript

Add a Left SubSuperScript equation to a Word document using LaTeX through the appendMath API.

The following code example illustrates how to create a Left SubSuperScript equation using LaTeX in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends a Left SubSuperScript equation using LaTeX.
document.getLastParagraph().appendMath("{_{40}^{20}}{100}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

The following table demonstrates the LaTeX equivalent to the professional format Left SubSuperScript equations.

S.No Professional LaTeX
1. Left SubSuperScript equation {_{\mathbf{40}}^{\mathbf{20}}}{\mathbf{100}}

Right SubSuperScript

Add a Right SubSuperScript equation to a Word document using LaTeX through the appendMath API.

The following code example illustrates how to create a Right SubSuperScript equation using LaTeX in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends a Right SubSuperScript equation using LaTeX.
document.getLastParagraph().appendMath("{100}_{40}^{20}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

The following table demonstrates the LaTeX equivalent to the professional format Right SubSuperScript equations.

S.No Professional LaTeX
1. Right SubSuperScript equation {\mathbf{100}}_{\mathbf{40}}^{\mathbf{20}}

Format Equations

Apply style to characters

Apply styles to characters, such as bold and bold-italic, for equations in a Word document using LaTeX with the Java Word library. Apply the following styles using LaTeX commands.

Styles LaTeX
Bold

\mathbf

BoldItalic

\mathbit

The following code example demonstrates how to apply styles to characters within equations in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends an accent equation with bold using LaTeX.
document.getLastParagraph().appendMath("\\dot{\\mathbf{a}}");
//Appends an accent equation with bold-italic using LaTeX.
document.getLastSection().addParagraph().appendMath("\\dot{\\mathbit{a}}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

Apply scripts to the equation

Apply scripts, such as double-struck, fraktur, and more, to equations in a Word document using LaTeX with the Java Word library. Apply the following scripts using LaTeX commands.

Scripts LaTeX
Double-struck

\mathbb

Fraktur

\mathfrak

Sans Serif

\mathsf

Script

\mathscr
\mathcal

The following code examples show how to apply the scripts to equations in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends an accent equation with Double-Struck font using LaTeX.
document.getLastParagraph().appendMath("\\dot{\\mathbb{a}}");
//Appends an accent equation with Fraktur font using LaTeX.
document.getLastSection().addParagraph().appendMath("\\dot{\\mathfrak{a}}");
//Appends an accent equation with Sans Serif font using LaTeX.
document.getLastSection().addParagraph().appendMath("\\dot{\\mathsf{a}}");
//Appends an accent equation with Script using LaTeX.
document.getLastSection().addParagraph().appendMath("\\dot{\\mathcal{a}}");
//Appends an accent equation with Script using LaTeX.
document.getLastSection().addParagraph().appendMath("\\dot{\\mathscr{a}}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

Apply scripts to the equation

Preserve as normal text

By default, characters in equations in a Word document are in italics. However, you can also include normal text within an equation using LaTeX.

The following code example shows how to preserve text as normal text, without any default formatting, within an equation using LaTeX.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends an accent equation as normal text using LaTeX.
document.getLastParagraph().appendMath("\\dot{\\mathrm{a}}");
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

Apply Math Justification

Apply justification, such as Left, Right, and more, to the equation in a Word document using the Java Word library.

The following code example shows how to apply the justification to equations in a Word document.

//Creates a new Word document.
WordDocument document = new WordDocument();
//Adds one section and one paragraph to the document.
document.ensureMinimal();
//Appends a border box equation using LaTeX.
WMath math = document.getLastParagraph().appendMath("\\boxed{{x}^{2}+{y}^{2}={z}^{2}}");
//Applies math justification.
math.getMathParagraph().setJustification(MathJustification.Left);
//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

Modify equation using LaTeX

You can modify the text of an equation by replacing the string value in the LaTeX API, which retrieves the LaTeX string representation of the professional equation in the Word document.

The following code snippet shows how to modify a LaTeX equation in a Word document.

//Opens an existing Word document.
WordDocument document = new WordDocument("Template.docx", FormatType.Automatic);
//Adds one section and one paragraph to the document.
document.ensureMinimal();

//Accesses the first paragraph from the last section of the document.
WParagraph paragraph = (WParagraph)document.getLastSection().getBody().getChildEntities().get(0);
//Retrieves the first math equation in the paragraph, if it exists.
WMath math = (WMath) paragraph.getChildEntities().get(0);
if (math != null)
{
    //Gets the LaTeX representation of the math equation.
    String laTeX = math.getMathParagraph().getLaTeX();
    //Replaces occurrences of 'x' with 'k' in the LaTeX representation.
    math.getMathParagraph().setLaTeX(laTeX.replace("x", "k"));
}

//Saves the Word document.
document.save("Sample.docx", FormatType.Docx);
//Closes the document.
document.close();

Edit equation using LaTeX