C++を使用してLaTeXをPNGにレンダリングする方法

この興味深いトピックは、C++を使用してLaTeXPNGにレンダリングする方法のデモンストレーションを提供します。また、LaTeXファイルを.texファイルと呼び、単純なAPIプロパティとメソッドを使用してLaTeXをC++でPNGに簡単にレンダリングできます。この例を使用することの良い点の1つは、API呼び出しが他のアプリケーションソフトウェアやサードパーティツールに依存せずに実行できることです。

C++を使用してLaTeXをPNGにレンダリングする手順

  1. NuGetパッケージマネージャーツールを使用してAspose.Tex.Cppをインストールします
  2. Aspose :: TeX、Aspose :: TeX :: IO、およびAspose :: TeX :: Presentation::Image名前空間への参照を含める
  3. 構成を設定するためにTeXOptions Classオブジェクトをインスタンス化します
  4. LaTeXをPNG画像に保存するためのPngSaveOptions Classオブジェクトをインスタンス化します
  5. レンダリング用にImageDeviceを初期化します
  6. LaTeXをPNG画像にレンダリングするためにTexJobを使用する

LaTeXドキュメントは、技術ユーザーが使用できる科学的および研究目的であり、プレーンテキスト形式の情報が含まれています。 LaTeXをC++でPNGイメージにエクスポートするために、最初に、入力ディレクトリ、出力ディレクトリ、およびコンソールオプションを含む構成を設定するためのTeXOptionsクラスのインスタンスを作成します。以降の手順では、画像の解像度などのPngSaveOptionsプロパティを設定します。最後に、* ImageDevice を初期化し、TexJobを使用して C++*を使用してLaTeXをPNG画像にレンダリングします。

C++でLaTeXをPNGに変換するコード

#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();
}
};

このトピックでは、C++が単純なAPI呼び出しを使用してLaTeXからPNG画像を作成する方法を学びました。 MPPファイルのレンダリングなどの他の機能の学習に興味がある場合は、C++を使用してMPPをXPSに変換する方法の記事を参照してください。

 日本語