Typeset TeX with ZIP I/O

Convert TeX to PDF using ZIP archives as input and output. API for C++

 

The TeX language is a typesetting tool that allows you to create professional documents without bothering about formatting. With TeX, you enter plain text containing text fragments of a document along with commands that control the output. At the output you will get a formatted file in the desired (specified) format. For a better understanding of TeX input and output, refer to the TeX documentation .

TeX is also a powerful, advanced system, but it’s not widely used outside of academic and scientific circles. PDF, on the other hand, is a portable format that can be easily viewed on most devices and is more widely accepted. The ability to read PDF is present on every device, in most modern browsers. Converting TeX to PDF allows the documents to be shared with a wider audience and provides a more accessible format for those without specialized software. Additionally, PDF provides several advantages over TeX, such as better font rendering, support for interactivity, and easier control of page size and layout.

The C++ code below typesets a TeX file, creating a PDF, which is then saved to a ZIP archive on the local file system. To accomplish this task, use the Aspose.TeX API for C++, which is a feature-rich, powerful, and easy-to-use document processing and conversion tool. To install the API, open the NuGet package manager and search for Aspose.TeX.Cpp. Alternatively, you can use the following command in the Package Manager Console:

Package Manager Console Command

PM> Install-Package Aspose.TeX.Cpp

In the code below, TeX takes the main input file from a ZIP archive, converts it to a PDF, which is written to another ZIP archive. Follow the guideline:

  1. Open a stream containing a ZIP archive that will serve as the input working directory. You can optionally specify a subdirectory in the archive. Otherwise, the file will be searched throughout the archive.
  2. Create a file stream for a new ZIP archive to serve as the output working directory.
  3. Create typesetting options for the Object TeX format on the Object TeX engine extension.
  4. Specify ZIP archive working directories for input and output using the InputZipDirectory and OutputZipDirectory classes.
  5. Specify the console as the output terminal.
  6. Using the PdfSaveOptions class, specify save options for typesetting options.
  7. Instantiate the TeXJob class passing the main input file name, a newly created PdfDevice object and typesetting options as arguments. Run the conversion by calling the Run() method.
  8. Finalize the output ZIP archive.

C++ code example: Converting TeX to PDF with ZIP input and output

using Aspose::TeX::IO;
using Aspose::TeX::Presentation::Pdf;
// Open a stream on a ZIP archive that will serve as the input working directory.
System::SharedPtr<System::IO::Stream> inZipStream =
    System::IO::File::Open(System::IO::Path::Combine(RunExamples::InputDirectory, u"zip-in.zip"), System::IO::FileMode::Open);
// Clearing resources under the 'using' statement
System::Details::DisposeGuard<1> __dispose_guard_1({ inZipStream });
try
{
    System::SharedPtr<System::IO::Stream> outZipStream =
        System::IO::File::Open(System::IO::Path::Combine(RunExamples::OutputDirectory, u"zip-pdf-out.zip"), System::IO::FileMode::Create);
    // Clearing resources under the 'using' statement
    System::Details::DisposeGuard<1> __dispose_guard_0({ outZipStream });
    try
    {
        // Create typesetting options for the default Object TeX format on the Object TeX engine extension.
        System::SharedPtr<TeXOptions> options = TeXOptions::ConsoleAppOptions(TeXConfig::ObjectTeX());
        // Specify a ZIP archive working directory for the input.
        options->set_InputWorkingDirectory(System::MakeObject<InputZipDirectory>(inZipStream, u"in"));
        // Specify a ZIP archive working directory for the output.
        options->set_OutputWorkingDirectory(System::MakeObject<OutputZipDirectory>(outZipStream));
        // Specify console as output terminal.
        options->set_TerminalOut(System::MakeObject<OutputConsoleTerminal>());

        // Create and specify saving options.
        options->set_SaveOptions(System::MakeObject<PdfSaveOptions>());
        // Run the typesetting job.
        System::MakeObject<Aspose::TeX::TeXJob>(u"hello-world", System::MakeObject<PdfDevice>(), options)->Run();

        // Finalize the output ZIP archive.
        (System::DynamicCast<Aspose::TeX::IO::OutputZipDirectory>(options->get_OutputWorkingDirectory()))->Finish();
    }
    catch(...)
    {
        __dispose_guard_0.SetCurrentException(std::current_exception());
    }
}
catch(...)
{
    __dispose_guard_1.SetCurrentException(std::current_exception());
}

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.