Flexible TeX inputs and working directories in Java

Configure input sources and virtual file system working directories for TeX and LaTeX document compilation using Aspose.TeX for Java. Pass source TeX files, packages, macros, and embedded images directly from local file systems, compressed ZIP archives, or custom in-memory streams without requiring desktop TeX distribution installations (like MiKTeX or TeX Live).

 

When compiling TeX or LaTeX documents, the engine needs access to main source files (.tex), referenced sub-files (\input{} or \include), styles (.sty), classes (.cls), fonts, and images.

Aspose.TeX for Java separates TeX input sources into abstracted working directories represented by the IInputWorkingDirectory interface:

  • Input working directory Local file system uses InputFileSystemDirectory to read TeX source files and assets directly from disk folders.
  • Input working derictory ZIP archive uses InputZipDirectory to read encapsulated TeX compilation where all dependencies (.tex, .sty, images) are stored in a single .zip file.
  • Input working directory Custom stream/memory uses IInputWorkingDirector to read virtualized file systems, database BLOB inputs, or cloud storage streams (S3/Azure Blob)."

Load TeX input from a local file system directory

Specify a physical disk folder as the input working directory for the TeX engine using InputFileSystemDirectory.

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

public class TeXInputFromFileSystem {
    public static void main(String[] args) throws IOException {
        // Create TeX options for LaTeX format using ObjectTeX engine
        TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());

        // Set the input working directory to a local file folder
        String inputDirPath = "C:/TeXProject/Input/";
        options.setInputWorkingDirectory(new InputFileSystemDirectory(inputDirPath));

        // Set output working directory for compiled results (PDF/XPS/Images)
        String outputDirPath = "C:/TeXProject/Output/";
        options.setOutputWorkingDirectory(new OutputFileSystemDirectory(outputDirPath));

        // Run TeX compilation for 'main.tex'
        TeXJob job = new TeXJob("main", new PdfDevice(), options);
        job.run();

        System.out.println("TeX compilation from local directory completed successfully.");
    }
}

Load TeX input from a ZIP archive

Compile self-contained TeX packages directly from a .zip file without unpacking them to disk using InputZipDirectory.

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

public class TeXInputFromZipArchive {
    public static void main(String[] args) throws IOException {
        // Initialize LaTeX conversion options
        TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());

        // Open input ZIP stream containing main.tex, packages, and assets
        FileInputStream zipStream = new FileInputStream("C:/TeXProject/ProjectBundle.zip");

        // Set input working directory to the ZIP archive stream
        options.setInputWorkingDirectory(new InputZipDirectory(zipStream, "subfolder_inside_zip"));

        // Define output directory on disk
        options.setOutputWorkingDirectory(new OutputFileSystemDirectory("C:/TeXProject/Output/"));

        // Execute TeX job referencing 'main.tex' inside the ZIP
        TeXJob job = new TeXJob("main", new PdfDevice(), options);
        job.run();

        zipStream.close();
        System.out.println("TeX compilation directly from ZIP archive completed.");
    }
}

Pass TeX source via memory stream

For dynamic LaTeX generation (such as server-rendered reports or dynamic equation rendering), pass raw string content or streams directly to TeXJob.

import com.aspose.tex.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class TeXInputFromMemoryStream {
    public static void main(String[] args) throws Exception {
        // Create dynamic LaTeX content string
        String latexCode = "\\documentclass{article}\n" +
                           "\\begin{document}\n" +
                           "\\section{Dynamic Report}\n" +
                           "Hello World from Aspose.TeX for Java!\n" +
                           "\\end{document}";

        InputStream inputSteam = new ByteArrayInputStream(latexCode.getBytes(StandardCharsets.UTF_8));

        // Configure options
        TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
        options.setOutputWorkingDirectory(new OutputFileSystemDirectory("C:/TeXProject/Output/"));

        // Run job passing stream directly
        TeXJob job = new TeXJob(inputSteam, new PdfDevice(), options);
        job.run();

        System.out.println("Dynamic LaTeX stream successfully compiled to PDF.");
    }
}

Installation and Setup

Integrate Aspose.TeX for Java into your project using 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. MiKTeX、TeX Live、または外部実行可能バイナリをインストールする必要がありますか?

いいえ。Aspose.TeX for Javaは完全にスタンドアロンのJavaエンジンであり、外部のネイティブ実行可能ファイルを必要とせずに、TeX/LaTeXの解析、組版、レンダリングをネイティブに実行します。

2. メインファイルと外部グラフィック/スタイルに異なる入力ソースを混在させることはできますか?

はい。プライマリInputWorkingDirectory(例:InputZipDirectory)を設定し、TeXOptions構成設定を使用して補助的なパッケージ/フォント検索パスを構成できます。

3. Aspose.TeXは\input{}または\includegraphics{}マクロ内の相対パスをどのように処理しますか?

LaTeXドキュメント内で定義された相対パスは、アクティブなIInputWorkingDirectoryインスタンスで指定されたルートを基準として自動的に解決されます。