Render LaTeX Equations and Math Formulas using C#

Render Equations and Math Formulas using C#

LaTeX is a powerful software to 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

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:

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

The following code sample demonstrates how to render a math formula programmatically using C#.

Render Inline Math Formulas using C#
Render Inline Math Formulas using C#

Render Complex Equations in C#

We can render any complex equations or formulas by following the steps mentioned earlier. However, we just need to provide the formula string at 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);
Render Complex Equations in C#
Render Complex Equations in C#

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 at 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);
Display-Long-Equations
Display Long Equations in C#

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 at 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);
Align Several Equations using C#
Align Several Equations using C#

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 at 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);
Group-and-Center-Equations-using-CSharp
Group and Center Equations using C#

Render Matrices, Parenthesis, and Brackets in C#

We can also render matrices, parenthesis, and brackets by following the steps mentioned earlier. However, we just need to provide the formula string at 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 Matrices, Parenthesis, and Brackets in C#
Render Matrices, Parenthesis, and Brackets in C#

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 at step # 4 as shown below:

MathRenderer.Render(@"\begin{document}
	            \[
	                \binom{n}{k} = \frac{n!}{k!(n-k)!}
	            \]
	            \end{document}", stream, options, out size);
Render Fractions and Binomials using C#
Render Fractions and Binomials using C#

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, you can learn more about Aspose.TeX for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also