LaTeX to image | Aspose.TeX for .NET

Aspose.TeX for .NET allows us to convert LaTeX files to a number of raster image formats.

How to convert LaTeX to PNG

Let’s take a detailed look at the code in C# providing the simplest way to convert LaTeX to PNG format.

1// Create conversion options for Object LaTeX format upon Object TeX engine extension.
2TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
3// Specify a file system working directory for the output.
4options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
5// Initialize the options for saving in PNG format.
6options.SaveOptions = new PngSaveOptions();
7// Run LaTeX to PNG conversion.
8new TeXJob(Path.Combine(RunExamples.InputDirectory, "hello-world.ltx"), new ImageDevice(), options).Run();

So, the first thing we need to do (well, sometimes not the very first) is to create an instance of the TeXOptions class. The only static method that does this is ConsoleAppOptions(), so let’s not be puzzled by 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 tells 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 .NET. In this example, we use the OutputFileSystemDirectory class, which lets us 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 lets us specify the resolution of the output images.

Then 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 need to specify the full path to the file. Otherwise, the engine will look for it in the current directory (which is CurrentDirectory) and most likely will not find it. Nevertheless, the .tex extension may be omitted. 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 instance, and this is common to all supported image formats. As the last argument, we pass the recently prepared conversion options.

All that’s left 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. If the run was successful, it will look 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 “fruits” of the engine’s labor in the folder that we specified as the output directory. Those will be the transcript file and, here it is!, 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// Create conversion options instance.
 2...
 3// Initialize the options for saving in PNG format.
 4PngSaveOptions pngSaveOptions = new PngSaveOptions();
 5pngSaveOptions.DeviceWritesImages = false;
 6options.SaveOptions = pngSaveOptions;
 7// Create the image device.
 8ImageDevice device = new ImageDevice();
 9// Run LaTeX to PNG conversion.
10new TeXJob(Path.Combine(RunExamples.InputDirectory, "hello-world.ltx"), device, options).Run();
11
12// Save pages file by file.
13for (int i = 0; i < device.Result.Length; i++)
14{
15    using (Stream fs = File.Open(Path.Combine(RunExamples.OutputDirectory, $"page-{i + 1}.png"), FileMode.Create))
16        fs.Write(device.Result[i], 0, device.Result[i].Length);
17}

The “page-n.png” file(s) will be written to any path we specify. Unlike PDF output, they will duplicate the output PNG files written to the output directory.

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 can also place the main input file somewhere inside the input directory and specify the relative path in the run() method (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 .NET 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.

Below, we discuss the LaTeX conversion to other supported raster image formats without sinking deep into details as there are actually no details. The only difference is in the type of the SaveOptions property in the conversion options.

How to convert LaTeX to JPG

1// Create conversion options instance.
2...
3// Initialize the options for saving in JPEG format.
4options.SaveOptions = new JpegSaveOptions();

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

How to convert LaTeX to TIFF

1// Create conversion options instance.
2...
3// Initialize the options for saving in TIFF format.
4options.SaveOptions = new TiffSaveOptions();

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

How to convert LaTeX to BMP

1// Create conversion options instance.
2...
3// Initialize the options for saving in BMP format.
4options.SaveOptions = new BmpSaveOptions();

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

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. SVG images can thus 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, and are rendered by the most-used web browsers.

There is the Aspose.SVG library that provides various features related to SVG. These include creating, loading, editing and converting SVG documents.

The LaTeX to SVG conversion is also as simple as the conversion to raster image formats, except that the SaveOptions MUST be set to an SvgSaveOptions class instance, and the device MUST be changed to an instance of the SvgDevice class.

1// Create conversion options for Object LaTeX format upon Object TeX engine extension.
2TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
3// Specify a file system working directory for the output.
4options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
5// Initialize the options for saving in SVG format.
6options.SaveOptions = new SvgSaveOptions();
7// Run LaTeX to SVG conversion.
8new TeXJob(Path.Combine(RunExamples.InputDirectory, "hello-world.ltx"), new PdfDevice(), options).Run();

You may also check out the free LaTeX-to-SVG conversion web app built based on Aspose.TeX for .NET API.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.