Other TeX conversion output formats | Python

It is unlikely that you would currently need to convert a TeX file written in a format other than LaTeX. However, if you are studying the TeX language or TeX’s internals for any reason, it is still possible. In any case, Aspose.TeX for Python enables you to convert files written in Plain TeX format and also allows you to create custom formats and typeset documents designed in these formats.

To begin, we will create a custom format.

Creating a custom format

Let’s keep in mind that the format file is a binary representation of the TeX engine’s internal state.

 1# Create TeX engine options for no format upon ObjectTeX engine extension.
 2options = TeXOptions.console_app_options(TeXConfig.object_ini_tex)
 3# Specify a file system working directory for the input.
 4options.input_working_directory = InputFileSystemDirectory(Util.input_directory)
 5# Specify a file system working directory for the output.
 6options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
 7
 8# Run format creation.
 9TeXJob.create_format("customtex", options)
10
11# For further output to look fine.
12options.terminal_out.writer.write_line()

As you can see, the code bears a resemblance to the code for converting a TeX file. However, there are a few distinctions.

Firstly, in this case, we utilize the TeXConfig.object_ini_tex job configuration. This configuration ensures that the engine’s state is “virgin”, meaning that the internal parameters have their default values, and the set of control sequences aligns with the set of primitives. In our example, the set of primitives is expanded in the Aspose.TeX and Object TeX article.

Following that, we proceed with setting up the input and output working directories as usual. The input working directory should include the main format source file and all its dependencies.

And the second major difference is the method by which we execute the job. In this case, we utilize the static create_format() method, which, in conjunction with the options, requires the name of the main source file to be identical to the format name.

Typesetting a TeX file in your custom format

Now that we have created our own TeX format, we can proceed to typeset a TeX file written in this format. Here is the code:

 1# Create the format provider using the file system input working directory.
 2# We use the project output directory as our custom format file is supposed to be located there.
 3with FormatProvider(InputFileSystemDirectory(Util.output_directory), "customtex") as format_provider:
 4    # Create conversion options for a custom format upon ObjectTeX engine extension.
 5    options = TeXOptions.console_app_options(TeXConfig.object_tex(format_provider))
 6    options.job_name = "typeset-with-custom-format"
 7    # Specify the input working directory. This is not required here as we are providing the main input as a stream.
 8    # But it is required when the main input has dependencies (e.g. images).
 9    options.input_working_directory = InputFileSystemDirectory(Util.input_directory)
10    # Specify a file system working directory for the output.
11    options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
12
13    # Run the job.
14    TeXJob(BytesIO("Congratulations! You have successfully typeset this text with your own TeX format!\\end".encode('ascii')),
15           XpsDevice(), options).run()
16
17    # For further output to look fine.
18    options.terminal_out.writer.write_line()

To specify the format, we need to create an instance of the FormatProvider class. In the options constructor, we use the TeXConfig.object_tex() configuration, which requires our format provider as an argument and loads the format on top of the “virgin” state of the engine.

The remaining code should be familiar to you, as it utilizes the features that were discussed earlier in this guide.

Typesetting a TeX file in Plain TeX format

If we remove the format provider from the code shown above, the engine will load the default format, which is Object TeX in its fourth sense. Therefore, if you have a TeX file written in the Plain TeX format, you can convert it to any supported target format using this approach.

Have any questions about Aspose.TeX?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.