Como renderizar LaTeX para PNG usando C++

Este tópico interessante fornece uma demonstração de como renderizar LaTeX para PNG usando C++. Também chamamos os arquivos LaTeX como arquivos .tex e pode-se facilmente renderizar LaTeX para PNG em C++ com propriedades e métodos de API simples. Uma coisa boa sobre usar este exemplo é que as chamadas de API não dependem de nenhum outro software de aplicativo ou ferramentas de terceiros para sua execução.

Etapas para renderizar LaTeX para PNG usando C++

  1. Instale Aspose.Tex.Cpp usando a ferramenta NuGet Package Manager
  2. Incluir referência a Aspose::TeX, Aspose::TeX::IO e Aspose::TeX::Presentation::Image namespaces
  3. Instancie o objeto TeXOptions Class para definir as configurações
  4. Instanciar objeto PngSaveOptions Class para salvar LaTeX para imagem PNG
  5. Inicialize o ImageDevice para renderização
  6. Use TexJob para renderizar LaTeX para imagem PNG

Os documentos LaTeX são destinados a fins científicos e de pesquisa que podem ser usados por usuários técnicos e contêm informações em formato de texto simples. Para exportar uma imagem LaTeX para PNG em C++, primeiro criaremos uma instância da classe TeXOptions para definir configurações envolvendo diretório de entrada, diretório de saída e opções do console. Nas etapas subsequentes, definiremos as propriedades PngSaveOptions, como a resolução da imagem. Finalmente, vamos inicializar ImageDevice e usando TexJob renderizar LaTeX para imagem PNG usando C++.

Código para converter LaTeX para PNG em 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();
}
};

Neste tópico, aprendemos como C++ cria uma imagem PNG do LaTeX usando chamadas de API simples. Se você estiver interessado em aprender outros recursos, como renderizar arquivos MPP, consulte o artigo em como converter MPP para XPS usando C++.

 Português