Other TeX conversion output formats | .NET
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 .NET 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.InputWorkingDirectory = new InputFileSystemDirectory(RunExamples.InputDirectory);
5// Specify a file system working directory for the output.
6options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
7
8// Run format creation.
9TeXJob.CreateFormat("customtex", options);
10
11// For further output to look fine.
12options.TerminalOut.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 the file system input working directory.
2// Create the format provider using the file system input working directory.
3// We use the project output directory as our custom format file is supposed to be located there.
4using (FormatProvider formatProvider =
5 new FormatProvider(new InputFileSystemDirectory(RunExamples.OutputDirectory), "customtex"))
6{
7 // Create conversion options for a custom format upon ObjectTeX engine extension.
8 TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectTeX(formatProvider));
9 options.JobName = "typeset-with-custom-format";
10 // Specify the input working directory. This is not required here as we are providing the main input as a stream.
11 // But it is required when the main input has dependencies (e.g. images).
12 options.InputWorkingDirectory = new InputFileSystemDirectory(RunExamples.InputDirectory);
13 // Specify a file system working directory for the output.
14 options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
15
16 // Run the job.
17 new TeXJob(new MemoryStream(Encoding.ASCII.GetBytes(
18 "Congratulations! You have successfully typeset this text with your own TeX format!\\end")),
19 new XpsDevice(), options).Run();
20
21 // For further output to look fine.
22 options.TerminalOut.Writer.WriteLine();
23}
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.