创建自定义 TeX 格式

通过 C# .NET 以自定义格式排版 TeX 文件

 

TeX 是一种编程语言,用于为计算机文档准备系统编写程序,该系统也称为 TeX(TeX 引擎)。它们的主要目的是帮助撰写数学或技术论文。TeX 语言包括控制排版过程的命令,无论是通过触发某些操作还是通过调整引擎的一些内部参数。为了方便起见,如果您定义了宏和参数值,使用 TeX 语言语法会更有效率。您可以将这些定义放在一个 TeX 文件中,并将其包含在每个包含实际文档数据的其他 TeX 文件的开头。或者,您可以通过在加载定义后转储引擎的内部状态来避免这种情况。这种二进制转储称为格式文件。然后,您只需在排版任何文档文件之前指定格式名称(TeX 格式文件的名称)即可。TeX 引擎加载此类格式文件的速度比 TeX 语法格式快得多,因为格式文件不需要处理。

如今,最常用的格式是 LaTeX。由于 LaTeX 涵盖了非常广泛的用途,您极不可能需要自己的格式。但如果您确实需要,Aspose.TeX API 解决方案允许您创建自己的 TeX 格式。在这里,您将找到解释如何创建自定义格式文件以及如何使用自定义格式排版文档的信息。为此,我们需要:

  • Aspose.TeX for .NET API,这是一个功能丰富、强大且易于使用的 C# 平台文档操作和转换 API。

  • 打开 NuGet 包管理器,搜索 Aspose.TeX 并安装。您也可以在包管理器控制台 (Package Manager Console) 中使用以下命令

Package Manager Console Command

PM> Install-Package Aspose.TeX

在 C# 中创建自定义 TeX 文件的步骤。

  1. 使用 TeXOptions 类的 ConsoleAppOptions() 方法,实例化 Object TeX 引擎扩展在其初始状态(完全没有格式)下的 TeX 引擎选项。
  2. 使用 InputFileSystemDirectory 类,为输入指定文件系统工作目录。
  3. 使用 OutputFileSystemDirectory 类为输出指定文件系统工作目录。
  4. 通过调用 TeXJob.CreateFormat() 方法创建格式文件。您作为参数提供的名称必须是包含该格式的 TeX 文件的名称。二进制格式文件将具有相同的名称。

C# 代码示例:创建自定义格式文件

using Aspose.TeX.IO;
using Aspose.TeX;
// Create the TeX engine options for no format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectIniTeX);
// Specify a file system working directory for the input.
options.InputWorkingDirectory = new InputFileSystemDirectory(RunExamples.InputDirectory);
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);

// Run the format creation.
TeXJob.CreateFormat("customtex", options);

在 C# 中以自定义格式排版 TeX 文件的步骤。

  1. 使用 InputFileSystemDirectory 类创建格式提供程序。
  2. 使用 TeXOptions 类的 ConsoleAppOptions() 方法,基于 Object TeX 引擎扩展为您自己的自定义格式创建转换选项。
  3. 如果主输入不作为流提供,请指定输入工作目录。使用 InputFileSystemDirectory 类执行此操作。
  4. 使用 OutputFileSystemDirectory 类为输出指定文件系统工作目录。
  5. 使用 XpsDevice 实例创建一个 TeXJob 类对象,并通过调用 Run() 方法运行作业。在这里,我们还展示了如何将主 TeX 输入文件作为流传递。

C# 代码示例:以自定义格式排版 TeX 文件

using Aspose.TeX.IO;
using Aspose.TeX.Presentation.Xps;
using Aspose.TeX.ResourceProviders;
using System.IO;
using System.Text;
// Create the format provider using the file system input working directory.
// We use the project output directory as our custom format file is supposed to be located there.
using (FormatProvider formatProvider =
    new FormatProvider(new InputFileSystemDirectory(RunExamples.OutputDirectory), "customtex"))
{
    // Create conversion options for a custom format upon ObjectTeX engine extension.
    TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectTeX(formatProvider));
    options.JobName = "typeset-with-custom-format";
    // Specify the input working directory. This is not required here as we are providing the main input as a stream.
    // But it is required when the main input has dependencies (e.g. images).
    options.InputWorkingDirectory = new InputFileSystemDirectory(RunExamples.InputDirectory);
    // Specify a file system working directory for the output.
    options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);

    // Run the job.
    new TeXJob(new MemoryStream(Encoding.ASCII.GetBytes(
            "Congratulations! You have successfully typeset this text with your own TeX format!\\end")),
            new XpsDevice(), options).Run();
}



常问问题

1. 什么是 LaTeX 模板?

模板是一组已保存的属性、设置或结构,可应用于新创建的文档以节省时间。对于 LaTeX,这可以是包含有关页面格式、字体、文档类、包含的包等信息的文件。

2. 如何使用 LaTeX 模板?

如果您没有使用任何其他软件并且模板只是一个 TeX 文件,只需打开它并开始工作。请记住,您不会直接将更改保存在本文档中,而是使用“另存为”选项。

3. 如何设置LaTeX文档的类型?

要指定 LaTeX 中的文档类型(例如文章、书籍或演示文稿),请使用命令“\documentclass{…}”以及大括号内受支持的类名称之一。

4. 如何创建自定义 TeX 格式?

要使用 Aspose.TeX API 解决方案创建自定义 TeX 格式,您首先需要创建适当的 TeX 引擎选项。然后为输入和输出指定文件系统工作目录。最后,通过调用 TeXJob.CreateFormat() 方法创建格式。

TeX What is TeX File Format

TeX 是一种排版系统和编程语言,用于创建高质量文档,尤其是数学公式。TeX 源文件为纯文本,由 TeX 引擎(如 pdfTeX、XeTeX、LuaTeX)处理,可生成 PDF、DVI 等输出。