Other TeX conversion output formats | C++

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 C++ 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 typesetting options for no format on ObjectTeX engine extension.
 2System::SharedPtr<TeXOptions> options = TeXOptions::ConsoleAppOptions(TeXConfig::get_ObjectIniTeX());
 3// Specify a file system working directory for input.
 4options->set_InputWorkingDirectory(System::MakeObject<InputFileSystemDirectory>(RunExamples::InputDirectory));
 5// Specify a file system working directory for output.
 6options->set_OutputWorkingDirectory(System::MakeObject<OutputFileSystemDirectory>(RunExamples::OutputDirectory));
 7    
 8// Run format creation.
 9Aspose::TeX::TeXJob::CreateFormat(u"customtex", options);
10    
11// For further output to look write.
12options->get_TerminalOut()->get_Writer()->WriteLine();

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 typesetting options for a custom format on ObjectTeX engine extension.
 2System::SharedPtr<TeXOptions> options = TeXOptions::ConsoleAppOptions(TeXConfig::ObjectTeX(formatProvider));
 3options->set_JobName(u"typeset-with-custom-format");
 4// Specify the input working directory.
 5options->set_InputWorkingDirectory(wd);
 6// Specify a file system working directory for output.
 7options->set_OutputWorkingDirectory(System::MakeObject<OutputFileSystemDirectory>(RunExamples::OutputDirectory));
 8            
 9// Run typesetting.
10System::MakeObject<Aspose::TeX::TeXJob>(System::MakeObject<System::IO::MemoryStream>(System::Text::Encoding::get_ASCII()->GetBytes(u"Congratulations! You have successfully typeset this text with your own TeX format!\\end")), System::MakeObject<XpsDevice>(), options)->Run();
11            
12// For further output to look write.
13options->get_TerminalOut()->get_Writer()->WriteLine();

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.