Topik menarik ini memberikan demonstrasi tentang cara render LaTeX ke PNG menggunakan C++. Kami juga memanggil file LaTeX sebagai file .tex dan seseorang dapat dengan mudah render LaTeX ke PNG dalam C++ dengan properti dan metode API sederhana. Satu hal yang baik tentang menggunakan contoh ini adalah bahwa panggilan API tidak bergantung pada perangkat lunak aplikasi lain atau alat pihak ketiga untuk eksekusinya.
Langkah-langkah untuk Render LaTeX ke PNG menggunakan C++
- Instal Aspose.Tex.Cpp menggunakan alat Pengelola paket NuGet
- Sertakan referensi ke Aspose::TeX, Aspose::TeX::IO dan Aspose::TeX::Presentation::Image namespaces
- Buat instance objek TeXOptions Class untuk mengatur konfigurasi
- Buat instance objek PngSaveOptions Class untuk menyimpan LaTeX ke gambar PNG
- Inisialisasi ImageDevice untuk rendering
- Gunakan TexJob untuk merender LaTeX ke gambar PNG
Dokumen LaTeX dimaksudkan untuk tujuan ilmiah dan penelitian yang dapat digunakan oleh pengguna teknis dan berisi informasi dalam bentuk teks biasa. Untuk mengekspor LaTeX ke gambar PNG di C++, pertama-tama kita akan membuat instance TeXOptions class untuk mengatur konfigurasi yang melibatkan direktori input, direktori output, dan opsi konsol. Pada langkah selanjutnya, kita akan mengatur properti PngSaveOptions seperti resolusi gambar. Terakhir, kita akan menginisialisasi ImageDevice dan dengan menggunakan TexJob render LaTeX ke gambar PNG menggunakan C++.
Kode untuk Mengonversi LaTeX ke PNG di C++
#pragma once | |
#include <system/text/encoding.h> | |
#include <system/io/text_writer.h> | |
#include <system/io/memory_stream.h> | |
#include <system/array.h> | |
#include <Aspose.TeX.Cpp/TeXOptions.h> | |
#include <Aspose.TeX.Cpp/TeXConfig.h> | |
#include <Aspose.TeX.Cpp/TeXJob.h> | |
#include <Aspose.TeX.Cpp/Presentation/SaveOptions.h> | |
#include <Aspose.TeX.Cpp/Presentation/Image/PngSaveOptions.h> | |
#include <Aspose.TeX.Cpp/Presentation/Image/ImageDevice.h> | |
#include <Aspose.TeX.Cpp/IO/OutputFileSystemDirectory.h> | |
#include <Aspose.TeX.Cpp/IO/OutputConsoleTerminal.h> | |
#include <Aspose.TeX.Cpp/IO/IOutputWorkingDirectory.h> | |
#include <Aspose.TeX.Cpp/IO/IOutputTerminal.h> | |
#include <Aspose.TeX.Cpp/IO/InputFileSystemDirectory.h> | |
#include <Aspose.TeX.Cpp/IO/InputConsoleTerminal.h> | |
#include <Aspose.TeX.Cpp/IO/IInputTerminal.h> | |
#include <cstdint> | |
#include <Aspose.TeX.Cpp/License.h> | |
using namespace Aspose::TeX; | |
using namespace Aspose::TeX::IO; | |
using namespace Aspose::TeX::Presentation::Image; | |
class TexToPngConverter{ | |
public: | |
static void TexToPNGRendering() | |
{ | |
// Initialize license object | |
System::SharedPtr<License> TexLicense = System::MakeObject<License>(); | |
// Applying license for rendering PNG | |
TexLicense->SetLicense(u"Aspose.Total.NET.lic"); | |
// Instantiate TeXOptions object for configuring settings | |
System::SharedPtr<TeXOptions> RenderingOptions = TeXOptions::ConsoleAppOptions(TeXConfig::ObjectTeX()); | |
// Specify the job name. | |
RenderingOptions->set_JobName(u"stream-image-out"); | |
// Specify the input file working directory. | |
RenderingOptions->set_InputWorkingDirectory(System::MakeObject<InputFileSystemDirectory>(u"")); | |
// Specify the output file working directory. | |
RenderingOptions->set_OutputWorkingDirectory(System::MakeObject<OutputFileSystemDirectory>(u"")); | |
// Setting the console input terminal | |
RenderingOptions->set_TerminalIn(System::MakeObject<InputConsoleTerminal>()); | |
// Setting the console output terminal | |
RenderingOptions->set_TerminalOut(System::MakeObject<OutputConsoleTerminal>()); | |
// Creating and specifying PngSaveOptions | |
System::SharedPtr<PngSaveOptions> ImageOptions = System::MakeObject<PngSaveOptions>(); | |
ImageOptions->set_Resolution(300); | |
RenderingOptions->set_SaveOptions(ImageOptions); | |
// Instantiating ImageDevice for rendering | |
System::SharedPtr<ImageDevice> ImageDevise = System::MakeObject<ImageDevice>(); | |
// Running TexJob for rendering to PNG | |
TeXJob::TeXJob(System::MakeObject<System::IO::MemoryStream>(System::Text::Encoding::get_ASCII()-> | |
GetBytes(u"\\hrule height 10pt width 95pt\\vskip10pt\\hrule height 5pt")), | |
ImageDevise, RenderingOptions); | |
// During executoion, when the console prompts the input, | |
// Step 1: Type "ABC", press Enter | |
// Step 2: Type "\end" and press Enter again | |
// In order to view the ouptut and to look write. | |
RenderingOptions->get_TerminalOut()->get_Writer()->WriteLine(); | |
// Alternatively, you can also get the images in form of array of byte arrays | |
// The 0th index belong to first page. | |
System::ArrayPtr<System::ArrayPtr<uint8_t>> result = ImageDevise->get_Result(); | |
} | |
}; |
Dalam topik ini, kita telah mempelajari bagaimana C++ membuat gambar PNG dari LaTeX menggunakan panggilan API sederhana. Jika Anda tertarik untuk mempelajari fitur lain seperti merender file MPP, lihat artikel di cara mengubah MPP ke XPS menggunakan C++.