Este interesante tema brinda una demostración sobre cómo renderizar LaTeX a PNG usando C++. También llamamos a los archivos LaTeX como archivos .tex y uno puede fácilmente renderizar LaTeX a PNG en C++ con propiedades y métodos API simples. Una cosa buena de usar este ejemplo es que las llamadas API no dependen de ningún otro software de aplicación o herramientas de terceros para su ejecución.
Pasos para renderizar LaTeX a PNG usando C++
- Instale Aspose.Tex.Cpp con la herramienta Administrador de paquetes NuGet
- Incluya una referencia a los espacios de nombres Aspose::TeX, Aspose::TeX::IO y Aspose::TeX::Presentation::Image
- Crear una instancia del objeto TeXOptions Class para establecer configuraciones
- Crear una instancia del objeto PngSaveOptions Class para guardar LaTeX en una imagen PNG
- Inicialice ImageDevice para renderizar
- Use TexJob para renderizar LaTeX a una imagen PNG
Los documentos LaTeX están destinados a fines científicos y de investigación que pueden ser utilizados por usuarios técnicos y contienen información en forma de texto sin formato. Para exportar LaTeX a una imagen PNG en C++, primero crearemos una instancia de la clase TeXOptions para establecer configuraciones que involucren el directorio de entrada, el directorio de salida y las opciones de la consola. En los pasos siguientes, configuraremos las propiedades PngSaveOptions como la resolución de la imagen. Finalmente, inicializaremos ImageDevice y usando TexJob renderizaremos LaTeX a una imagen PNG usando C++.
Código para convertir LaTeX a PNG en 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(); | |
} | |
}; |
En este tema, hemos aprendido cómo C++ crea una imagen PNG a partir de LaTeX usando simples llamadas a la API. Si está interesado en conocer otras funciones, como renderizar archivos MPP, consulte el artículo sobre cómo convertir MPP a XPS usando C++.