LaTeX to image | Java

Aspose.TeX for Java provides features for converting LaTeX files to a number of raster image formats.

How to convert LaTeX to PNG

Let’s take a closer look at the Java code providing the simplest way to convert LaTeX to PNG format.

Set inputFile to the full path of your LaTeX file, such as hello-world.ltx, and outputDirectory to an existing output folder. Import the classes from com.aspose.tex and com.aspose.tex.rendering used in each snippet. The calling method must handle or declare exceptions. Apply a license as described in the licensing guide to avoid evaluation restrictions. The examples select noninteractive processing and close output resources. The PNG example sets the resolution to 300 DPI. For the single-page input hello-world.ltx, the automatic output files are hello-world.png, hello-world.jpg, hello-world.tif, hello-world.bmp, or hello-world.svg, depending on the selected format.

 1// Convert LaTeX to PNG.
 2// inputFile is the full input path; outputDirectory is an existing output folder.
 3TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
 4OutputFileSystemDirectory output = new OutputFileSystemDirectory(outputDirectory);
 5options.setOutputWorkingDirectory(output);
 6PngSaveOptions saveOptions = new PngSaveOptions();
 7saveOptions.setResolution(300);
 8options.setSaveOptions(saveOptions);
 9options.setInteraction(Interaction.NonstopMode);
10
11ImageDevice device = new ImageDevice();
12TeXJobResult result;
13try {
14    result = new TeXJob(inputFile, device, options).run();
15} finally {
16    device.dispose();
17    output.close();
18}

So, the first thing we need to do (sometimes not the very first, as we will find out later) is to create an instance of the TeXOptions class. The only static method that does this is consoleAppOptions(), so let’s not bother with the meaning of its name. The method takes the objectLaTeX() instance of the TeXConfig class, which is exactly suitable for converting a LaTeX file. This configuration instructs the Object TeX engine to load the Object LaTeX format and to be ready to accept the LaTeX file. Object LaTeX format is actually just the LaTeX format, except that it uses Object TeX specific primitives to set up the page metrics.

The first of the required options is OutputWorkingDirectory which defines the space, or area, where the TeX output will be written. Here are the details about the output directory concept in Aspose.TeX for Java. In this example, we use the OutputFileSystemDirectory class, which allows us to write the output to the specified directory, or folder.

The second option is a SaveOptions class instance which will control the transformation of the object model to the target format. Since we are converting LaTeX to PNG, it’s the PngSaveOptions class instance, which allows us to specify the resolution of the output images.

Next, we need to create an instance of the TeXJob class. Wanting to convert a LaTeX file stored in the file system, we use this version of the constructor. We should specify the full path to the file. Otherwise, the engine will look for it in the current directory (which is defined here) and most likely will not find it. Nevertheless, we may omit the extension if our file has the .tex one. The engine will append it automatically. The second argument of the constructor is a Device class instance. Since we are converting LaTeX to PNG, it’s an ImageDevice class (which is common to all supported image formats) instance. As the last argument, we pass the recently prepared conversion options.

All that we have to do now is to run the job.

Regardless of whether the run was successful or not, the first result that we’ll see will be the terminal output. In case of success, it looks something like this:

 1This is ObjectTeX, Version 3.1415926-1.0 (Aspose.TeX 21.8)
 2entering extended mode
 3
 4(<input_directory>\hello-world.ltx
 5LaTeX2e <2011/06/27>
 6(article.cls
 7Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
 8(size10.clo))
 9No file hello-world.aux.
10[1]
11(<output_directory>\hello-world.aux) )
12Output written on hello-world.png (1 page).
13Transcript written on hello-world.log.

We will find other products of the engine’s run in the folder that we specified as the output directory. Those will be the transcript (.log) file and, Voila!, the main output PNG image file(s).

An alternative way to write the main output PNG file(s)

There is another way to get image data as an array of byte arrays, each array in the second dimension represents image data for a separate page.

 1// Convert LaTeX to PNG and save each page from the returned byte arrays.
 2// inputFile is the full input path; outputDirectory is an existing output folder.
 3TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
 4OutputFileSystemDirectory output = new OutputFileSystemDirectory(outputDirectory);
 5options.setOutputWorkingDirectory(output);
 6PngSaveOptions saveOptions = new PngSaveOptions();
 7saveOptions.deviceWritesImages(false);
 8options.setSaveOptions(saveOptions);
 9options.setInteraction(Interaction.NonstopMode);
10
11ImageDevice device = new ImageDevice();
12TeXJobResult result;
13try {
14    result = new TeXJob(inputFile, device, options).run();
15    byte[][] pages = device.getResult();
16    for (int i = 0; i < pages.length; i++) {
17        Files.write(Paths.get(outputDirectory, "page-" + (i + 1) + ".png"), pages[i]);
18    }
19} finally {
20    device.dispose();
21    output.close();
22}

The manual example also requires java.nio.file.Files and java.nio.file.Paths. It saves page data from ImageDevice.getResult() as page-1.png, page-2.png, and so on. Because it sets deviceWritesImages(false), automatic PNG output is disabled and the manually saved pages are not duplicated in the output directory. This differs from PDF stream output, where the stream defines the destination of a single PDF document.

About input options

In case our main input file requires dependencies, for example, packages, that are not included in the basic LaTeX system and supported packages, we MUST set the RequiredInputDirectory option the similar way we set the OutputWorkingDirectory option and put the dependencies in that directory. Dependencies may be arbitrarily organized in subdirectories. In case we have our own files to include along the typesetting process, say external graphics files, we MUST also set the InputWorkingDirectory using the path to the location where those files are collected. We may also place the main input file somewhere inside the input directory and pass the relative path to the TeXJob constructor (or specify no path at all if the main input file is in the root). Here are the details about the input directory concept in Aspose.TeX for Java and provided implementations.

Other TeX job options are discussed here.

You may also check out the free LaTeX-to-PNG conversion web app built based on Aspose.TeX for .NET API. Here is the Java version page.

Below, we discuss the LaTeX conversion to other supported raster image formats. We won’t go into details just because there aren’t actually any details. The only difference is in the type of the SaveOptions property in the conversion options.

How to convert LaTeX to JPG

 1// Convert LaTeX to JPEG.
 2// inputFile is the full input path; outputDirectory is an existing output folder.
 3TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
 4OutputFileSystemDirectory output = new OutputFileSystemDirectory(outputDirectory);
 5options.setOutputWorkingDirectory(output);
 6options.setSaveOptions(new JpegSaveOptions());
 7options.setInteraction(Interaction.NonstopMode);
 8
 9ImageDevice device = new ImageDevice();
10TeXJobResult result;
11try {
12    result = new TeXJob(inputFile, device, options).run();
13} finally {
14    device.dispose();
15    output.close();
16}

You may also check out the free LaTeX-to-JPG conversion web app built based on Aspose.TeX for .NET API. Here is the Java version page.

How to convert LaTeX to TIFF

 1// Convert LaTeX to TIFF.
 2// inputFile is the full input path; outputDirectory is an existing output folder.
 3TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
 4OutputFileSystemDirectory output = new OutputFileSystemDirectory(outputDirectory);
 5options.setOutputWorkingDirectory(output);
 6options.setSaveOptions(new TiffSaveOptions());
 7options.setInteraction(Interaction.NonstopMode);
 8
 9ImageDevice device = new ImageDevice();
10TeXJobResult result;
11try {
12    result = new TeXJob(inputFile, device, options).run();
13} finally {
14    device.dispose();
15    output.close();
16}

You may also check out the free LaTeX-to-TIFF conversion web app built based on Aspose.TeX for .NET API. Here is the Java version page.

How to convert LaTeX to BMP

 1// Convert LaTeX to BMP.
 2// inputFile is the full input path; outputDirectory is an existing output folder.
 3TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
 4OutputFileSystemDirectory output = new OutputFileSystemDirectory(outputDirectory);
 5options.setOutputWorkingDirectory(output);
 6options.setSaveOptions(new BmpSaveOptions());
 7options.setInteraction(Interaction.NonstopMode);
 8
 9ImageDevice device = new ImageDevice();
10TeXJobResult result;
11try {
12    result = new TeXJob(inputFile, device, options).run();
13} finally {
14    device.dispose();
15    output.close();
16}

You may also check out the free LaTeX-to-BMP conversion web app built based on Aspose.TeX for .NET API. Here is the Java version page.

How to convert LaTeX to SVG

And yet another supported target format is SVG. Scalable Vector Graphics (SVG) is an XML-based vector image format for defining two-dimensional graphics, having support for interactivity and animation.

SVG images are defined in a vector graphics format and stored in XML text files. This means that SVG images can be scaled in size without loss of quality, and SVG files can be searched, indexed, scripted, and compressed. The XML text files can be created and edited with text editors or vector graphics editors. Thay are rendered by the most-used web browsers.

Also, there is the Aspose.SVG library that has various features based on the SVG format. These include creating, loading, editing and converting SVG documents.

The conversion to SVG is just as simple as the conversion to raster image formats, except that in addition to the SaveOptions, we have to change the device to an instance of the SvgDevice class.

 1// Convert LaTeX to SVG.
 2// inputFile is the full input path; outputDirectory is an existing output folder.
 3TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
 4OutputFileSystemDirectory output = new OutputFileSystemDirectory(outputDirectory);
 5options.setOutputWorkingDirectory(output);
 6options.setSaveOptions(new SvgSaveOptions());
 7options.setInteraction(Interaction.NonstopMode);
 8
 9SvgDevice device = new SvgDevice();
10TeXJobResult result;
11try {
12    result = new TeXJob(inputFile, device, options).run();
13} finally {
14    device.dispose();
15    output.close();
16}

You may also check out the free LaTeX-to-SVG conversion web app built based on Aspose.TeX for .NET . Here is the Java version page.