LaTeX to image | Aspose.TeX for Python
Aspose.TeX for Python provides us with the capability to convert LaTeX files into various raster image formats.
Converting LaTeX to PNG
Now, let’s examine the Python code that offers the most straightforward approach to converting LaTeX to PNG format in depth.
1# Create conversion options for Object LaTeX format upon Object TeX engine extension.
2options = TeXOptions.console_app_options(TeXConfig.object_latex)
3# Specify a file system working directory for the output.
4options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
5# Initialize the options for saving in PNG format.
6options.save_options = PngSaveOptions()
7
8# Run LaTeX to PNG conversion.
9TeXJob(path.join(Util.input_directory, "hello-world.ltx"), ImageDevice(True), options).run()
To start with, we need to create an instance of the TeXOptions class using the console_app_options() method. Don’t worry about the name, it’s just the static method that creates the instance. This method takes the object_latex instance of the TeXConfig class, which is perfect for converting a LaTeX file. This configuration sets up the Object TeX engine to load the Object LaTeX format and be ready to process the LaTeX file. The Object LaTeX format is essentially the same as the LaTeX format, but it uses Object TeX specific primitives to define the page metrics.
The first of the necessary options is output_working_directory, which specifies the location where the TeX output will be saved. If interested, you can learn more information about the concept of the output directory in Aspose.TeX for Python. In this particular example, we utilize the OutputFileSystemDirectory class to write the output to a specific directory or folder.
The second option is an instance of the SaveOptions class, which is responsible for managing the conversion from the object model to the desired format. In this case, since we are converting LaTeX to PNG, we use the PngSaveOptions class instance, which allows us to specify the resolution of the output images.
Next, we create an instance of the
TeXJob class. In this case, since we want to convert a LaTeX file stored in the file system, we use
this version of the constructor. We need to provide the full path to the file, otherwise, the engine will look for it in the current directory (which is the script’s working directory) and may not find it. However, the .tex
extension can be omitted as the engine will automatically append it. The second argument of the constructor is an instance of the
Device class. Since we are converting LaTeX to PNG, it’s an
ImageDevice class instance, which is common to all supported image formats. Finally, we pass the prepared conversion options as the last argument.
Now, all that remains is to run the job.
After the job is executed, regardless of its success or failure, the first output we will encounter is the terminal output. In the case of a successful run, the output will resemble the following:
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.
The results of the engine’s work can be found in the output directory that we specified. This will include the transcript file and, most importantly, the main output PNG image file(s).
An alternative way to write the main output PNG file(s)
There is an alternative method to obtain image data as an array of byte arrays, where each array in the second dimension represents the image data for a separate page.
1# Create conversion options for Object LaTeX format upon Object TeX engine extension.
2options = TeXOptions.console_app_options(TeXConfig.object_latex)
3# Specify a file system working directory for the output.
4options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
5# Initialize the options for saving in PNG format.
6so = PngSaveOptions()
7so.device_writes_images = False # Run LaTeX to PNG conversion.
8options.save_options = so
9device = ImageDevice(True)
10TeXJob(path.join(Util.input_directory, "hello-world.ltx"), device, options).run()
11
12# Save pages file by file.
13for i in range(len(device.result)):
14 with open(path.join(Util.output_directory, f"page-{(i + 1)}" + ".png"), "wb") as fs:
15 fs.write(device.result[i][0:len(device.result[i])])
The “page-n.png” file(s) can be written to any specified path. Unlike PDF output, these PNG files will duplicate the output PNG files that are written to the output directory.
Regarding input options
If our main input file requires dependencies, such as packages that are not included in the basic LaTeX system or supported packages, we need to set the
required_input_directory option in a similar way to how we set the output_working_directory option and place the dependencies in that directory. The dependencies can be organized in subdirectories as needed. If we have our files to include in the typesetting process, such as external graphics files, we also need to set the
input_working_directory to the location where those files are stored. We can also place the main input file within 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 directory. There are also more details about the
input directory concept in Aspose.TeX and the provided implementations described.
Also learn the other TeX job options here.
You can also explore the free web app for LaTeX-to-PNG conversion, which is developed using the Aspose.TeX for .NET API.
Below, we will briefly explain the conversion of LaTeX to other supported raster image formats, without diving into too much detail as there are no significant differences. The only variation lies in the type of the save_options property in the conversion options.
Converting LaTeX to JPG
1# Create conversion options instance.
2...
3# Initialize the options for saving in JPEG format.
4options.save_options = JpegSaveOptions()
You can also explore the free web app for LaTeX-to-JPG conversion, which is developed using the Aspose.TeX for .NET API.
Converting LaTeX to TIFF
1# Create conversion options instance.
2...
3# Initialize the options for saving in TIFF format.
4options.save_options = TiffSaveOptions()
You can also explore the free web app for LaTeX-to-TIFF conversion, which is developed using the Aspose.TeX for .NET API.
Converting LaTeX to BMP
1# Create conversion options instance.
2...
3# Initialize the options for saving in BMP format.
4options.save_options = BmpSaveOptions()
You can also explore the free web app for LaTeX-to-BMP conversion, which is developed using the Aspose.TeX for .NET API.
Converting LaTeX to SVG
Another supported target format is SVG. Scalable Vector Graphics (SVG) is an XML-based vector image format that is used to define two-dimensional graphics. It supports interactivity and animation.
SVG images are stored in XML text files and defined in a vector graphics format. This means that SVG images can be scaled in size without losing quality, and SVG files themselves can be searched, indexed, scripted, and compressed. The XML text files can be created and edited using text editors or vector graphics editors, and they can be rendered by the most commonly used web browsers.
The Aspose.SVG API offers a range of features for working with SVG, such as creating, loading, editing, and converting SVG documents.
Converting LaTeX to SVG is just as straightforward as converting to raster image formats, with the only difference being that the save_options must be set to an instance of the SvgSaveOptions class 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.
2options = TeXOptions.console_app_options(TeXConfig.object_latex)
3# Specify a file system working directory for the output.
4options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
5# Initialize the options for saving in SVG format.
6options.save_options = SvgSaveOptions()
7# Run LaTeX to SVG conversion.
8TeXJob(path.join(Util.input_directory, "hello-world.ltx"), SvgDevice(), options).run()
You can also explore the free web app for LaTeX-to-SVG conversion, which is developed using the Aspose.TeX for .NET API.