LaTeX math formulas rendering | 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:
1// Create rendering options setting the image resolution 150 dpi.
2PngMathRendererOptions options = new PngMathRendererOptions();
3options.setResolution(150);
4// Specify the preamble.
5options.setPreamble("\\usepackage{amsmath}\r\n\\usepackage{amsfonts}\r\n\\usepackage{amssymb}\r\n\\usepackage{color}");
6// Specify the scaling factor 300%.
7options.setScale(3000);
8// Specify the foreground color.
9options.setTextColor(Color.BLACK);
10// Specify the background color.
11options.setBackgroundColor(Color.WHITE);
12// Specify the output stream for the log file.
13options.setLogStream(new ByteArrayOutputStream());
14// Specify whether to show the terminal output on the console or not.
15options.showTerminal(true);
16
17// Create the output stream for the formula image.
18final OutputStream stream = new FileOutputStream(Utils.getOutputDirectory() + "math-formula.png");
19try {
20 // Run rendering.
21 com.aspose.tex.Size2D size = MathRenderer.render("\\begin{equation*}\r\n" +
22 "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!}\r\n" +
23 "\\end{equation*}", stream, options);
24
25 // Show other results.
26 System.out.println(options.getErrorReport());
27 System.out.println();
28 System.out.println("Size: " + size.getWidth() + "x" + size.getHeight()); // Dimensions of the resulting image.
29} finally {
30 if (stream != null)
31 stream.close();
32}
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 were errors).
Here is the result of rendering.
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// Create rendering options.
2MathRendererOptions options = new SvgMathRendererOptions();
3// Specify the preamble.
4options.setPreamble("\\usepackage{amsmath}\r\n\\usepackage{amsfonts}\r\n\\usepackage{amssymb}\r\n\\usepackage{color}");
5// Specify the scaling factor 300%.
6options.setScale(3000);
7// Specify the foreground color.
8options.setTextColor(Color.BLACK);
9// Specify the background color.
10options.setBackgroundColor(Color.WHITE);
11// Specify the output stream for the log file.
12options.setLogStream(new ByteArrayOutputStream());
13// Specify whether to show the terminal output on the console or not.
14options.showTerminal(true);
15
16// Create the output stream for the formula image.
17final OutputStream stream = new FileOutputStream(Utils.getOutputDirectory() + "math-formula.svg");
18try {
19 // Run rendering.
20 com.aspose.tex.Size2D size = new SvgMathRenderer().render("\\begin{equation*}\r\n" +
21 "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!}\r\n" +
22 "\\end{equation*}", stream, options, size);
23
24 // Show other results.
25 System.out.println(options.getErrorReport());
26 System.out.println();
27 System.out.println("Size: " + size.getWidth() + "x" + size.getHeight()); // Dimensions of the resulting image.
28} finally {
29 if (stream != null)
30 stream.close();
31}
The differences are:
- We use SvgMathRendererOptions class instead of PngMathRendererOptions.
- We don’t specify resolution.
- We use SvgMathRenderer class instead of PngMathRenderer.
Here is the result:
You may also check out the free web app built based on the feature implemented within Aspose.TeX for .NET API.