LaTeX is a powerful software for typeset math. It allows embedding formulas and mathematical equations in the TEX file, which is a source document prepared by LaTeX. It provides many commands to specify the format of the document, including text, symbols, mathematical expressions, and graphics. We can write basic equations and mathematical formulas programmatically without using LaTeX. In this article, we will learn how to render LaTeX equations and math formulas using C#.
The following topics shall be covered in this article:
- C# API to Render LaTeX Equations and Math Formulas
- Render LaTeX Inline Math Formulas
- Render Complex Equations
- Display Long Equations
- Align Several Equations
- Group and Center Equations
- Render Matrices, Parenthesis, and Brackets
- Render Fractions and Binomials
C# API to Render LaTeX Equations and Math Formulas
For rendering LaTeX math formulas, we will be using the Aspose.TeX for .NET API. It allows typesetting TeX files to different file formats like PDF, XPS, or images.
Please either download the DLL of the API or install it using NuGet.
PM> Install-Package Aspose.TeX
Render LaTeX Inline Math Formulas using C#
We can render simple inline math formulas or equations by following the steps given below:
- Firstly, create an instance of the MathRendererOptions class
- Next, specify the LaTeX document preamble.
- Optionally, set various properties such as Scale, TextColor, BackgroundColor, etc.
- After that, create the output stream for the formula image.
- Finally, call the Render() method to render the formula. It takes formula string, stream, MathRendererOptions, and the output image size as arguments.
The following code sample demonstrates how to render a math formula programmatically using C#.
Render Complex Latex Equations in C#
We can render any complex Latex equations or formulas by following the steps mentioned earlier. However, we just need to provide the formula string in step # 4 as shown below:
MathRenderer.Render(@"\begin{equation*}
e^x = x^{\color{red}0} + x^{\color{red}1} +
\frac{x^{\color{red}2}}{2} +
\frac{x^{\color{red}3}}{6} +
\cdots = \sum_{n\geq 0} \frac{x^{\color{red}n}}{n!}
\end{equation*}", stream, options, out size);
Display Long Equations in C#
We can display long equations on multiple lines by following the steps mentioned earlier. However, we just need to provide the formula string in step # 4 as shown below:
MathRenderer.Render(@"\begin{document}
\begin{multline*}
p(x) = x^1+x^2+x^3+x^4\\
- x^4 - x^3 - x^2 - x
\end{multline*}
\end{document}", stream, options, out size);
Align Several Equations using C#
We can also align and render several equations or formulas at once by following the steps mentioned earlier. However, we just need to provide the formula string in step # 4 as shown below:
MathRenderer.Render(@"\begin{document}
\begin{align*}
a+b & a-b & (a+b)(a-b)\\
x+y & x-y & (x+y)(x-y)\\
p+q & p-q & (p+q)(p-q)
\end{align*}
\end{document}", stream, options, out size);
Group and Center Equations using C#
We can group and center multiple equations while rendering by following the steps mentioned earlier. However, we just need to provide the formula string in step # 4 as shown below:
MathRenderer.Render(@"\begin{gather*}
(a+b)=a^2+b^2+2ab \\
(a-b)=a^2+b^2-2ab \\
(a-b)=a^2+b^2-2ab
\end{gather*}", stream, options, out size);
Render Matrices, Parenthesis, and Brackets in C# {#Render-Matrices,-Parenthesis,-and-Brackets-in-CSharp}
We can also render matrices, parentheses, and brackets by following the steps mentioned earlier. However, we just need to provide the formula string in step # 4 as shown below:
MathRenderer.Render(@"\begin{document}
[
\left \{
\begin{tabular}{ccc}
1 & 4 & 7 \\
2 & 5 & 8 \\
3 & 6 & 9
\end{tabular}
\right \}
]
\end{document}", stream, options, out size);
Render Fractions and Binomials using C#
We can render fractions and binomials as well by following the steps mentioned earlier. However, we just need to provide the formula string in step # 4 as shown below:
MathRenderer.Render(@"\begin{document}
[
\binom{n}{k} = \frac{n!}{k!(n-k)!}
]
\end{document}", stream, options, out size);
Get a Free License
You can get a free temporary license to try the library without evaluation limitations.
Conclusion
In this article, we have learned how to:
- render simple and complex mathematical formulas and equations in C#
- align and group equations programmatically
- render Matrices, Parenthesis, Brackets, Fractions, and Binomials
Besides rendering LaTeX equations and Math formulas in C#, you can learn more about Aspose.TeX for .NET API using documentation. In case of any ambiguity, please feel free to contact us on our free support forum.