Kết xuất hình LaTeX độc lập và công thức toán học trong Java

Kết xuất các phương trình toán học LaTeX độc lập, công thức hóa học và hình cấu trúc trực tiếp sang các định dạng hình ảnh chất lượng cao (PNG, SVG, JPEG, BMP) hoặc đầu ra vector bằng Aspose.TeX for Java. Chuyển đổi các đoạn công thức như \sum_{i=1}^{n} i = \frac{n(n+1)}{2} thành hình ảnh độc lập mà không cần biên dịch các tài liệu \documentclass{article} hoàn chỉnh hoặc ghi các tệp .tex tạm thời vào đĩa.

 

Aspose.TeX for Java cung cấp các công cụ kết xuất chuyên dụng (MathRendererFigureRenderer) được thiết kế riêng để kết xuất vi mô các biểu thức toán học và khối đồ họa:

  • Đối với ứng dụng, màn hình di động, nền tảng LMS (Canvas, Moodle) và lưu trữ cơ sở dữ liệu, hãy chọn định dạng PNG được kết xuất bằng lớp PngMathRenderer.
  • Đối với màn hình DPI cao, trang web đáp ứng, các phần tử giao diện người dùng có thể mở rộng và xuất bản sẵn sàng in, hãy chọn định dạng SVG được kết xuất bằng lớp SvgMathRenderer.
  • Đối với đồ họa LaTeX độc lập, hình TikZ hoặc các khối dạng bảng được kết xuất trực tiếp dưới dạng hình ảnh, hãy chọn định dạng PNG được kết xuất bằng lớp PngFigureRenderer.
  • Đối với các hình vector và sơ đồ không phụ thuộc vào độ phân giải được kết xuất trực tiếp từ các đoạn LaTeX, hãy chọn định dạng đầu ra SVG được kết xuất bằng lớp SvgFigureRenderer.

Kết xuất công thức toán học LaTeX thành hình ảnh PNG

Sử dụng PngMathRenderer để chuyển đổi các đoạn toán học LaTeX thô thành hình ảnh PNG có nền trong suốt hoặc nền đồng nhất chỉ với vài dòng mã.

import com.aspose.tex.*;
import com.aspose.tex.mathrenderer.*;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class RenderLaTeXFormulaToPng {
    public static void main(String[] args) throws Exception {
        // Step 1: Initialize PNG Math Renderer Options
        PngMathRendererOptions options = new PngMathRendererOptions();
        options.setResolution(300); // Set DPI resolution (300 DPI for high quality)
        options.setTextColor(java.awt.Color.BLACK); // Formula text color
        options.setBackgroundColor(java.awt.Color.WHITE); // Background color
        options.setLogOutput(new OutputConsoleTerminal());

        // Define LaTeX math snippet
        String mathFormula = "\\int_{0}^{\\infty} e^{-x^2} dx = \\frac{\\sqrt{\\pi}}{2}";

        // Define output stream for the generated PNG
        OutputStream stream = new FileOutputStream("C:/TeXProject/Output/formula.png");

        // Execute rendering job
        PngMathRenderer renderer = new PngMathRenderer();
        MathRendererResult result = renderer.render(mathFormula, stream, options, null);

        stream.close();
        System.out.println("LaTeX math formula rendered to PNG successfully.");
    }
}

Kết xuất công thức toán học LaTeX thành SVG vector

Tạo đồ họa vector có thể mở rộng (.svg) từ các biểu thức toán học LaTeX để kết xuất web đáp ứng bằng SvgMathRenderer.

import com.aspose.tex.*;
import com.aspose.tex.mathrenderer.*;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class RenderLaTeXFormulaToSvg {
    public static void main(String[] args) throws Exception {
        // Create SVG Renderer Options
        SvgMathRendererOptions options = new SvgMathRendererOptions();
        options.setTextColor(java.awt.Color.BLUE); // Custom formula color

        // Define quadratic formula snippet
        String mathFormula = "x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}";

        // Stream output to SVG file
        OutputStream stream = new FileOutputStream("C:/TeXProject/Output/quadratic.svg");

        // Perform rendering
        SvgMathRenderer renderer = new SvgMathRenderer();
        renderer.render(mathFormula, stream, options, null);

        stream.close();
        System.out.println("LaTeX math formula rendered to SVG successfully.");
    }
}

Kết xuất các hình và bảng LaTeX độc lập

Kết xuất các khối LaTeX riêng biệt chứa bảng, sơ đồ hoặc môi trường đặc biệt bằng PngFigureRenderer.

import com.aspose.tex.*;
import com.aspose.tex.figurerenderer.*;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class RenderLaTeXFigureToPng {
    public static void main(String[] args) throws Exception {
        // Configure Figure Renderer Options
        PngFigureRendererOptions options = new PngFigureRendererOptions();
        options.setResolution(150);

        // Create standalone figure or matrix snippet
        String figureText = "\\begin{matrix} a & b \\\\ c & d \\end{matrix}";

        // Render snippet directly to output image stream
        OutputStream stream = new FileOutputStream("C:/TeXProject/Output/matrix.png");

        PngFigureRenderer renderer = new PngFigureRenderer();
        renderer.render(figureText, stream, options, null);

        stream.close();
        System.out.println("LaTeX matrix figure rendered to image successfully.");
    }
}

Cài đặt và thiết lập

Tích hợp Aspose.TeX for Java vào dự án của bạn bằng 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. Điều gì xảy ra nếu tôi không chỉ định tên công việc trong TeXOptions?

Nếu tên công việc không được cấu hình rõ ràng thông qua setJobName(), Aspose.TeX sẽ mặc định sử dụng tên tệp đầu vào chính (ví dụ: main.tex tạo ra main.pdfmain.log).

2. Làm thế nào để tắt hoàn toàn việc tạo tệp biên dịch .log?

Đặt options.setLogOutput(false) trước khi tạo và chạy TeXJob. Điều này ngăn công cụ tạo các tệp nhật ký phụ trợ trên đĩa hoặc trong thư mục làm việc đầu ra.

3. Tôi có thể thu thập các thông báo terminal TeX trong môi trường máy chủ/headless không?

Có. Việc gán một OutputMemoryTerminal tùy chỉnh hoặc triển khai IOutputTerminal cho phép bạn chuyển hướng tất cả thông báo stdout/stderr của terminal vào hệ thống ghi nhật ký doanh nghiệp của máy chủ (ví dụ: Log4j hoặc SLF4J).