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#.

## 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.

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:

1. Firstly, create an instance of the MathRendererOptions class
2. Next, specify the LaTeX document preamble.
3. Optionally, set various properties such as Scale, TextColor, BackgroundColor, etc.
4. After that, create the output stream for the formula image.
5. 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);


You can get a free temporary license to try the library without evaluation limitations.