LaTeX math formulas rendering | .NET

How to render a LaTeX math formula to PNG

The API reference section related to this topic is here. In fact, the easiest way to demonstrate the LaTeX math formula rendering feature is to start with the example. Here it is:

 1// Create rendering options setting the image resolution to 150 dpi.
 2PngMathRendererOptions options = new PngMathRendererOptions();
 3options.Resolution = 150;
 4// Specify the preamble.
 5options.Preamble = @"\usepackage{amsmath}
 6\usepackage{amsfonts}
 7\usepackage{amssymb}
 8\usepackage{color}";
 9// Specify the scaling factor 300%.
10options.Scale = 3000;
11// Specify the foreground color.
12options.TextColor = System.Drawing.Color.Black;
13// Specify the background color.
14options.BackgroundColor = System.Drawing.Color.White;
15// Specify the output stream for the log file.
16options.LogStream = new System.IO.MemoryStream();
17// Specify whether to show the terminal output on the console or not.
18options.ShowTerminal = true;
19
20// Create the output stream for the formula image.
21using (System.IO.Stream stream = System.IO.File.Open(
22    System.IO.Path.Combine(RunExamples.OutputDirectory, "math-formula.png"), System.IO.FileMode.Create))
23{
24    // Run rendering.
25    System.Drawing.SizeF size = new PngMathRenderer().Render(@"\begin{equation*}
26e^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!}
27\end{equation*}", stream, options);
28    
29    // Show other results.
30    System.Console.Out.WriteLine(options.ErrorReport);
31    System.Console.Out.WriteLine();
32    System.Console.Out.WriteLine("Size: " + size); // Dimensions of the resulting image.
33}

Let’s get to the details. First of all, we create a rendering options instance, similar to the TeX/LaTeX typesetting. We do it here simultaneously specifying the output image resolution.

Next, we specify the preamble. The default preamble is:

1\usepackage{amsmath}
2\usepackage{amsfonts}
3\usepackage{amssymb}

which provides slightly more advanced math formula support than basic LaTeX. You can, for example, add the color package if you want to use your own highlighting in the formula, as we showed in the code example.

Then we instruct the renderer to scale the output by 300%.

Next two options define the foreground and background colors. Those parts of the formula that are not covered (“colored”) by the custom highlighting will be displayed in the TextColor color.

The next line of the example doesn’t make much sense. It just demonstrates that you can direct the log output to some stream.

And the last option ShowTerminal allows you to toggle writing the terminal output to the console.

The method that actually performs the rendering is MathRenderer.Render(). It returns the size of the formula in points.

The stream where the image is to be written is accepted by the method as the second argument. We create the stream next.

And finally, we call the MathRenderer.Render() method itself, passing options as the third argument. The LaTeX code of the formula is passed as the first argument.

The last lines of the example print two artifacts of math formula rendering - the size of the formula and the brief error report (in case there are errors).

Here is the result of rendering.

LaTeX Math Formula rendering to PNG

This is the most general use case for the LaTeX math formula rendering feature.

You may also check out the free web app built based on the feature implemented within Aspose.TeX for .NET API.

How to render a LaTeX math formula to SVG

In much the same way, we can render a LaTeX math formula to SVG format.

 1// Create rendering options.
 2MathRendererOptions options = new SvgMathRendererOptions();
 3// Specify the preamble.
 4options.Preamble = @"\usepackage{amsmath}
 5\usepackage{amsfonts}
 6\usepackage{amssymb}
 7\usepackage{color}";
 8// Specify the scaling factor 300%.
 9options.Scale = 3000;
10// Specify the foreground color.
11options.TextColor = System.Drawing.Color.Black;
12// Specify the background color.
13options.BackgroundColor = System.Drawing.Color.White;
14// Specify the output stream for the log file.
15options.LogStream = new System.IO.MemoryStream();
16// Specify whether to show the terminal output on the console or not.
17options.ShowTerminal = true;
18
19// Create the output stream for the formula image.
20using (System.IO.Stream stream = System.IO.File.Open(
21    System.IO.Path.Combine(RunExamples.OutputDirectory, "math-formula.svg"), System.IO.FileMode.Create))
22{
23    // Run rendering.
24    System.Drawing.SizeF size = new SvgMathRenderer().Render(@"\begin{equation*}
25e^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!}
26\end{equation*}", stream, options);
27    
28    // Show other results.
29    System.Console.Out.WriteLine(options.ErrorReport);
30    System.Console.Out.WriteLine();
31    System.Console.Out.WriteLine("Size: " + size); // Dimensions of the resulting image.
32}

The differences are:

Here is the result:

LaTeX Math Formula rendering to SVG You may also check out the free web app built based on the feature implemented within Aspose.TeX for .NET API.

Have any questions about Aspose.TeX?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.