Render Equations and Math Formulas using C#

LaTeX is a robust tool for typesetting mathematical content, enabling the embedding of formulas and equations within a TEX file. This source document, prepared by LaTeX, offers numerous commands to format text, symbols, mathematical expressions, and graphics. In this article, you will discover how to render LaTeX equations and math formulas using C# efficiently.

Table of Contents

  1. C# API to Render LaTeX Equations and Math Formulas
  2. Render LaTeX Inline Math Formulas
  3. Render Complex Equations
  4. Display Long Equations
  5. Align Several Equations
  6. Group and Center Equations
  7. Render Matrices, Parentheses, and Brackets
  8. Render Fractions and Binomials

C# API to Render LaTeX Equations and Math Formulas

To render LaTeX math formulas, we will utilize the Aspose.TeX for .NET API. This powerful .NET LaTeX equation rendering library allows you to typeset TeX files into various formats, including PDF, XPS, or images.

You can either download the DLL of the API or install it using NuGet:

PM> Install-Package Aspose.TeX

Render LaTeX Inline Math Formulas using C#

Rendering simple inline math formulas or equations is straightforward. Follow these steps:

  1. Create an instance of the MathRendererOptions class.
  2. Specify the LaTeX document preamble.
  3. Optionally, adjust properties such as Scale, TextColor, BackgroundColor, etc.
  4. Create the output stream for the formula image.
  5. Call the Render() method to render the formula, passing the formula string, stream, MathRendererOptions, and the output image size as arguments.

Here’s a code sample demonstrating how to render a math formula programmatically using C#:

Render Inline Math Formulas using C#

Render Inline Math Formulas using C#

Render Complex LaTeX Equations in C#

You can render complex LaTeX equations by following the same steps as before, but with a more intricate formula string in step #4:

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#

To display long equations across multiple lines, use the following approach:

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#

You can align multiple equations by modifying the formula string in step #4:

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#

To group and center multiple equations, use the following code:

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

Group and Center Equations using C#

Render Matrices, Parentheses, and Brackets in C# {#Render-Matrices,-Parenthesis,-and-Brackets-in-CSharp}

Rendering matrices, parentheses, and brackets can be done similarly:

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#

To render fractions and binomials, follow this structure:

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 evaluate the library without limitations.

Conclusion

In this article, we explored how to:

  • Render simple and complex mathematical formulas and equations in C#.
  • Align and group equations programmatically.
  • Render matrices, parentheses, brackets, fractions, and binomials.

Beyond rendering LaTeX equations and math formulas in C#, you can delve deeper into the Aspose.TeX for .NET API by referring to the documentation. If you have any questions, feel free to reach out on our free support forum.

See Also