LaTeX Figure rendering | Aspose.TeX for Java
It may happen that you want to extract some content (a drawing, a writing, a plot, etc.) from a LaTeX file as a separately rendered piece, or a “figure”, regardless of its place on an output document page. An illustration for your publication on the Internet, for example, is the case. Our API can help you with the task. There are two available target formats - PNG and SVG. Just like in LaTeX math formula rendering. We must also note that LaTeX figure rendering is a generalization of LaTeX math formula rendering.
How to render a LaTeX figure to PNG
And as with formula rendering, we will start with an example. Here it is:
Set outputFile to a java.nio.file.Path in an existing folder, for example Paths.get(outputDirectory, "text-and-formula.png") for PNG or Paths.get(outputDirectory, "text-and-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 LaTeX figure to PNG.
2// outputFile is a java.nio.file.Path in an existing output folder.
3PngFigureRendererOptions options = new PngFigureRendererOptions();
4options.setResolution(150);
5options.setPreamble("\\usepackage{pict2e}");
6options.setScale(3000); // 300%.
7options.setMargin(5); // Leave space around the figure.
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 PngFigureRenderer().render("\\setlength{\\unitlength}{0.8cm}\r\n" +
16 "\\begin{picture}(6,5)\r\n" +
17 "\\thicklines\r\n" +
18 "\\put(1,0.5){\\line(2,1){3}} \\put(4,2){\\line(-2,1){2}} \\put(2,3){\\line(-2,-5){1}} \\put(0.7,0.3){$A$} \\put(4.05,1.9){$B$} \\put(1.7,2.95){$C$}\r\n" +
19 "\\put(3.1,2.5){$a$} \\put(1.3,1.7){$b$} \\put(2.5,1.05){$c$} \\put(0.3,4){$F=\\sqrt{s(s-a)(s-b)(s-c)}$} \\put(3.5,0.4){$\\displaystyle s:=\\frac{a+b+c}{2}$}\r\n" +
20 "\\end{picture}", stream, options);
21 System.out.println(options.getErrorReport());
22 System.out.println("Size: " + size.getWidth() + "x" + size.getHeight());
23}First of all, we create a rendering options instance. We do it here along with specifying the output image resolution.
Next, we specify the preamble. There’s no default preamble for LaTeX figure rendering, so if you want, for instance, to render some graphics plotted using the pict2e LaTeX package, you need to specify it in the preamble:
1\usepackage{pict2e}Then we instruct the renderer to scale the output by 300%. The examples also set a 5-point margin with setMargin(5) to leave space around the figure. The PNG example uses a resolution of 150 DPI.
The next option defines the background color. Unlike with math formula rendering, we don’t specify a foreground color since we assume that the colors are entirely under the control of the LaTeX code. In fact, so is the background color, so this is just a convenience option.
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
FigureRenderer.render(), called on a PngFigureRenderer or SvgFigureRenderer instance. It returns a Size2D with the figure 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 figure 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 figure rendering - the size of the figure and the brief error report (in case there are errors).
Here is the result of rendering.

This is the most general use case for the LaTeX figure rendering feature.
How to render a LaTeX figure to SVG
In much the same way, we can render a LaTeX figure to SVG format.
1// Render a LaTeX figure to SVG.
2// outputFile is a java.nio.file.Path in an existing output folder.
3SvgFigureRendererOptions options = new SvgFigureRendererOptions();
4options.setPreamble("\\usepackage{pict2e}");
5options.setScale(3000); // 300%.
6options.setMargin(5); // Leave space around the figure.
7options.setBackgroundColor(Color.WHITE);
8options.showTerminal(false);
9
10Size2D size;
11try (ByteArrayOutputStream log = new ByteArrayOutputStream();
12 OutputStream stream = Files.newOutputStream(outputFile)) {
13 options.setLogStream(log);
14 size = new SvgFigureRenderer().render("\\setlength{\\unitlength}{0.8cm}\r\n" +
15 "\\begin{picture}(6,5)\r\n" +
16 "\\thicklines\r\n" +
17 "\\put(1,0.5){\\line(2,1){3}} \\put(4,2){\\line(-2,1){2}} \\put(2,3){\\line(-2,-5){1}} \\put(0.7,0.3){$A$} \\put(4.05,1.9){$B$} \\put(1.7,2.95){$C$}\r\n" +
18 "\\put(3.1,2.5){$a$} \\put(1.3,1.7){$b$} \\put(2.5,1.05){$c$} \\put(0.3,4){$F=\\sqrt{s(s-a)(s-b)(s-c)}$} \\put(3.5,0.4){$\\displaystyle s:=\\frac{a+b+c}{2}$}\r\n" +
19 "\\end{picture}", stream, options);
20 System.out.println(options.getErrorReport());
21 System.out.println("Size: " + size.getWidth() + "x" + size.getHeight());
22}The differences are:
- We use SvgFigureRendererOptions class instead of PngFigureRendererOptions.
- We don’t specify the resolution.
- We use SvgFigureRenderer class instead of PngFigureRenderer.
Here is the result: