Embedding graphics in LaTeX documents using Java

Incorporate high-resolution raster images (PNG, JPEG, BMP) and vector figures (EPS, PDF) into LaTeX documents using Aspose.TeX for Java. By utilizing native LaTeX packages like graphicx and configuring input working directories, developers can seamlessly compile documents with embedded charts, figures, photos, and vector graphics into publication-ready PDF, XPS, and image outputs.

 

When compiling LaTeX documents containing graphic elements, Aspose.TeX for Java resolves referenced image files relative to the configured IInputWorkingDirectory.

  • For photos, raster screenshots, and web graphics choose PNG/JPEG formats embedded using \usepackage{graphicx} - \includegraphics{figure.png} commands.
  • For high-precision vector diagrams and CAD/scientific plots, choose the EPS format embedded using \usepackage{graphicx} - \includegraphics{diagram.eps} commands.
  • For legacy bitmap image embedding., choose the BMP/TIFF format embedded using \usepackage{graphicx} - \includegraphics{image.bmp} commands.

Embedding raster images (PNG/JPEG) in LaTeX

To embed standard raster images, include the graphicx package in your LaTeX preamble and specify the target image filename in \includegraphics{}.

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

public class EmbedRasterGraphicsInLaTeX {
    public static void main(String[] args) throws Exception {
        // Create LaTeX document string referencing an image file 'sample.png'
        String latexCode = "\\documentclass{article}\n" +
                           "\\usepackage{graphicx}\n" +
                           "\\begin{document}\n" +
                           "\\section{Graphics Rendering Demo}\n" +
                           "Here is an embedded raster image:\n\n" +
                           "\\begin{figure}[h]\n" +
                           "  \\centering\n" +
                           "  \\includegraphics[width=0.5\\textwidth]{sample.png}\n" +
                           "  \\caption{Sample PNG Image in LaTeX}\n" +
                           "\\end{figure}\n" +
                           "\\end{document}";

        ByteArrayInputStream inputStream = new ByteArrayInputStream(latexCode.getBytes(StandardCharsets.UTF_8));

        // Initialize conversion options for ObjectLaTeX
        TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());

        // Set input working directory where 'sample.png' is located
        options.setInputWorkingDirectory(new InputFileSystemDirectory("C:/TeXProject/Images/"));

        // Set output working directory for compiled PDF
        options.setOutputWorkingDirectory(new OutputFileSystemDirectory("C:/TeXProject/Output/"));

        // Execute TeX job
        TeXJob job = new TeXJob(inputStream, new PdfDevice(), options);
        job.run();

        System.out.println("LaTeX document with embedded PNG successfully compiled to PDF.");
    }
}

Embedding vector graphics (EPS) in LaTeX

Aspose.TeX natively processes Encapsulated PostScript (.eps) graphics when compiling through the ObjectLaTeX format engine.

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

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

        // Configure directory paths containing .tex source and .eps graphics
        options.setInputWorkingDirectory(new InputFileSystemDirectory("C:/TeXProject/SourceWithEPS/"));
        options.setOutputWorkingDirectory(new OutputFileSystemDirectory("C:/TeXProject/Output/"));

        // Run job for 'document_with_eps.tex' containing \includegraphics{vector_chart.eps}
        TeXJob job = new TeXJob("document_with_eps", new PdfDevice(), options);
        job.run();

        System.out.println("LaTeX document with vector EPS figure compiled successfully.");
    }
}

Loading graphic assets from ZIP archives

Package TeX source files, macro packages, and dynamic images together in a .zip archive, and instruct Aspose.TeX to resolve graphic dependencies directly from memory or compressed storage.

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

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

        // Open input ZIP archive containing 'main.tex' and 'figure.png'
        FileInputStream zipStream = new FileInputStream("C:/TeXProject/ProjectWithAssets.zip");
        options.setInputWorkingDirectory(new InputZipDirectory(zipStream, "src"));

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

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

        zipStream.close();
        System.out.println("Compiled LaTeX document with images embedded directly from ZIP file.");
    }
}

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. What happens if I do not specify a job name in TeXOptions?

If a job name is not explicitly configured via setJobName(), Aspose.TeX defaults to the main input filename (e.g., main.tex yields main.pdf and main.log).

2. How do I completely disable compilation .log file creation?

Set options.setLogOutput(false) before creating and running TeXJob. This prevents the engine from generating auxiliary log files on disk or in the output working directory.

3. Can I capture TeX terminal messages in server/headless environments?

Yes. Assigning a custom OutputMemoryTerminal or implementing IOutputTerminal allows you to route all terminal stdout/stderr messages into your server’s enterprise logging system (e.g., Log4j or SLF4J).