在 Java 中使用自定义 TeX 格式

使用 Aspose.TeX for Java 创建、加载和执行自定义 TeX 格式文件 (.fmt),扩展文档编译功能。除了标准的预配置设置(例如 Plain TeX 或 ObjectLaTeX)之外,Aspose.TeX 还允许开发人员创建包含预加载宏包的自定义格式转储文件,从而显著加快重复排版任务的速度,并支持专用的 TeX 方言。

 

在 TeX 生态系统中,格式文件 (.fmt) 是加载特定宏包和命令定义(例如 LaTeX 或 AMS-TeX)后 TeX 内存状态的二进制转储。使用格式文件可以让 TeX 引擎在每次执行时跳过解析大型宏库,从而大幅提升文档编译速度。

Aspose.TeX for Java 同时支持内置格式和通过编程方式创建的用户定义自定义格式。支持的输出目录类型:

  • 引擎 ObjectLaTeX 是一种标准 LaTeX 格式,基于 ObjectTeX 引擎扩展,用于常规 LaTeX 文档编译(文章、报告、书籍)。
  • 引擎 ObjectTeX 是一种扩展了 ObjectTeX 功能的 TeX 格式,用于轻量级、无宏的 TeX 排版。
  • 引擎 Custom Format (.fmt) 是由开发人员创建的二进制格式,其中包含预编译的宏/宏包,用于使用预加载的自定义宏集进行高性能批量编译。

创建自定义 TeX 格式文件 (.fmt)

使用 TeXFormatEngine.tex 初始化文件(包含 \font\catcode\dump 等宏定义)生成自定义格式转储文件。

import com.aspose.tex.*;
import java.io.IOException;

public class CreateCustomTeXFormat {
    public static void main(String[] args) throws IOException {
        // Set up options for creating a custom format using ObjectTeX
        TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectEngineOptions());

        // Define input and output working directories
        options.setInputWorkingDirectory(new InputFileSystemDirectory("C:/TeXProject/CustomMacros/"));
        options.setOutputWorkingDirectory(new OutputFileSystemDirectory("C:/TeXProject/Formats/"));

        // Run the format generation job on the custom macro source file (e.g., 'myformat.tex')
        // The source file must end with the \dump command
        TeXJob job = new TeXJob("myformat", new PdfDevice(), options);
        job.run();

        System.out.println("Custom TeX format created successfully.");
    }
}

使用自定义 TeX 格式编译文档

加载并执行现有的或新生成的 .fmt 文件,以处理输入的 TeX 文档。

import com.aspose.tex.*;
import java.io.IOException;

public class RunWithCustomTeXFormat {
    public static void main(String[] args) throws IOException {
        // Define working directory where the custom format file resides
        IInputWorkingDirectory formatDir = new InputFileSystemDirectory("C:/TeXProject/Formats/");

        // Initialize options using the custom format name ('myformat') and location
        TeXOptions options = TeXOptions.consoleAppOptions(
            TeXConfig.customFormat("myformat", formatDir)
        );

        // Set input/output directories for the target TeX document
        options.setInputWorkingDirectory(new InputFileSystemDirectory("C:/TeXProject/Input/"));
        options.setOutputWorkingDirectory(new OutputFileSystemDirectory("C:/TeXProject/Output/"));

        // Compile the document using the pre-loaded custom format
        TeXJob job = new TeXJob("document", new PdfDevice(), options);
        job.run();

        System.out.println("Document compiled using custom TeX format successfully.");
    }
}

使用内置引擎和格式

对于标准文档处理,请使用 TeXConfig 提供的内置配置。

import com.aspose.tex.*;

public class BuiltInTeXFormats {
    public static void main(String[] args) throws Exception {
        // Option A: Standard ObjectLaTeX (LaTeX document class support)
        TeXOptions latexOptions = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());

        // Option B: Plain ObjectTeX (Plain TeX primitives)
        TeXOptions plainTexOptions = TeXOptions.consoleAppOptions(TeXConfig.objectTeX());

        System.out.println("Built-in TeX format options initialized.");
    }
}

安装和设置

使用 Maven 将 Aspose.TeX for Java 集成到您的项目中:

Package Manager Console Command

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-tex</artifactId>
    <version>Latest</version>
</dependency>

FAQ

1. 使用自定义格式文件(.fmt)有什么优势?

创建自定义格式会将宏包预加载到二进制格式中。这样可以消除每次运行时的解析开销,从而显著加快高负载文档渲染流程。

2. 源文件中必须包含什么命令才能创建 .fmt 文件?

源 TeX 文件必须以原生 \dump TeX 命令结尾,该命令会通知引擎创建二进制格式快照。

3. 可以加载存储在 ZIP 存档中的格式文件吗?

可以。将 InputZipDirectory 实例作为 TeXConfig.customFormat() 中的格式提供程序目录参数传入。