Convert LaTex to SVG using a C# LaTex Library

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 convert LaTeX to SVG in C#.

The following topics shall be covered in this article:

  1. C# LaTex Library Installation
  2. Convert LaTex to SVG in C# - Code Sample
  3. LaTex to SVG Online

C# LaTex Library Installation

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

Convert LaTex to SVG in C# - Code Sample

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.

Please follow the steps given below to convert LaTeX to SVG in C#.

  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# using this C# LaTex library.

Render LaTeX Formula to SVG using C#

Render LaTeX Formula to SVG using C#

LaTex to SVG Online

You can use thgis LaTex online render application which is powered by Aspose.TeX for .NET. It is free and provides robust rendering features.

LaTex to SVG online

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 this C# LaTex library using documentation. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also