การทำงานกับรูปแบบ TeX แบบกำหนดเองใน Java
ขยายความสามารถในการคอมไพล์เอกสารด้วยการสร้าง โหลด และเรียกใช้ไฟล์รูปแบบ TeX แบบกำหนดเอง (.fmt) โดยใช้ Aspose.TeX for Java นอกเหนือจากการกำหนดค่ามาตรฐานที่กำหนดค่าไว้ล่วงหน้า (เช่น Plain TeX หรือ ObjectLaTeX) แล้ว Aspose.TeX ยังช่วยให้นักพัฒนาสามารถสร้างดัมป์รูปแบบแบบกำหนดเองที่มีแพ็กเกจมาโครที่โหลดไว้ล่วงหน้า เพื่อเร่งงานจัดเรียงตัวอักษรที่ต้องทำซ้ำให้เร็วขึ้นอย่างมาก และรองรับไดอะเล็กต์ TeX เฉพาะทาง
ในระบบนิเวศของ TeX ไฟล์รูปแบบ (.fmt) คือดัมป์ไบนารีของสถานะหน่วยความจำของ TeX หลังจากโหลดชุดแพ็กเกจมาโครและคำจำกัดความคำสั่งที่กำหนดไว้ (เช่น LaTeX หรือ AMS-TeX) การใช้ไฟล์รูปแบบช่วยให้เอนจิน TeX ข้ามขั้นตอนการวิเคราะห์ไลบรารีมาโครขนาดใหญ่ในทุกครั้งที่ทำงาน ซึ่งช่วยเร่งการคอมไพล์เอกสารได้อย่างมาก
Aspose.TeX for Java รองรับทั้งรูปแบบในตัวและรูปแบบแบบกำหนดเองที่ผู้ใช้กำหนดและสร้างขึ้นโดยทางโปรแกรม ประเภทไดเรกทอรีเอาต์พุตที่รองรับ:
- เอนจิน ObjectLaTeX เป็นรูปแบบ LaTeX มาตรฐานที่อ้างอิงจากส่วนขยายของเอนจิน ObjectTeX และใช้สำหรับการคอมไพล์เอกสาร LaTeX ทั่วไป (บทความ รายงาน และหนังสือ)
- เอนจิน ObjectTeX เป็นรูปแบบ TeX ที่ขยายด้วยคุณสมบัติของ
ObjectTeXและใช้สำหรับการจัดเรียงตัวอักษร TeX ที่มีน้ำหนักเบาและไม่มีมาโคร - เอนจิน Custom Format (.fmt) เป็นรูปแบบไบนารีที่สร้างโดยนักพัฒนา ซึ่งมีมาโคร/แพ็กเกจที่คอมไพล์ไว้ล่วงหน้า และใช้สำหรับการคอมไพล์แบบแบตช์ประสิทธิภาพสูงด้วยชุดมาโครแบบกำหนดเองที่โหลดไว้ล่วงหน้า
การสร้างไฟล์รูปแบบ TeX แบบกำหนดเอง (.fmt)
สร้างดัมป์รูปแบบแบบกำหนดเองจากไฟล์เริ่มต้น .tex (ที่มีคำจำกัดความมาโคร เช่น \font, \catcode หรือ \dump) โดยใช้ TeXFormatEngine
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.");
}
}การติดตั้งและการตั้งค่า
ผสานรวม Aspose.TeX for Java เข้ากับโปรเจกต์ของคุณโดยใช้ Maven:
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 ต้นฉบับต้องลงท้ายด้วยคำสั่ง TeX แบบ native \dump ซึ่งเป็นการส่งสัญญาณให้เอนจินสร้างสแนปช็อตของรูปแบบไบนารี
3. สามารถโหลดไฟล์รูปแบบที่จัดเก็บไว้ภายในไฟล์ ZIP ได้หรือไม่?
ได้ ส่งอินสแตนซ์ของ InputZipDirectory เป็นพารามิเตอร์ไดเรกทอรีของตัวให้บริการรูปแบบใน TeXConfig.customFormat()