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

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

The differences are:

Here is the result:

LaTeX Math Formula rendering to SVG

Building a LaTeX Math Rendering AI Agent with Aspose.TeX

For developers looking to encapsulate this functionality into a conversational or programmatic AI Agent, the core engineering challenge shifts from simple code execution to intent recognition, structure checking, and runtime error loop processing.

Below is the suggestion for the production workflow architecture for an AI agent that interprets user requests, builds valid LaTeX syntax, and returns fully rendered visual components.

Blockscheme for AI LaTeX math rendering workflow

As you can see - AI Agent for math rendering should operate as a closed feedback loop consisting of three micro-layers:

  1. Input and syntax harvesting (LLM layer) The user types in natural language or partial syntax expressions (e.g., “Show me the derivative of a matrix multiplication equation”). Agent transforms these prompts to valid LaTeX math structures inside a structured JSON payload, avoiding markdown wrappers like ````latex`.

  2. AST validation and sanitization (validator layer) Before routing strings directly to execution pipelines, the software agent validates token patterns to prevent security injections or broken execution chains.

  3. API execution pipeline (Aspose.TeX engine) The agent passes the sanitized token directly to the backend processing framework using the programmatic execution rules. The Agent instantiates either SvgMathRendererOptions or PngMathRendererOptions depending on the client context (e.g., if the user is on mobile or web, use SVG for optimal crispness).

  4. If the Aspose.TeX compilation fails or encounters syntax warnings, it pipes standard log diagnostics directly into a runtime error stream. Instead of silently failing or crashing user interfaces, the agent intercepts the stack trace data, builds a context loop, and prompts the LLM to rewrite the equation payload.

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