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. Do I need MiKTeX, TeX Live, or external executable binaries installed?

No. Aspose.TeX for Java is a completely standalone Java engine that executes TeX/LaTeX parsing, typesetting, and rendering natively without requiring external native executables.

2. Can I mix different input sources for main files and external graphics/styles?

Yes. You can set a primary InputWorkingDirectory (e.g., InputZipDirectory) and configure auxiliary package/font search paths using the TeXOptions configuration settings.

3. How does Aspose.TeX handle relative paths in \input{} or \includegraphics{} macros?

Relative paths defined within LaTeX documents are automatically resolved relative to the root specified by the active IInputWorkingDirectory instance.