Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-com-gists/cd0acc55d1634f17dcf42e02075ca005 to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/cd0acc55d1634f17dcf42e02075ca005 to your computer and use it in GitHub Desktop.
Render LaTeX math formulas to PNG and SVG with .NET

Aspose.TeX for .NET – Rendering Math Examples

These snippets demonstrate rendering LaTeX mathematical formulas to images with Aspose.TeX for .NET. Convert complex mathematical expressions to high-quality PNG or SVG images with flexible configuration options for resolution, colors, and scaling.

Key Use Cases

  • Render LaTeX math formulas to PNG
  • How to render complex equations to PNG with custom resolution
  • Render LaTeX math formulas to SVG
  • How to render mathematical expressions to vector graphics
  • Render simple inline formulas
  • Configure rendering options (resolution, scale, colors)

How to Run Examples

  1. Reference Aspose.TeX for .NET: Aspose.TeX on Windows; Aspose.TeX.Drawing on non‑Windows.
  2. Copy a snippet into your project.
  3. Apply a temporary license as described in the licensing guide.
  4. Build and run.

Restrictions

In evaluation mode, the output may contain watermarks and limitations. Apply a valid license to unlock full features.

Related Documentation

More about LaTeX math rendering:

Related Resources

Requirements

  • .NET 6.0+, .NET Core, or .NET Framework
  • Aspose.TeX for .NET library

Examples

Rendering Complex Equations

The examples demonstrate rendering of complex mathematical expressions, including:

  • Exponential functions with colored elements
  • Fractions and summations
  • Custom preambles with AMS packages
  • Configurable scaling and resolution

Output Formats

  • PNG: Raster images with configurable resolution (DPI)
  • SVG: Vector graphics for scalable output
Aspose.TeX for .NET – Render Math Examples
// Render LaTeX math formula to PNG image
// Learn more: https://docs.aspose.com/tex/net/latex-math-formula-rendering/
// Create rendering options setting the image resolution to 150 dpi.
PngMathRendererOptions options = new PngMathRendererOptions();
options.Resolution = 150;
// Specify the preamble.
options.Preamble = @"\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{color}";
// Specify the scaling factor 300%.
options.Scale = 3000;
// Specify the foreground color.
options.TextColor = System.Drawing.Color.Black;
// Specify the background color.
options.BackgroundColor = System.Drawing.Color.White;
// Specify the output stream for the log file.
options.LogStream = new System.IO.MemoryStream();
// Specify whether to show the terminal output on the console or not.
options.ShowTerminal = true;
// Create the output stream for the formula image.
using (System.IO.Stream stream = System.IO.File.Open(
System.IO.Path.Combine(OutputDir, "math-formula.png"), System.IO.FileMode.Create))
{
// Run rendering.
System.Drawing.SizeF size = new PngMathRenderer().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);
// Show other results.
System.Console.Out.WriteLine(options.ErrorReport);
System.Console.Out.WriteLine();
System.Console.Out.WriteLine($"Size: {size}"); // Dimensions of the resulting image.
}
// Render LaTeX math formula to SVG image
// Learn more: https://docs.aspose.com/tex/net/latex-math-formula-rendering/
// Create rendering options.
MathRendererOptions options = new SvgMathRendererOptions();
// Specify the preamble.
options.Preamble = @"\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{color}";
// Specify the scaling factor 300%.
options.Scale = 3000;
// Specify the foreground color.
options.TextColor = System.Drawing.Color.Black;
// Specify the background color.
options.BackgroundColor = System.Drawing.Color.White;
// Specify the output stream for the log file.
options.LogStream = new System.IO.MemoryStream();
// Specify whether to show the terminal output on the console or not.
options.ShowTerminal = true;
// Create the output stream for the formula image.
using (System.IO.Stream stream = System.IO.File.Open(
System.IO.Path.Combine(OutputDir, "math-formula.svg"), System.IO.FileMode.Create))
{
// Run rendering.
System.Drawing.SizeF size = new SvgMathRenderer().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);
// Show other results.
System.Console.Out.WriteLine(options.ErrorReport);
System.Console.Out.WriteLine();
System.Console.Out.WriteLine($"Size: {size}"); // Dimensions of the resulting image.
}
// Render simple inline math formula to PNG
// Learn more: https://docs.aspose.com/tex/net/latex-math-formula-rendering/
// Create rendering options with default settings.
PngMathRendererOptions options = new PngMathRendererOptions();
options.Resolution = 150;
options.Scale = 3000;
// Create the output stream.
using (System.IO.Stream stream = System.IO.File.Open(
System.IO.Path.Combine(OutputDir, "simple-formula.png"), System.IO.FileMode.Create))
{
// Render a simple inline formula.
new PngMathRenderer().Render(@"$E=mc^2$", stream, options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment