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:
- 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.
- Create a file stream for a new ZIP archive to serve as the output working directory.
- Create typesetting options for the Object TeX format on the Object TeX engine extension.
- Specify ZIP archive working directories for input and output using the InputZipDirectory and OutputZipDirectory classes.
- Specify the console as the output terminal.
- Using the PdfSaveOptions class, specify save options for typesetting options.
- 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.
- 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());
}
FAQ
1. Can I typeset TeX documents using a ZIP file as input?
Aspose.TeX supports reading TeX source files from compressed archives like ZIP files. You can include all necessary files, including TeX source files, images, and additional resources, within a ZIP archive and then specify the ZIP file as input to the LaTeX compiler. This allows for convenient organization and distribution of LaTeX projects, especially when dealing with large or complex documents with multiple dependencies.
2. Are there any advantages to using ZIP files for typesetting TeX documents?
Yes, using ZIP files for typesetting TeX documents offers several advantages. Firstly, ZIP files provide a convenient way to package and organize multiple files associated with a LaTeX project, including TeX source files, bibliographies, images, and style files, into a single compressed archive. This simplifies distribution and sharing of LaTeX documents, especially when collaborating with others or sharing projects online.
3. How to typeset TeX files?
To typeset TeX files using the Aspose.TeX API Solution, you need to create conversion options for the Object TeX or Object LaTeX format based on the Object TeX engine extension. Then specify a file system working directory for the output. Finally, create an instance of the TeXJob class and run the typesetting process using the TeXJob.Run() 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.