Other TeX conversion output formats | Java

It’s highly unlikely that you will currently want to convert a TeX file written in any other format than LaTeX. But this is possible if you are studying the TeX language and/or internals for some reason. Anyway, Aspose.TeX for Java allows you to convert files written in Plain TeX format. It also allows you to create custom formats and typeset documents designed in these formats.

We will start with creating a custom format.

Creating a custom format

Let’s remember that the format file is a binary dump of the TeX engine’s internal state.

 1// Create TeX engine options for no format upon ObjectTeX engine extension.
 2TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectIniTeX());
 3// Specify a file system working directory for the input.
 4options.setInputWorkingDirectory(new InputFileSystemDirectory(Utils.getInputDirectory()));
 5// Specify a file system working directory for the output.
 6options.setOutputWorkingDirectory(new OutputFileSystemDirectory(Utils.getOutputDirectory()));
 7
 8// Run format creation.
 9TeXJob.createFormat("customtex", options);
10
11// For further output to look fine.
12options.getTerminalOut().getWriter().newLine();

As you can see, the code is similar to the code for converting a TeX file. But there are a few differences.

First, here we use the TeXConfig.objectIniTeX() job configuration. This is a special configuration that leaves the engine’s state “virgin”, i.e., the internal parameters have their default values, and the set of control sequences coincides with the set of primitives. In our example, the set of primitives is extended in the sense mentioned here.

Next, we set up the input and output working directories as usual. The input working directory needs to contain the main format source file and all its dependencies.

And the second key difference is the way we run the job. This time we use the static createFormat() method, which together with the options takes the name of the main source file, that must be the same as the format name.

Typesetting a TeX file in your custom format

Now that we have our own TeX format, we want 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.
 3final FormatProvider formatProvider = new FormatProvider(
 4    new InputFileSystemDirectory(Utils.getOutputDirectory()), "customtex");
 5try {
 6  // Create conversion options for a custom format upon ObjectTeX engine extension.
 7  TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectTeX(formatProvider));
 8  options.setJobName("typeset-with-custom-format");
 9  // Specify the input working directory. This is not required here as we are providing the main input as a stream.
10  // But it is required when the main input has dependencies (e.g. images).
11  options.setInputWorkingDirectory(new InputFileSystemDirectory(Utils.getInputDirectory()));
12  // Specify a file system working directory for the output.
13  options.setOutputWorkingDirectory(new OutputFileSystemDirectory(Utils.getOutputDirectory()));
14  
15  // Run the job.
16  new TeXJob(new ByteArrayInputStream(
17    "Congratulations! You have successfully typeset this text with your own TeX format!\\end".getBytes("ASCII")),
18    new XpsDevice(), options).run();
19  
20  // For further output to look fine.
21  options.getTerminalOut().getWriter().newLine();
22} finally {
23  formatProvider.close();
24}

Obviously, we have to specify the format somehow. First of all, we need to create an instance of the FormatProvider class. Then, in the options constructor, we use the TeXConfig.objectTeX() configuration, which takes our format provider as an argument and loads the format on top of the “virgin” state of the engine.

The rest of the code should be familiar to you. It uses the features discussed earlier in this guide.

Typesetting a TeX file in Plain TeX format

If we throw away the format provider from the code just demonstrated, the engine will load the default format, which is Object TeX in its fourth sense. Thus, if you have a TeX file written in Plain TeX format, this way you may convert it to any supported target format.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.