LaTeX Formula to SVG in C#

LaTeX is a powerful software for typeset math. Rendering LaTeX formulas to SVG images is a popular way to display equations on the web. We can write basic equations and mathematical formulas programmatically without using LaTeX and save as SVG image format. In this article, we will show you how to render LaTeX formulas to SVG in C#.

The following topics shall be covered in this article:

  1. C# API to Render LaTeX Formula to SVG
  2. Steps to Render LaTeX Formula to SVG
  3. Render LaTeX Formula to SVG

C# API to Render LaTeX Formula to SVG

For rendering LaTeX formulas to SVG format, we will be using the Aspose.TeX for .NET API. It allows typesetting TeX files to different file formats like PDF, XPS, or images.

The MathRendererOptions class of the API enables to specify common Math formula rendering options. The SvgMathRenderer class provides the Render(string, Stream, MathRendererOptions, out SizeF) method for implementing the rendering of math formula to SVG.

Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.TeX

How to Render LaTeX Formula to SVG in C#

We can render inline math formulas or complex equations to SVG format by following the steps given below:

  1. Initialize and Specify the SVG math renderer options.
  2. Specify the LaTeX document preamble.
  3. Initialize SVG math renderer.
  4. Render formula to SVG using the Renderer method.

The following section describes how to transform these steps into C# and render LaTeX formula to SVG.

Render LaTeX Formula to SVG using C#

Please follow the steps given below for rendering of math formulas to SVG format.

  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. Next, create the output stream for the formula image.
  5. After that, create an instance of the SvgMathRenderer class.
  6. 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 to SVG in C#.

Render LaTeX Formula to SVG using C#

Render LaTeX Formula to SVG 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 mathematical formulas and equations to SVG in C#. Besides, 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.

See Also