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

A format file is a binary representation of the TeX engine’s internal state. Apply a license before running these examples. Place customtex.tex in the CustomTeXFormat subdirectory of ASPOSE_TEX_INPUT_DIR (default: input). Set ASPOSE_TEX_OUTPUT_DIR to the output folder, or use the default output. The first example creates customtex.fmt there.

 1# Create a reusable custom TeX format.
 2import os
 3from pathlib import Path
 4from os import path
 5from io import BytesIO
 6from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 7from aspose.tex.io import InputFileSystemDirectory, InputZipDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
 8from aspose.tex.resourceproviders import FormatProvider
 9from aspose.tex.presentation.xps import XpsDevice
10
11output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
12output_dir.mkdir(parents=True, exist_ok=True)
13input_dir = Path(os.environ.get("ASPOSE_TEX_INPUT_DIR", "input")).resolve() / "CustomTeXFormat"
14# Create TeX engine options for no format upon ObjectTeX engine extension.
15options = TeXOptions.console_app_options(TeXConfig.object_ini_tex)
16options.interaction = Interaction.NONSTOP_MODE
17options.terminal_out = OutputMemoryTerminal()
18# Specify a file system working directory for the input.
19options.input_working_directory = InputFileSystemDirectory(str(input_dir))
20# Specify a file system working directory for the output.
21options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
22
23# Run format creation.
24TeXJob.create_format("customtex", options)
25
26# For further output to look fine.
27options.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. The following example creates customtex.fmt first so it can run independently, then loads that format and typesets text from a binary stream into typeset-with-custom-format.xps:

 1# Typeset text using a custom TeX format.
 2import os
 3from pathlib import Path
 4from os import path
 5from io import BytesIO
 6from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 7from aspose.tex.io import InputFileSystemDirectory, InputZipDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
 8from aspose.tex.resourceproviders import FormatProvider
 9from aspose.tex.presentation.xps import XpsDevice
10
11output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
12output_dir.mkdir(parents=True, exist_ok=True)
13input_dir = Path(os.environ.get("ASPOSE_TEX_INPUT_DIR", "input")).resolve() / "CustomTeXFormat"
14
15format_options = TeXOptions.console_app_options(TeXConfig.object_ini_tex)
16format_options.input_working_directory = InputFileSystemDirectory(str(input_dir))
17format_options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
18format_options.interaction = Interaction.NONSTOP_MODE
19format_options.terminal_out = OutputMemoryTerminal()
20TeXJob.create_format("customtex", format_options)
21# Create the format provider using the file system input working directory.
22# We use the project output directory as our custom format file is supposed to be located there.
23with FormatProvider(InputFileSystemDirectory(str(output_dir)), "customtex") as format_provider:
24    # Create conversion options for a custom format upon ObjectTeX engine extension.
25    options = TeXOptions.console_app_options(TeXConfig.object_tex(format_provider))
26    options.interaction = Interaction.NONSTOP_MODE
27    options.terminal_out = OutputMemoryTerminal()
28    options.job_name = "typeset-with-custom-format"
29    # Specify the input working directory. This is not required here as we are providing the main input as a stream.
30    # But it is required when the main input has dependencies (e.g. images).
31    options.input_working_directory = InputFileSystemDirectory(str(input_dir))
32    # Specify a file system working directory for the output.
33    options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
34
35    # Run the job.
36    TeXJob(BytesIO("Congratulations! You have successfully typeset this text with your own TeX format!\\end".encode('ascii')),
37           XpsDevice(), options).run()
38
39    # For further output to look fine.
40    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.