LaTeX math formulas rendering | Aspose.TeX for Java

How to render a LaTeX math formula to PNG

In fact, the easiest way to demonstrate the LaTeX math formula rendering feature is to start with the example. Here it is:

Set outputFile to a java.nio.file.Path in an existing folder, for example Paths.get(outputDirectory, "math-formula.png") for PNG or Paths.get(outputDirectory, "math-formula.svg") for SVG. Import the Aspose.TeX renderer, options and Size2D classes used in the snippet, plus java.awt.Color, java.io.ByteArrayOutputStream, java.io.OutputStream, java.nio.file.Files, java.nio.file.Path, and java.nio.file.Paths. The calling method must handle or declare exceptions. Apply a license as described in the licensing guide to avoid evaluation restrictions.

 1// Render a highlighted LaTeX math formula to PNG.
 2// outputFile is a java.nio.file.Path in an existing output folder.
 3PngMathRendererOptions options = new PngMathRendererOptions();
 4options.setResolution(150);
 5options.setPreamble("\\usepackage{amsmath}\n\\usepackage{amsfonts}\n"
 6        + "\\usepackage{amssymb}\n\\usepackage{color}");
 7options.setScale(3000); // 300%.
 8options.setTextColor(Color.BLACK);
 9options.setBackgroundColor(Color.WHITE);
10options.showTerminal(false);
11
12Size2D size;
13try (ByteArrayOutputStream log = new ByteArrayOutputStream();
14        OutputStream stream = Files.newOutputStream(outputFile)) {
15    options.setLogStream(log);
16    size = new PngMathRenderer().render("\\begin{equation*}\n"
17            + "e^x = x^{\\color{red}0} + x^{\\color{red}1} + \\frac{x^{\\color{red}2}}{2}"
18            + " + \\frac{x^{\\color{red}3}}{6} + \\cdots = \\sum_{n\\geq 0} \\frac{x^{\\color{red}n}}{n!}\n"
19            + "\\end{equation*}", stream, options);
20    System.out.println(options.getErrorReport());
21    System.out.println("Size: " + size.getWidth() + "x" + size.getHeight());
22}

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 color set by setTextColor().

The example directs rendering logs to a ByteArrayOutputStream using setLogStream(). The log and image output streams are closed automatically after rendering.

The showTerminal() option controls terminal output on the console. The examples set it to false; set it to true to display terminal output.

The method that actually performs the rendering is MathRenderer.render(), called on a PngMathRenderer or SvgMathRenderer instance. It returns a Size2D with the formula dimensions in points.

The method accepts the image output stream as its second argument. The example opens this stream in a try-with-resources block so it is closed after rendering.

Finally, we call render() on the renderer instance, passing the LaTeX formula as the first argument, the image output stream as the second, and the rendering options as the third.

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 were errors).

Here is the result of rendering.

LaTeX Figure rendering to PNG

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

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

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 a highlighted LaTeX math formula to SVG.
 2// outputFile is a java.nio.file.Path in an existing output folder.
 3SvgMathRendererOptions options = new SvgMathRendererOptions();
 4options.setPreamble("\\usepackage{amsmath}\n\\usepackage{amsfonts}\n"
 5        + "\\usepackage{amssymb}\n\\usepackage{color}");
 6options.setScale(3000); // 300%.
 7options.setTextColor(Color.BLACK);
 8options.setBackgroundColor(Color.WHITE);
 9options.showTerminal(false);
10
11Size2D size;
12try (ByteArrayOutputStream log = new ByteArrayOutputStream();
13        OutputStream stream = Files.newOutputStream(outputFile)) {
14    options.setLogStream(log);
15    size = new SvgMathRenderer().render("\\begin{equation*}\n"
16            + "e^x = x^{\\color{red}0} + x^{\\color{red}1} + \\frac{x^{\\color{red}2}}{2}"
17            + " + \\frac{x^{\\color{red}3}}{6} + \\cdots = \\sum_{n\\geq 0} \\frac{x^{\\color{red}n}}{n!}\n"
18            + "\\end{equation*}", stream, options);
19    System.out.println(options.getErrorReport());
20    System.out.println("Size: " + size.getWidth() + "x" + size.getHeight());
21}

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.