其他TeX转换输出格式 | Java
您目前不太可能要转换以除LaTeX以外的任何其他格式编写的TEX文件。但是,如果您出于某种原因正在学习Tex语言和/或内部语言,这是可能的。无论如何,用于 Java 的 Aspose.TeX允许您转换以简单的Tex格式编写的文件。它还使您可以创建以这些格式设计的自定义格式和排版文档。
我们将从创建自定义格式开始。
创建自定义格式
让我们记住,格式文件是Tex发动机内部状态的二进制文件。
将 inputDirectory 设置为包含 customtex.tex 及其依赖文件的文件夹,将 outputDirectory 设置为现有的输出文件夹。格式源文件必须以 \dump 结束;此示例在输出文件夹中创建 customtex.fmt。导入示例中使用的 Aspose.TeX 类,以及排版所需的 com.aspose.tex.rendering.XpsDevice、java.io.ByteArrayInputStream 和 java.nio.charset.StandardCharsets。调用方法必须处理或声明异常。按照
许可指南应用许可证,以避免评估版限制。
1// Create a custom TeX format from customtex.tex, which ends with \dump.
2// inputDirectory contains customtex.tex; outputDirectory is an existing output folder.
3TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectIniTeX());
4InputFileSystemDirectory input = new InputFileSystemDirectory(inputDirectory);
5OutputFileSystemDirectory output = new OutputFileSystemDirectory(outputDirectory);
6options.setInputWorkingDirectory(input);
7options.setOutputWorkingDirectory(output);
8options.setInteraction(Interaction.NonstopMode);
9options.setTerminalOut(new OutputMemoryTerminal());
10try {
11 TeXJob.createFormat("customtex", options);
12} finally {
13 output.close();
14 input.close();
15}如您所见,该代码类似于用于转换Tex文件的代码。但是有一些差异。
首先,在这里,我们使用 TexConfig.ObigntInitex() 作业配置。这是一种特殊的配置,它使引擎的状态“处女”,即内部参数具有其默认值,并且控制序列的集合与原始序列相吻合。在我们的示例中,从提到的意义上提到的 此处的含义扩展了一组原语。
接下来,我们照常设置输入和输出工作目录。输入工作目录需要包含主要格式源文件及其所有依赖项。
第二个关键区别是我们运行工作的方式。这次,我们使用静态 createformat() 方法,该方法与选项一起使用主源文件的名称,该名称必须与格式名称相同。
以您的自定义格式排版TEX文件
现在,我们已经拥有自己的TEX格式,我们想在以这种格式写下Tex文件。这是代码:
将 formatDirectory 设置为包含已创建的 customtex.fmt 的文件夹,将 outputDirectory 设置为现有的输出文件夹。此示例将独立的文本流排版为 typeset-with-custom-format.xps,因此无需输入工作目录。如果主输入有图像等依赖文件,请设置输入工作目录。
1// Typeset text using the previously created customtex.fmt format.
2// formatDirectory contains customtex.fmt; outputDirectory is an existing output folder.
3InputFileSystemDirectory formatInput = new InputFileSystemDirectory(formatDirectory);
4FormatProvider formatProvider = new FormatProvider(formatInput, "customtex");
5OutputFileSystemDirectory output = new OutputFileSystemDirectory(outputDirectory);
6XpsDevice device = new XpsDevice();
7TeXJobResult result;
8try {
9 TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectTeX(formatProvider));
10 options.setJobName("typeset-with-custom-format");
11 options.setOutputWorkingDirectory(output);
12 options.setInteraction(Interaction.NonstopMode);
13 options.setTerminalOut(new OutputMemoryTerminal());
14 // No input working directory is needed for this self-contained stream.
15 try (ByteArrayInputStream text = new ByteArrayInputStream(
16 "Congratulations! You have successfully typeset this text with your own TeX format!\\end"
17 .getBytes(StandardCharsets.US_ASCII))) {
18 result = new TeXJob(text, device, options).run();
19 }
20} finally {
21 device.dispose();
22 output.close();
23 formatProvider.close();
24 formatInput.close();
25}显然,我们必须以某种方式指定格式。首先,我们需要创建一个 formatprovider 类的实例。然后,在选项构造函数中,我们使用 TeXConfig.objectTeX(FormatProvider)配置,该配置将我们的格式提供程序作为参数,并将格式加载到引擎的“处女”状态之上。
两个示例均以非交互方式运行,并将终端输出保存在内存中。排版示例在作业完成后关闭输入流,释放 XPS 设备,并关闭工作目录和格式提供程序。这些功能已在此 指南中介绍。
以普通Tex格式排版Tex文件
如果我们使用不带格式提供程序的 TeXConfig.objectTeX(),则引擎将加载默认格式,即
Object Tex在其第四种意义上。因此,如果您有一个以普通Tex格式编写的TEX文件,则可以将其转换为任何受支持的目标格式。