Standalone LaTeX figure and math formula rendering in Java
Render standalone LaTeX math equations, chemical formulas, and structural figures directly into high-fidelity image formats (PNG, SVG, JPEG, BMP) or vector outputs using Aspose.TeX for Java. Convert snippet formulas like \sum_{i=1}^{n} i = \frac{n(n+1)}{2} into standalone images without compiling full \documentclass{article} documents or writing temporary .tex files to disk.
Aspose.TeX for Java provides dedicated rendering engines (MathRenderer and FigureRenderer) tailored for micro-renderings of math expressions and graphic blocks:
- For applications, mobile screens, LMS platforms (Canvas, Moodle), and database storage choose the output PNG format rendered with
PngMathRendererclass. - For high-DPI displays, responsive web pages, scalable UI elements, and print-ready publishing choose the SVG format rendered with
SvgMathRendererclass. - For standalone LaTeX graphics, TikZ figures, or tabular blocks rendered directly as images choose the output PNG format rendered with
PngFigureRendererclass. - For the resolution-independent vector figures and diagrams rendered directly from LaTeX snippets choose the SVG output format rendered with
SvgFigureRendererclass.
Render LaTeX math formula to PNG image
Use PngMathRenderer to convert raw LaTeX math snippets into transparent or solid-background PNG images in a few lines of code.
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.");
}
}Render LaTeX math formula to vector SVG
Generate scalable vector graphics (.svg) from LaTeX math expressions for responsive web rendering using 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.");
}
}Render standalone LaTeX figures and tables
Render isolated LaTeX blocks containing tables, diagrams, or special environments using 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.");
}
}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).