LaTeX to PDF | Aspose.TeX for Python
Converting LaTeX to PDF
Now, let’s examine the Python code that offers the most straightforward approach to converting LaTeX to PDF 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 PDF format.
6options.save_options = PdfSaveOptions()
7# Run LaTeX to PDF conversion.
8TeXJob(path.join(Util.input_directory, "hello-world.ltx"), PdfDevice(), options).run()
To begin, we need to create an instance of the TeXOptions class. The console_app_options() method is the only static method that accomplishes this, so don’t worry about its name. This method takes an object_latex instance of the TeXConfig class, which is perfect for converting a LaTeX file. This configuration instructs the Object TeX engine to load the Object LaTeX format and be prepared to process the LaTeX file. The Object LaTeX format is essentially the same as the LaTeX format, but it utilizes Object TeX specific primitives to define the page metrics.
The first option that needs to be specified is output_working_directory, which determines the location where the TeX output will be saved. If needed, you can find more information on the concept of the output directory in Aspose.TeX. In this particular case, we utilize the OutputFileSystemDirectory class, which enables us to write the output to a designated directory or folder.
The second option requires an instance of the SaveOptions class, which determines how the object model is transformed into the desired format. In our case, as we are converting LaTeX to PDF, it refers to an instance of the PdfSaveOptions class.
To convert a LaTeX file stored in the file system, we need to create an instance of the
TeXJob class using the
constructor that takes the file’s full path as a parameter. If we don’t specify the full path, the engine will look for the file in the current directory (which is the script’s working directory) and may not find it. However, we can omit the extension if our file has the .tex
extension, as the engine will automatically append it. The second argument of the constructor is an instance of the
Device class. In our case, since we are converting LaTeX to PDF, it’s an instance of the
PdfDevice class. Finally, we pass the recently prepared conversion options as the last argument.
Now, all that remains is to run the job.
After the execution, regardless of whether it was successful or not, the first thing we will see is the terminal output. If the execution is successful, the output will look similar to 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.pdf (1 page).
13Transcript written on hello-world.log.
In the folder we specified as the output directory, we will find the transcript file and, here it is!, the main output PDF file, among other results of the engine’s work.
An alternative way to write the main output PDF file
The PdfDevice class has another constructor that allows us to obtain the resulting PDF file differently.
1# Create the stream to write the PDF file to.
2with open(path.join(Util.output_directory, "any-name.pdf"), "wb") as pdf_stream:
3 # Create conversion options for Object LaTeX format upon Object TeX engine extension.
4 options = TeXOptions.console_app_options(TeXConfig.object_latex)
5 # Specify a file system working directory for the output.
6 options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
7 # Initialize the options for saving in PDF format.
8 options.save_options = PdfSaveOptions()
9 # Run LaTeX to PDF conversion.
10 TeXJob(path.join(Util.input_directory, "hello-world.ltx"), PdfDevice(pdf_stream), options).run()
The main output PDF file will be named any-name.pdf
and will be located in the specified directory. However, unlike the
image output, there will be no other PDF files in the output directory specified by the conversion options. The exception is if any-name.pdf
is located in the same file system directory assigned to the output_working_directory option using OutputFileSystemDirectory.
About input options
If our main input file requires dependencies, such as packages that are not included in the basic LaTeX system and supported packages, it is necessary to set the
required_input_directory option in a similar way as we set the output_working_directory option and place the dependencies in that directory. The dependencies can be organized in subdirectories as desired. Additionally, if we have our own files to include in the typesetting process, such as external graphics files, we must also set the
input_working_directory using the path to the location where those files are stored. We can also place the main input file 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. For more details about the input directory concept in Aspose.TeX for Python and the provided implementations, please refer to
here.
There are also other TeX job options discussed.
You can also explore the free web app for LaTeX-to-PDF conversion, which is developed using the Aspose.TeX for .NET API.