Typesetting TeX input

Typesetting TeX input

Typesetting of TeX input runs by a single call of one of Aspose.TeX.TeX.Typeset(…); methods. The way typesetting will be performed is defined by a TeXOptions class instance passed as the argument. Also, all three methods accept a Device class instance which defines the output format. Two methods allow passing main TeX file as a stream or by a file name specification. The third one will prompt a file name online, i.e., from the console or whatever IInputTerminal implementation you specify in options as input terminal.

Typesetting options

TeXOptions class provides the factory method that returns the TeXOptions instance with properties set to default values. Namely, the TerminalIn property is set to be an InputConsoleTerminal class instance, the TerminalOut is set to be a OuputConsoleTerminal class instance, and the SaveOptions is set to be an XpsSaveOptions class instance as XPS is supposed to be the default output format.

Below, we will take a look at some examples on various use cases to make understanding easier.

Reading TeX input from a file system directory, writing output to a file system directory, setting up XPS as a destination format, and writing terminal output to the console

Here, the first thing we see is that the ConsoleAppOptions() factory method accepts TeXConfig.ObjectTeX() wich means that typesetting will run on the TeX engine extension called Object TeX. This extension contains auxiliary primitives to be capable of outputting to graphic formats. The default format is supposed to be the plain TeX plus Object TeX’s page size set to those of A4 paper sheets and content origin dimensions set to 1 inch for both horizontal and vertical.

Next, we see that TeXOption’s InputWorkingDirectory property is set be an InputFileSystemDirectory class instance defined by an absolute path of the directory that’s supposed to be used for input.

The similar way, the OutputWorkingDirectory property is set be an OutputFileSystemDirectory class instance defined by an absolute path of the directory that’s supposed to be used for output.

Then the TerminalOut property is unnecessarily set to its default (as we mentioned above) value.

Then, having it commented out, we show how terminal output can be written to memory, e.g., for complete suppression.

Next line is where the TeX engine (we mean TeX engine extension hereafter) comes to life. It reads hello-world.tex file from the InputWorkingDirectory and writes output XPS file and transcript file to the OutputWorkingDirectory. To specify the output format, we use an XpsDevice instance.

The last line is added to make further console output appear on a new line.

Overriding the job name and writing terminal output to a file

This example is similar to the previous one. The first difference is that on the second line we specify so called job name which is one of important TeX’s parameters. Having the JobName property initialized, the engine will take this name as a base for the output file, transcript file (log) and terminal output file in case the former one is being written to a file.

The second difference is that on the fifth line we specify the terminal output to be written to a file in InputWorkingDirectory.

Writing typeset XPS file to an external stream

The new feature we use here is instantiation of XpsDevice using a stream object. In this case the output XPS file will be written to this stream, and the rest of output will be routed to the OutputWorkingDirectory (except probably terminal output which can be routed separately).

Reading TeX input from a ZIP archive, writing output to a ZIP archive, setting up PDF as a destination format

Here the whole example is enclosed in two using clauses for two streams - an input stream on a ZIP archive that is supposed to be the TeX input source, and an output stream on a ZIP archive that is supposed to be the output destination.

The InputWorkingDirectory and OutputWorkingDirectory are set to be instances of InputZipDirectory and OutputZipDirectory, respectively. Note that you can specify a subdirectory inside the input ZIP archive. If input files are located in the root, then pass an empty string to the constructor.

In this example we use PdfDevice to specify that the output format must be PDF. To use this device, we must set the SaveOptions property to be a PdfSaveOptions class instance. This class has some properties for fine tuning of PDF output. To get familiar with these properties and their values, please refer to Aspose.TeX.Presentation.Pdf namespace documentation.

And the last line is necessary to finalize the output ZIP archive.

Writing terminal output to ZIP archive

By assignment of the TerminalOut property we force terminal output to be written to a file in the output ZIP archive.

Writing typeset PDF file to an external stream

Here we instantiate PdfDevice class using a stream object, so that output PDF file will be written to this stream. In this case the stream will pass output to a file on your disk. There’s also a commented option 2) which will let you write the output PDF to a file in the OutputWorkingDirectory, i.e., in the output ZIP archive.

Reading main TeX file as a stream, reading terminal input, setting up an image format as a destination format

This example demonstrates the usage of the version of Typeset(…); method that accepts main TeX file as a stream. All other auxiliary input files will be taken from the InputWorkingDirectory.

Here we wanted to retrieve the typesetting result as a set of PNG images (pages). To do so, we passed an instance of ImageDevice and set the SaveOptions property to be a PngSaveOptions class instance. To learn more about other implementations of ImageSaveOptions please refer to Aspose.TeX.Presentation.Image namespace reference.

The last line shows the alternative way to retrieve output images from a page-by-page array of byte arrays.