Create Equation using LaTeX
19 Sep 202424 minutes to read
The .NET Word (DocIO) library allows to create mathematical equation in Word document using LaTeX.
Accent
Add accent equation to a Word document using the LaTeX through AppendMath API.
The following code example illustrates how to create accent equation using LaTeX in Word document.
NOTE
Refer to the appropriate tabs in the code snippets section: C# [Cross-platform] for ASP.NET Core, Blazor, Xamarin, UWP, .NET MAUI, and WinUI; C# [Windows-specific] for WinForms and WPF; VB.NET [Windows-specific] for VB.NET applications.
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an accent equation using LaTeX.
document.LastParagraph.AppendMath(@"\dot{a}");
//Save the Word document to MemoryStream
using MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an accent equation using LaTeX.
document.LastParagraph.AppendMath(@"\dot{a}");
//Save the Word document.
document.Save("Result.docx", FormatType.Docx);
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an accent equation using LaTeX.
document.LastParagraph.AppendMath(@"\dot{a}")
'Save the Word document.
document.Save("Result.docx", FormatType.Docx)
You can download a complete working sample from GitHub.
The following table demonstrates the LaTeX equivalent to professional format accent equations.
S.No | Professional | LaTeX |
---|---|---|
1. | \dot{a} | |
2. | \ddot{a} | |
3. | \dddot{a} | |
4. | \hat{a} | |
5. | \check{a} | |
6. | \acute{a} | |
7. | \grave{a} | |
8. | \breve{a} | |
9. | \widetilde{a} | |
10. | \bar{a} | |
11. | \bar{\bar{a}} | |
12. | \vec{a} | |
13. | \hvec{a} | |
14. | \widehat{AAA} |
Bar
Add bar equation to a Word document using the LaTeX through AppendMath API.
The following code example illustrates how to create bar equation using LaTeX in Word document.
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an bar equation using LaTeX.
document.LastParagraph.AppendMath(@"\overline{a}");
//Save the Word document to MemoryStream
using MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an bar equation using LaTeX.
document.LastParagraph.AppendMath(@"\overline{a}");
//Save the Word document.
document.Save("Result.docx", FormatType.Docx);
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an bar equation using LaTeX.
document.LastParagraph.AppendMath(@"\overline{a}")
'Save the Word document.
document.Save("Result.docx", FormatType.Docx)
You can download a complete working sample from GitHub.
The following table demonstrates the LaTeX equivalent to professional format bar equations.
S.No | Professional | LaTeX |
---|---|---|
1. | \overline{a} | |
2. | \underline{abc} |
Box
Add box equation to a Word document using the LaTeX through AppendMath API.
The following code example illustrates how to create box equation using LaTeX in Word document.
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an box equation using LaTeX.
document.LastParagraph.AppendMath(@"\box{a}");
//Save the Word document to MemoryStream
using MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an box equation using LaTeX.
document.LastParagraph.AppendMath(@"\box{a}");
//Save the Word document.
document.Save("Result.docx", FormatType.Docx);
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an box equation using LaTeX.
document.LastParagraph.AppendMath(@"\box{a}")
'Save the Word document.
document.Save("Result.docx", FormatType.Docx)
You can download a complete working sample from GitHub.
The following table demonstrates the LaTeX equivalent to professional format box equations.
S.No | Professional | LaTeX |
---|---|---|
1. | \box{a} |
Border Box
Add border box equation to a Word document using the LaTeX through AppendMath API.
The following code example illustrates how to create border box equation using LaTeX in Word document.
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an border box equation using LaTeX.
document.LastParagraph.AppendMath(@"\boxed{{x}^{2}+{y}^{2}={z}^{2}}");
//Save the Word document to MemoryStream
using MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an border box equation using LaTeX.
document.LastParagraph.AppendMath(@"\boxed{{x}^{2}+{y}^{2}={z}^{2}}");
//Save the Word document.
document.Save("Result.docx", FormatType.Docx);
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an border box equation using LaTeX.
document.LastParagraph.AppendMath(@"\boxed{{x}^{2}+{y}^{2}={z}^{2}}")
'Save the Word document.
document.Save("Result.docx", FormatType.Docx)
You can download a complete working sample from GitHub.
The following table demonstrates the LaTeX equivalent to professional format border box equations.
S.No | Professional | LaTeX |
---|---|---|
1. | \boxed{{x}^{2}+{y}^{2}={z}^{2}} |
Delimiter
Add delimiter equation to a Word document using the LaTeX through AppendMath API.
The following code example illustrates how to create delimiter equation using LaTeX in Word document.
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an delimiter equation using LaTeX.
document.LastParagraph.AppendMath(@"\left(a\right)");
//Save the Word document to MemoryStream
using MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an delimiter equation using LaTeX.
document.LastParagraph.AppendMath(@"\left(a\right)");
//Save the Word document.
document.Save("Result.docx", FormatType.Docx);
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an delimiter equation using LaTeX.
document.LastParagraph.AppendMath(@"\left(a\right)")
'Save the Word document.
document.Save("Result.docx", FormatType.Docx)
You can download a complete working sample from GitHub.
The following table demonstrates the LaTeX equivalent to professional format delimiter equations.
S.No | Professional | LaTeX |
---|---|---|
1. | \left(a\right) | |
2. | \left[a\right] | |
3. | \left\{a\right\} | |
4. | \left\lfloora\right\rfloor | |
5. | \left\lceila\right\rceil | |
6. | \left|a\right| | |
7. | \left[a\right[ | |
8. | \left]a\right] | |
9. | \left]a\right[ | |
10. | \left(a\middle|b\right) | |
11. | \left\{a\middle|b\right\} | |
12. | \left\langlea\middle|b\right\rangle | |
13. | \left\langlea\middle|b\middle|c\right\rangle | |
14. | \left(a\right. | |
15. | \left. a\right) | |
16. | \left[a\right. | |
17. | \left. a\right] | |
18. | \left\{a\right. | |
19. | \left. a\right\} | |
20. | \left\langlea\right. | |
21. | \left. a\right\rangle | |
22. | \left\lfloora\right. | |
23. | \left. a\right\rfloor | |
24. | \left\lceila\right. | |
25. | \left. a\right\rceil | |
26. | \left|a\right. | |
27. | \left. a\right| | |
28. | \binom{a}{b} |
Equation Array
Add equation array to a Word document using the LaTeX through AppendMath API.
The following code example illustrates how to create an equation array using LaTeX in Word document.
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an box equation using LaTeX.
document.LastParagraph.AppendMath(@"\eqarray{a@&b}");
//Save the Word document to MemoryStream
using MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an box equation using LaTeX.
document.LastParagraph.AppendMath(@"\eqarray{a@&b}");
//Save the Word document.
document.Save("Result.docx", FormatType.Docx);
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an box equation using LaTeX.
document.LastParagraph.AppendMath(@"\eqarray{a@&b}");
'Save the Word document.
document.Save("Result.docx", FormatType.Docx)
You can download a complete working sample from GitHub.
The following table demonstrates the LaTeX equivalent to professional format equation array.
S.No | Professional | LaTeX |
---|---|---|
1. | \eqarray{a@&b} |
Fraction
Add fraction equation to a Word document using the LaTeX through AppendMath API.
The following code example illustrates how to create fraction equation using LaTeX in Word document.
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an fraction equation using LaTeX.
document.LastParagraph.AppendMath(@"{\frac{dy}{dx}}");
//Save the Word document to MemoryStream
using MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an fraction equation using LaTeX.
document.LastParagraph.AppendMath(@"{\frac{dy}{dx}}");
//Save the Word document.
document.Save("Result.docx", FormatType.Docx);
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an fraction equation using LaTeX.
document.LastParagraph.AppendMath(@"{\frac{dy}{dx}}")
'Save the Word document.
document.Save("Result.docx", FormatType.Docx)
You can download a complete working sample from GitHub.
The following table demonstrates the LaTeX equivalent to professional format fraction equations.
S.No | Professional | LaTeX |
---|---|---|
1. | frac{\mathbit{dy}}{\mathbit{dx}} | |
2. | \sfrac{dy}{dx} | |
3. | {\frac{dy}{dx}} |
Function
Add function equation to a Word document using the LaTeX through AppendMath API.
The following code example illustrates how to create function equation using LaTeX in Word document.
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an function equation using LaTeX.
document.LastParagraph.AppendMath(@"\sin{\theta}");
//Save the Word document to MemoryStream
using MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an function equation using LaTeX.
document.LastParagraph.AppendMath(@"\sin{\theta}");
//Save the Word document.
document.Save("Result.docx", FormatType.Docx);
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an function equation using LaTeX.
document.LastParagraph.AppendMath(@"\sin{\theta}")
'Save the Word document.
document.Save("Result.docx", FormatType.Docx)
You can download a complete working sample from GitHub.
The following table demonstrates the LaTeX equivalent to professional format function equations.
S.No | Professional | LaTeX |
---|---|---|
1. | \sin{\theta} | |
2. | \cos{\theta} | |
3. | \tan{\theta} | |
4. | \csc{\theta} | |
5. | \sec{\theta} | |
6. | \cot{\theta} | |
7. | \sin^{-1}{\theta} | |
8. | \cos^{-1}{\theta} | |
9. | \tan^{-1}{\theta} | |
10. | \csc^{-1}{\theta} | |
11. | \sec^{-1}{\theta} | |
12. | \cot^{-1}{\theta} | |
13. | \sinh{\theta} | |
14. | \cosh{\theta} | |
15. | \tanh{\theta} | |
16. | \csch{\theta} | |
17. | \sech{\theta} | |
18. | \coth{\theta} | |
19. | \sinh^{-1}{\theta} | |
20. | \cosh^{-1}{\theta} | |
21. | \tanh^{-1}{\theta} | |
22. | \csch^{-1}{\theta} | |
23. | \sech^{-1}{\theta} | |
24. | \coth^{-1}{\theta} | |
25. | \arcsin{\theta} | |
26. | \arccos{\theta} | |
27. | \arctan{\theta} | |
28. | \arccsc{\theta} | |
29. | \arcsec{\theta} | |
30. | \arccot{\theta} |
Group character
Add group character equation to a Word document using the LaTeX through AppendMath API.
The following code example illustrates how to create group character equation using LaTeX in Word document.
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an group character equation using LaTeX.
document.LastParagraph.AppendMath(@"\overbrace{a-b}");
//Save the Word document to MemoryStream
using MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an group character equation using LaTeX.
document.LastParagraph.AppendMath(@"\overbrace{a-b}");
//Save the Word document.
document.Save("Result.docx", FormatType.Docx);
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an group character equation using LaTeX.
document.LastParagraph.AppendMath(@"\overbrace{a-b}")
'Save the Word document.
document.Save("Result.docx", FormatType.Docx)
You can download a complete working sample from GitHub.
The following table demonstrates the LaTeX equivalent to professional format group character equations.
S.No | Professional | LaTeX |
---|---|---|
1. | \overbrace{a-b} | |
2. | \underbrace{a} |
Limit
Add limit equation to a Word document using the LaTeX through AppendMath API.
The following code example illustrates how to create limit equation using LaTeX in Word document.
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an limit equation using LaTeX.
document.LastParagraph.AppendMath(@"\lim\below{b}{a}");
//Save the Word document to MemoryStream
using MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an limit equation using LaTeX.
document.LastParagraph.AppendMath(@"\lim\below{b}{a}");
//Save the Word document.
document.Save("Result.docx", FormatType.Docx);
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an limit equation using LaTeX.
document.LastParagraph.AppendMath(@"\lim\below{b}{a}")
'Save the Word document.
document.Save("Result.docx", FormatType.Docx)
You can download a complete working sample from GitHub.
The following table demonstrates the LaTeX equivalent to professional format limit equations.
S.No | Professional | LaTeX |
---|---|---|
1. | \lim\below{b}{a} | |
2. | \min\below{b}{a} | |
3. | \max\below{b}{a} |
Matrix
Add matrix equation to a Word document using the LaTeX through AppendMath API.
The following code example illustrates how to create matrix equation using LaTeX in Word document.
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an matrix equation using LaTeX.
document.LastParagraph.AppendMath(@"\begin{matrix}a&b\\\end{matrix}");
//Save the Word document to MemoryStream
using MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an matrix equation using LaTeX.
document.LastParagraph.AppendMath(@"\begin{matrix}a&b\\\end{matrix}");
//Save the Word document.
document.Save("Result.docx", FormatType.Docx);
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an matrix equation using LaTeX.
document.LastParagraph.AppendMath(@"\begin{matrix}a&b\\\end{matrix}")
'Save the Word document.
document.Save("Result.docx", FormatType.Docx)
You can download a complete working sample from GitHub.
The following table demonstrates the LaTeX equivalent to professional format matrix equations.
S.No | Professional | LaTeX |
---|---|---|
1. | \begin{matrix}\mathbit{a}&\mathbit{b}\\\end{matrix} |
N-array
Add N-array equation to a Word document using the LaTeX through AppendMath API.
The following code example illustrates how to create N-array equation using LaTeX in Word document.
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an N-array equation using LaTeX.
document.LastParagraph.AppendMath(@"\sum{a}");
//Save the Word document to MemoryStream
using MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an N-array equation using LaTeX.
document.LastParagraph.AppendMath(@"\sum{a}");
//Save the Word document.
document.Save("Result.docx", FormatType.Docx);
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an N-array equation using LaTeX.
document.LastParagraph.AppendMath(@"\sum{a}")
'Save the Word document.
document.Save("Result.docx", FormatType.Docx)
You can download a complete working sample from GitHub.
The following table demonstrates the LaTeX equivalent to professional format N-array equations.
S.No | Professional | LaTeX |
---|---|---|
1. | \sum{a} | |
2. | \sum_{l}^{u}a | |
3. | \sum_{l}^{u}a | |
4. | \sum_{l}a | |
5. | \sum_{b}a | |
6. | \prod{a} | |
7. | \amalg{a} | |
8. | \bigcup{a} | |
9. | \bigcap{a} | |
10. | \bigvee{a} | |
11. | \bigwedge{a} |
Radical
Add radical equation to a Word document using the LaTeX through AppendMath API.
The following code example illustrates how to create radical equation using LaTeX in Word document.
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an radical equation using LaTeX.
document.LastParagraph.AppendMath(@"\sqrt{a}");
//Save the Word document to MemoryStream
using MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an radical equation using LaTeX.
document.LastParagraph.AppendMath(@"\sqrt{a}");
//Save the Word document.
document.Save("Result.docx", FormatType.Docx);
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an radical equation using LaTeX.
document.LastParagraph.AppendMath(@"\sqrt{a}")
'Save the Word document.
document.Save("Result.docx", FormatType.Docx)
You can download a complete working sample from GitHub.
The following table demonstrates the LaTeX equivalent to professional format radical equations.
S.No | Professional | LaTeX |
---|---|---|
1. | \sqrt{a} | |
2. | \sqrt[b]{a} |
SubSuperScript
Add SubSuperScript equation to a Word document using the LaTeX through AppendMath API.
The following code example illustrates how to create SubSuperScript equation using LaTeX in Word document.
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an SubSuperScript equation using LaTeX.
document.LastParagraph.AppendMath((@"{a}^{b}");
//Save the Word document to MemoryStream
using MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an SubSuperScript equation using LaTeX.
document.LastParagraph.AppendMath((@"{a}^{b}");
//Save the Word document.
document.Save("Result.docx", FormatType.Docx);
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an SubSuperScript equation using LaTeX.
document.LastParagraph.AppendMath((@"{a}^{b}")
'Save the Word document.
document.Save("Result.docx", FormatType.Docx)
You can download a complete working sample from GitHub.
The following table demonstrates the LaTeX equivalent to professional format SubSuperScript equations.
S.No | Professional | LaTeX |
---|---|---|
1. | {\mathbit{a}}^{\mathbit{b}} | |
2. | {\mathbit{a}}_{\mathbit{b}} |
Left SubSuperScript
Add Left SubSuperScript equation to a Word document using the LaTeX through AppendMath API.
The following code example illustrates how to create Left SubSuperScript equation using LaTeX in Word document.
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an Left SubSuperScript equation using LaTeX.
document.LastParagraph.AppendMath(@"{_{40}^{20}}{100}");
//Save the Word document to MemoryStream
using MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an Left SubSuperScript equation using LaTeX.
document.LastParagraph.AppendMath(@"{_{40}^{20}}{100}");
//Save the Word document.
document.Save("Result.docx", FormatType.Docx);
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an Left SubSuperScript equation using LaTeX.
document.LastParagraph.AppendMath(@"{_{40}^{20}}{100}");
'Save the Word document.
document.Save("Result.docx", FormatType.Docx)
You can download a complete working sample from GitHub
The following table demonstrates the LaTeX equivalent to professional format Left SubSuperScript equations.
S.No | Professional | LaTeX |
---|---|---|
1. | {_{\mathbf{40}}^{\mathbf{20}}}{\mathbf{100}} |
Right SubSuperScript
Add Right SubSuperScript equation to a Word document using the LaTeX through AppendMath API.
The following code example illustrates how to create Right SubSuperScript equation using LaTeX in Word document.
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an Right SubSuperScript equation using LaTeX.
document.LastParagraph.AppendMath(@"{100}_{40}^{20}");
//Save the Word document to MemoryStream
using MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
// Create a new Word document.
using WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an Right SubSuperScript equation using LaTeX.
document.LastParagraph.AppendMath(@"{100}_{40}^{20}");
//Save the Word document.
document.Save("Result.docx", FormatType.Docx);
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an Right SubSuperScript equation using LaTeX.
document.LastParagraph.AppendMath(@"{100}_{40}^{20}")
'Save the Word document.
document.Save("Result.docx", FormatType.Docx)
You can download a complete working sample from GitHub.
The following table demonstrates the LaTeX equivalent to professional format Right SubSuperScript equations.
S.No | Professional | LaTeX |
---|---|---|
1. | {\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 .NET 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.
// Create a new Word document.
using (WordDocument document = new WordDocument())
{
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an accent equation with bold using LaTeX.
document.LastParagraph.AppendMath(@"\dot{\mathbf{a}}");
//Append an accent equation with bold-italic using LaTeX.
document.LastSection.AddParagraph().AppendMath(@"\dot{\mathbit{a}}");
//Save a Word document to the MemoryStream.
using (MemoryStream stream = new MemoryStream())
{
document.Save(stream, FormatType.Docx);
}
}
// Create a new Word document.
using (WordDocument document = new WordDocument())
{
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an accent equation with bold using LaTeX.
document.LastParagraph.AppendMath(@"\dot{\mathbf{a}}");
//Append an accent equation with bold-italic using LaTeX.
document.LastSection.AddParagraph().AppendMath(@"\dot{\mathbit{a}}");
//Save a Word document.
document.Save("Result.docx", FormatType.Docx);
}
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an accent equation with Bold using LaTeX.
document.LastParagraph.AppendMath(@"\dot{\mathbf{a}}")
'Append an accent equation with Bold-Italic using LaTeX.
document. LastSection.AddParagraph().AppendMath(@"\dot{\mathbit{a}}")
'Save the Word document.
document.Save("Result.docx", FormatType.Docx)
Apply scripts to the equation
Apply scripts, such as double-struck, fraktur, and more, to equations in a Word document using LaTeX with the .NET 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.
// Create a new Word document.
using (WordDocument document = new WordDocument())
{
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an accent equation with Double-Struck font using LaTeX.
document.LastParagraph.AppendMath(@"\dot{\mathbb{a}}");
//Append an accent equation with Fraktur font using LaTeX.
document.LastSection.AddParagraph().AppendMath(@"\dot{\mathfrak{a}}");
//Append an accent equation with Sans Serif font using LaTeX.
document.LastSection.AddParagraph().AppendMath(@"\dot{\mathsf{a}}");
//Append an accent equation with Script using LaTeX.
document.LastSection.AddParagraph().AppendMath(@"\dot{\mathcal{a}}");
//Append an accent equation with Script using LaTeX.
document.LastSection.AddParagraph().AppendMath(@"\dot{\mathscr{a}}");
//Save a Word document to the MemoryStream.
using (MemoryStream stream = new MemoryStream())
{
document.Save(stream, FormatType.Docx);
}
}
// Create a new Word document.
using (WordDocument document = new WordDocument())
{
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an accent equation with DoubleStruck font using LaTeX.
document.LastParagraph.AppendMath(@"\dot{\mathbb{a}}");
//Append an accent equation with Fraktur font using LaTeX.
document.LastSection.AddParagraph().AppendMath(@"\dot{\mathfrak{a}}");
//Append an accent equation with SansSerif font using LaTeX.
document.LastSection.AddParagraph().AppendMath(@"\dot{\mathsf{a}}");
//Append an accent equation with Script using LaTeX.
document.LastSection.AddParagraph().AppendMath(@"\dot{\mathcal{a}}");
//Append an accent equation with Script using LaTeX.
document.LastSection.AddParagraph().AppendMath(@"\dot{\mathscr{a}}");
//Save a Word document.
document.Save("Result.docx", FormatType.Docx);
}
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an accent equation with DoubleStruck font using LaTeX.
document.LastParagraph.AppendMath(@"\dot{\mathbb{a}}")
'Append an accent equation with Fraktur font using LaTeX.
document.LastSection.AddParagraph().AppendMath(@"\dot{\mathfrak{a}}")
'Append an accent equation with SansSerif font using LaTeX.
document. LastSection.AddParagraph().AppendMath(@"\dot{\mathsf{a}}")
'Append an accent equation with Script using LaTeX.
document. LastSection.AddParagraph().AppendMath(@"\dot{\mathcal{a}}")
'Append an accent equation with Script using LaTeX.
document. LastSection.AddParagraph().AppendMath(@"\dot{\mathscr{a}}")
'Save a Word document.
document.Save("Result.docx", FormatType.Docx)
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.
// Create a new Word document.
using (WordDocument document = new WordDocument())
{
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an accent equation as normal text using LaTeX.
document.LastParagraph.AppendMath(@"\dot{\mathrm{a}}");
//Save a Word document to the MemoryStream.
using (MemoryStream stream = new MemoryStream())
{
document.Save(stream, FormatType.Docx);
}
}
// Create a new Word document.
using (WordDocument document = new WordDocument())
{
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an accent equation as normal text using LaTeX.
document.LastParagraph.AppendMath(@"\dot{\mathrm{a}}");
//Save a Word document.
document.Save("Result.docx", FormatType.Docx);
}
' Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append an accent equation as normal text using LaTeX.
document.LastParagraph.AppendMath(@"\dot{\mathrm{a}}")
'Save a Word document.
document.Save("Result.docx", FormatType.Docx)
You can download a complete working sample from GitHub.
Apply Math Justification
Apply justification, such as Left, Right, and more to the equation in a Word document using the .NET Word Library.
The following code examples show how to apply the justification to equations in a Word document.
// Create a new Word document.
using (WordDocument document = new WordDocument())
{
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an border box equation using LaTeX.
WMath math = document.LastParagraph.AppendMath(@"\boxed{{x}^{2}+{y}^{2}={z}^{2}}");
//Apply math justification.
math.MathParagraph.Justification = MathJustification.Left;
using (FileStream outputFileStream = new FileStream("Output.docx", FileMode.Create, FileAccess.ReadWrite))
{
//Save Word document.
document.Save(outputFileStream, FormatType.Docx);
}
}
//Create a new Word document.
WordDocument document = new WordDocument();
//Add one section and one paragraph to the document.
document.EnsureMinimal();
//Append an border box equation using LaTeX.
WMath math = document.LastParagraph.AppendMath(@"\boxed{{x}^{2}+{y}^{2}={z}^{2}}");
//Apply math justification.
math.MathParagraph.Justification = MathJustification.Left;
//Save Word document.
document.Save("Output.docx", FormatType.Docx);
'Create a new Word document.
Dim document As New WordDocument()
'Add one section and one paragraph to the document.
document.EnsureMinimal()
'Append a border box equation using LaTeX.
Dim math As WMath = document.LastParagraph.AppendMath("\boxed{{x}^{2}+{y}^{2}={z}^{2}}")
'Apply math justification.
math.MathParagraph.Justification = MathJustification.Left
'Save Word document.
document.Save("Output.docx", FormatType.Docx)
You can download a complete working sample from GitHub.
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 the LaTeX equation in a Word document.
// Open the FileStream for reading and writing the "Template.docx" file
FileStream fileStream = new FileStream("Template.docx", FileMode.Open, FileAccess.ReadWrite);
// Load the template Word document from the FileStream
WordDocument document = new WordDocument(fileStream, FormatType.Automatic);
// Access the first paragraph from the last section of the document
WParagraph paragraph = document.LastSection.Body.ChildEntities[0] as WParagraph;
// Retrieve the first math equation in the paragraph, if it exists
WMath math = paragraph.ChildEntities[0] as WMath;
if (math != null)
{
// Get the LaTeX representation of the math equation
string laTeX = math.MathParagraph.LaTeX;
// Replace occurrences of 'x' with 'k' in the LaTeX representation
math.MathParagraph.LaTeX = laTeX.Replace("x", "k");
}
//Saves and closes the Word document instance
MemoryStream stream = new MemoryStream();
//Saves the Word document to MemoryStream
document.Save(stream, FormatType.Docx);
document.Close();
// Open the FileStream for reading and writing the "Template.docx" file
WordDocument document = new WordDocument("Template.docx");
// Access the first paragraph from the last section of the document
WParagraph paragraph = document.LastSection.Body.ChildEntities[0] as WParagraph;
// Retrieve the first math equation in the paragraph, if it exists
WMath math = paragraph.ChildEntities[0] as WMath;
if (math != null)
{
// Get the LaTeX representation of the math equation
string laTeX = math.MathParagraph.LaTeX;
// Replace occurrences of 'x' with 'k' in the LaTeX representation
math.MathParagraph.LaTeX = laTeX.Replace("x", "k");
}
//Saves the word document
document.Save("Sample.docx");
//Close the word document
document.Close();
// Save the modified document to the FileStream in DOCX format
document.Save(stream, FormatType.Docx);
'Opens an existing Word document
Dim document As WordDocument = New WordDocument("Template.docx")
'Access the paragraph from Word document
Dim paragraph As WParagraph = CType(document.LastSection.Body.ChildEntities(0), WParagraph)
'Access the mathematical equation from the paragraph
Dim math As WMath = CType(paragraph.ChildEntities(0), WMath)If math IsNot Nothing Then
' Get the LaTeX representation of the math equation
Dim laTeX As String = math.MathParagraph.LaTeX
' Replace occurrences of 'x' with 'k' in the LaTeX representation
math.MathParagraph.LaTeX = laTeX.Replace("x", "k")
End If
'Saves the word document
document.Save("Sample.docx")
'Close the word document
document.Close()
You can download a complete working sample from GitHub.
Online Demo
- Explore how to create a Word document with mathematical equations using LaTeX and the .NET Word Library (DocIO) in a live demo here.
See Also
- How to create equation using LaTeX in Word document?
- How to insert equation using LaTeX in Word document using C#?
- How to convert equation in Word document to LaTeX using C#?
- How to modify existing equation using LaTeX in Word document using C#?
- How to create equation using LaTeX and convert as PDF in C#?
- How to create equation using LaTeX and convert as image in C#?