Convert TeX Files to Images using C++

TeX is a typesetting system that has been noted as one of the most sophisticated digital typographical systems. TeX files are used for typesetting different documents and books containing text, symbols, and arithmetic expressions. Furthermore, these files can generate output in PNG, JPEG, TIFF, and BMP image formats. In this article, you will learn how to convert TeX files to images using C++.

C++ API for Converting TeX Files to Images

Aspose.TeX for C++ is a C++ library for working with TeX files without installing any additional software. The API allows you to typeset TeX files and generate output in PNG, JPEG, TIFF, and BMP image formats. You can either install the API through NuGet or download it directly from the Downloads section.

PM> Install-Package Aspose.TeX.Cpp

Convert TeX Files to JPG Images using C++

The following are the steps to convert TeX files to JPG images.

The following sample code shows how to convert a TeX file to a JPG image using C++.

// Create typesetting options for default ObjectTeX format on ObjectTeX engine extension.
System::SharedPtr<TeXOptions> options = TeXOptions::ConsoleAppOptions(TeXConfig::ObjectTeX());
// Specify a file system working directory for input.
options->set_InputWorkingDirectory(System::MakeObject<InputFileSystemDirectory>(u"SourceDirectory"));
// Specify a file system working directory for output.
options->set_OutputWorkingDirectory(System::MakeObject<OutputFileSystemDirectory>(u"OutputDirectory"));
// Specify console as output terminal.
options->set_TerminalOut(System::MakeObject<Aspose::TeX::IO::OutputConsoleTerminal>());
// Default. No need to specify.
// Specify memory stream as output terminal, if you don't want terminal output to be written to console.
// options.TerminalOut = new OutputMemoryTerminal();
// Set the save options
options->set_SaveOptions([&] {
auto tmp_0 = System::MakeObject<JpegSaveOptions>();
tmp_0->set_Resolution(300);
return tmp_0;
}());
// Run typesetting.
Aspose::TeX::TeX::Typeset(u"hello-world", System::MakeObject<ImageDevice>(), options);

Converting TeX Files to PNG Images using C++

In order to convert TeX Files to PNG images, follow the steps given below.

The following sample code demonstrates how to convert a TeX file to a PNG image using C++.

// Create typesetting options for default ObjectTeX format on ObjectTeX engine extension.
System::SharedPtr<TeXOptions> options = TeXOptions::ConsoleAppOptions(TeXConfig::ObjectTeX());
// Specify a file system working directory for input.
options->set_InputWorkingDirectory(System::MakeObject<InputFileSystemDirectory>(u"SourceDirectory"));
// Specify a file system working directory for output.
options->set_OutputWorkingDirectory(System::MakeObject<OutputFileSystemDirectory>(u"OutputDirectory"));
// Specify console as output terminal.
options->set_TerminalOut(System::MakeObject<Aspose::TeX::IO::OutputConsoleTerminal>());
// Default. No need to specify.
// Specify memory stream as output terminal, if you don't want terminal output to be written to console.
//options.TerminalOut = new OutputMemoryTerminal();
// Set the save options
options->set_SaveOptions([&] {
auto tmp_0 = System::MakeObject<PngSaveOptions>();
tmp_0->set_Resolution(300);
return tmp_0;
}());
// Run typesetting.
Aspose::TeX::TeX::Typeset(u"hello-world", System::MakeObject<ImageDevice>(), options);

Convert TeX Files to TIFF Images using C++

The following are the steps to convert TeX Files to TIFF images using C++.

The following sample code shows how to convert a TeX file to a TIFF image using C++.

// Create typesetting options for default ObjectTeX format on ObjectTeX engine extension.
System::SharedPtr<TeXOptions> options = TeXOptions::ConsoleAppOptions(TeXConfig::ObjectTeX());
// Specify a file system working directory for input.
options->set_InputWorkingDirectory(System::MakeObject<InputFileSystemDirectory>(u"SourceDirectory"));
// Specify a file system working directory for output.
options->set_OutputWorkingDirectory(System::MakeObject<OutputFileSystemDirectory>(u"OutputDirectory"));
// Specify console as output terminal.
options->set_TerminalOut(System::MakeObject<Aspose::TeX::IO::OutputConsoleTerminal>());
// Default. No need to specify.
// Specify memory stream as output terminal, if you don't want terminal output to be written to console.
//options.TerminalOut = new OutputMemoryTerminal();
// Set the save options
options->set_SaveOptions([&] {
auto tmp_0 = System::MakeObject<TiffSaveOptions>();
tmp_0->set_Resolution(300);
return tmp_0;
}());
// Run typesetting.
Aspose::TeX::TeX::Typeset(u"hello-world", System::MakeObject<ImageDevice>(), options);

Converting TeX Files to BMP Images using C++

The following are the steps to convert TeX files to BMP images using C++.

The following sample code shows how to convert a TeX file to a BMP image using C++.

// Create typesetting options for default ObjectTeX format on ObjectTeX engine extension.
System::SharedPtr<TeXOptions> options = TeXOptions::ConsoleAppOptions(TeXConfig::ObjectTeX());
// Specify a file system working directory for input.
options->set_InputWorkingDirectory(System::MakeObject<InputFileSystemDirectory>(u"SourceDirectory"));
// Specify a file system working directory for output.
options->set_OutputWorkingDirectory(System::MakeObject<OutputFileSystemDirectory>(u"OutputDirectory"));
// Specify console as output terminal.
options->set_TerminalOut(System::MakeObject<Aspose::TeX::IO::OutputConsoleTerminal>());
// Default. No need to specify.
// Specify memory stream as output terminal, if you don't want terminal output to be written to console.
//options.TerminalOut = new OutputMemoryTerminal();
// Set the save options
options->set_SaveOptions([&] {
auto tmp_0 = System::MakeObject<BmpSaveOptions>();
tmp_0->set_Resolution(300);
return tmp_0;
}());
// Run typesetting.
Aspose::TeX::TeX::Typeset(u"hello-world", System::MakeObject<ImageDevice>(), options);

Get a Free License

In order to try the API without evaluation limitations, you can request a free temporary license.

Conclusion

In this article, you have learned how to convert TeX files to images using C++. Specifically, you have learned how to convert TeX files to JPG, PNG, TIFF, and BMP image formats using Aspose.TeX for C++ API. The API provides many additional features for working with TeX files. You can explore the API in detail by visiting the official documentation. In case of any queries, please feel free to reach us at our free support forum.

See Also