将输出写入 ZIP

通过 C++ 将 TeX 文件转换的结果保存为 ZIP

 

TeX 是一种数据标记语言,是计算机排版系统的核心,也称为出版集系统。虽然它通常被称为文件格式,但它实际上是一种编程语言和解释器引擎,用于创建数学、技术和其他复杂文档。但是,一旦创建了文件,可能需要将其转换为更流行的格式,以便能够在任何设备和平台上使用结果。

Aspose.TeX API 解决方案提供了转换 TeX 文件并将结果保存为 ZIP 存档的功能。此处提供的代码片段显示了如何将 TeX 文件转换为 PDF 并将输出保存为 zip 文件。 API 转换器可以将 TeX 文件转换为 C++ 语言,可用于创建跨平台应用程序或集成到您的 C++ 项目中。

要运行示例,您将需要 Aspose.TeX for C++ API,这是一个功能丰富且易于使用的 C++ 平台文档操作和转换工具。您可以通过在 NuGet 包管理器中搜索它或在包管理器控制台中使用 Install-Package Aspose.TeX 命令来安装 Aspose.TeX API。

Package Manager Console Command


    PM> Install-Package Aspose.TeX

此处的代码正在转换 TeX 文件并将其另存为 ZIP 存档中的 PDF。采取后续步骤来处理此类转换:

  1. 创建 TeXOptions 类的实例。使用它为使用 OutputWorkingDirectory 属性的输出指定 ZIP 存档工作目录。
  2. 使用 OutputConsoleTerminal 类将控制台指定为输出终端。
  3. 使用 PdfSaveOptions 类定义用于保存输出的选项。
  4. 使用 TeXJob() PdfDevice 方法运行 TeX 到 PDF 的转换。
  5. 细化输出。
  6. 使用 OutputZipDirectory 类完成输出 ZIP 存档。该代码使用 try-catch 块来处理过程中可能发生的任何异常。

使用 ZIP 目录进行输出的 C++ 代码

    using Aspose::TeX::IO;
    using Aspose::TeX::Presentation::Pdf;
    using System::IO;
    // Open a stream on a ZIP archive that will serve as the input working directory.
    {
        System::SharedPtr<System::IO::Stream> inZipStream = System::IO::File::Open(System::IO::Path::Combine(RunExamples::InputDirectory, u"zip-in.zip"), System::IO::FileMode::Open);
        // Clearing resources under 'using' statement
        System::Details::DisposeGuard<1> __dispose_guard_1({ inZipStream});
        // ------------------------------------------

        try{
            System::SharedPtr<System::IO::Stream> outZipStream = System::IO::File::Open(System::IO::Path::Combine(RunExamples::OutputDirectory, u"zip-pdf-out.zip"), System::IO::FileMode::Create);
            // Clearing resources under 'using' statement
            System::Details::DisposeGuard<1> __dispose_guard_0({ outZipStream});
            // ------------------------------------------

            try
            {
                // Create typesetting options for default ObjectTeX format on ObjectTeX engine extension.
                System::SharedPtr<TeXOptions> options = TeXOptions::ConsoleAppOptions(TeXConfig::ObjectTeX());
                // Specify a ZIP archive working directory for the input.
                options->set_InputWorkingDirectory(System::MakeObject<InputZipDirectory>(inZipStream, u"in"));
                // Specify a ZIP archive working directory for the output.
                options->set_OutputWorkingDirectory(System::MakeObject<OutputZipDirectory>(outZipStream));
                // Specify the console as an output terminal.
                options->set_TerminalOut(System::MakeObject<OutputConsoleTerminal>());
                // Default. Not necessary to specify.

                // Create and specify saving options.
                options->set_SaveOptions(System::MakeObject<PdfSaveOptions>());
                // Run typesetting.
                Aspose::TeX::TeX::Typeset(u"hello-world", System::MakeObject<PdfDevice>(), options);

                // For the consequent output to look right. 
                options->get_TerminalOut()->get_Writer()->WriteLine();

                // Finalize the output ZIP archive.
                (System::DynamicCast<Aspose::TeX::IO::OutputZipDirectory>(options->get_OutputWorkingDirectory()))->Finish();
            }
            catch(...)
            {
                __dispose_guard_0.SetCurrentException(std::current_exception());
            }
        }
        catch(...)
        {
            __dispose_guard_1.SetCurrentException(std::current_exception());
        }
    }

TeX 什么是TeX文件格式

TeX 实际上不是一种格式。它同时是一种编程语言,也是理解这种语言的解释器引擎。 TeX 文件是在 LaTeX 中创建的文档。该文档可以包括图形、表格、符号、列表、公式和方程式。