Create custom TeX format

Typeset a TeX file in custom format via C# .NET

 

TeX is a programming language used to write programs for a computer document preparation system which is also called TeX (the TeX engine). Their main purpose is to help in writing mathematical or technical papers. The TeX language includes commands to control the typesetting process, whether by triggering some actions or by tuning some internal parameters of the engine. Using the TeX language syntax can be more productive if you define macros and parameter values for your convenience. You can place those definitions in a TeX file, which you will include at the beginning of every other TeX file that contains actual document data. Or you may avoid this by dumping the internal state of the engine after loading the definitions. Such binary dump is called a format file. Then you will only need to specify the format name (the name of the TeX format file) before typesetting any document file. The TeX engine will load such a format file much faster than a format in TeX syntax since format files do not need to be processed.

Nowadays, the most common format used is LaTeX. It's highly unlikely that you will need your format since LaTeX covers a very wide range of purposes. But if you ever need it, the Aspose.TeX API solution allows you to create your own TeX format. Here you will find information that explains how to create custom-format files, and how to typeset documents using a custom format. For this we need:

  • The Aspose.TeX for .NET API which is a feature-rich, powerful, and easy-to-use document manipulation and conversion API for C# platform.

  • Open the NuGet package manager, search for Aspose.TeX, and install it. You may also use the following command from the Package Manager Console

Package Manager Console Command

PM> Install-Package Aspose.TeX

Steps to create a custom TeX file C#.

  1. Using the ConsoleAppOptions() method of the TeXOptions class, instantiate TeX engine options for the Object TeX engine extension in its initial state (with no format at all).
  2. Using the InputFileSystemDirectory class, specify a file system working directory for the input.
  3. Use the OutputFileSystemDirectory class to specify a file system working directory for the output.
  4. Create the format file by calling the TeXJob.CreateFormat() method. The name you provide as an argument must be the name of your TeX file containing the format. The binary format file will have the same name.

C# code example: Creating a custom format file

using Aspose.TeX.IO;
using Aspose.TeX;
// Create the TeX engine options for no format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectIniTeX);
// Specify a file system working directory for the input.
options.InputWorkingDirectory = new InputFileSystemDirectory(RunExamples.InputDirectory);
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);

// Run the format creation.
TeXJob.CreateFormat("customtex", options);

Steps to typeset a TeX file in a custom format C#.

  1. Create the format provider using the InputFileSystemDirectory class.
  2. Create conversion options for your custom format upon the Object TeX engine extension using the ConsoleAppOptions() method of TeXOptions class.
  3. Specify the input working directory if the main input is not provided as a stream. Use the InputFileSystemDirectory class to do this.
  4. Specify a file system working directory for the output using the OutputFileSystemDirectory class.
  5. Create an object of the TeXJob class with an instance of XpsDevice and run the job by calling the Run() method. Here we also show how to pass the main TeX input file as a stream.

C# code example: Typesetting a TeX file in a custom format

using Aspose.TeX.IO;
using Aspose.TeX.Presentation.Xps;
using Aspose.TeX.ResourceProviders;
using System.IO;
using System.Text;
// Create the format provider using the file system input working directory.
// We use the project output directory as our custom format file is supposed to be located there.
using (FormatProvider formatProvider =
    new FormatProvider(new InputFileSystemDirectory(RunExamples.OutputDirectory), "customtex"))
{
    // Create conversion options for a custom format upon ObjectTeX engine extension.
    TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectTeX(formatProvider));
    options.JobName = "typeset-with-custom-format";
    // Specify the input working directory. This is not required here as we are providing the main input as a stream.
    // But it is required when the main input has dependencies (e.g. images).
    options.InputWorkingDirectory = new InputFileSystemDirectory(RunExamples.InputDirectory);
    // Specify a file system working directory for the output.
    options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);

    // Run the job.
    new TeXJob(new MemoryStream(Encoding.ASCII.GetBytes(
            "Congratulations! You have successfully typeset this text with your own TeX format!\\end")),
            new XpsDevice(), options).Run();
}



FAQ

1. What is a LaTeX template?

A template is a saved set of properties, settings or structure that can be applied to a newly created document to save time. For LaTeX, this can be a file that contains information about page format, fonts, document class, included packages, etc.

2. How do I use LaTeX templates?

If you are not using any additional software and the template is just a TeX file, just open it and start working. Just keep in mind that you don’t save the changes right in this document, but use the ‘Save as’ option instead.

3. How to set the type of a LaTeX document?

To specify the type of a document in LaTeX such as an article, book, or presentation, use the command \documentclass{...} with one of the supported class names within braces.

4. How to create a custom TeX format?

To create a custom TeX format using the Aspose.TeX API Solution you create proper TeX engine options first. Then you specify a file system working directory for the input and output. And finally, you create the format by calling the TeXJob.CreateFormat() method.

TeX What is TeX File Format

TeX is not actually a format. It is both a programming language and also an interpreter engine that understands this language. A TeX file is a plain text file created using TeX syntax to be converted to some target format by being processed by a TeX engine. This output document may include graphics, tables, lists, formulas, and equations.