
Hello fellow Java programmers! This is another exciting blog post on how to write math equations in LaTeX programmatically. We will install Aspose.TeX for Java and implement the functionality to programmatically write LaTeX mathematical expressions and save them in PNG image format. This LaTeX to image API provides LaTeX rendering options as well as the conversion to other popular file formats. In addition, it automates the whole process to render LaTeX math equations smoothly. So, let’s get started and see the implementation of rendering math in LaTeX/TeX to PNG in Java.
The following points will be discussed in this blog post:
LaTeX 到圖片 API 安裝
Aspose.TeX for Java offers classes and methods to create math equations with LaTeX, turn different content into raster images, and more. However, the installation process of this enterprise-level library is as simple as it could be. Moreover, please have the Java installed in your local machine So, download this JAR file or use the following Maven configurations to install this LaTeX 到圖片 API in your local environment:
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose<https://releases.aspose.com/tex/java//groupId>
<artifactId>aspose-tex</artifactId>
<version>22.6</version>
</dependency>
</dependencies>
Please visit this installation guide to learn about complete details.

在 Java 中將 LaTeX 數學表達式渲染為 PNG
Now, we will write a code snippet in Java and use the classes and methods to render LaTeX mathematical expressions as PNG image programmatically.
You can follow the steps to 在 Java 中編寫 LaTeX 數學方程:
- Create an instance of the MathRendererOptions class and assign an object of the PngMathRendererOptions class.
- Specify the preamble by calling the setPreamble method.
- Specify the foreground color by invoking the setTextColor method.
- Call the setScale method to define the scaling factor 300%.
- Invoke the setBackgroundColor method to set the background color.
- The setLogStream method will used to define the output stream for the log file.
- Call the showTerminal method to specify whether to show the terminal output on the console or not.
- Instantiate an instance of the Size2D.Float class in which the dimensions of the resulting image will be written.
- Create the output stream for the formula image by initializing an object of the FileOutputStream class.
- Call the render method to fire up the rendering.
The following code sample demonstrates how to 以程式方式編寫 LaTeX 數學表達式:
package com.example; | |
import java.awt.Color; | |
import java.io.ByteArrayOutputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import com.aspose.tex.MathRendererOptions; | |
import com.aspose.tex.PngMathRenderer; | |
import com.aspose.tex.PngMathRendererOptions; | |
import com.aspose.tex.Size2D; | |
public class Main { | |
// Latex Mathematical Expressions using Latex to Image API | |
public static void main(String[] args) throws IOException { | |
// Create an instance of the MathRendererOptions class and assign an object of the PngMathRendererOptions class. | |
MathRendererOptions options = new PngMathRendererOptions(); | |
// Specify the preamble by calling the setPreamble method. | |
options.setPreamble("\\usepackage{amsmath}\r\n" | |
+ "\\usepackage{amsfonts}\r\n" | |
+ "\\usepackage{amssymb}\r\n" | |
+ "\\usepackage{color}"); | |
// Specify the foreground color by invoking the setTextColor method. | |
options.setTextColor(Color.BLACK); | |
// Call the setScale method to define the scaling factor 300%. | |
options.setScale(3000); | |
// Invoke the setBackgroundColor method to set the background color. | |
options.setBackgroundColor(Color.YELLOW); | |
// The setLogStream method will used to define the output stream for the log file. | |
options.setLogStream(new ByteArrayOutputStream()); | |
// Call the showTerminal method to specify whether to show the terminal output on the console or not. | |
options.showTerminal(true); | |
// Instantiate an instance of the Size2D.Float class in which the dimensions of the resulting image will be written. | |
Size2D size = new Size2D.Float(); | |
// Create the output stream for the formula image by initializing an object of the FileOutputStream class. | |
final OutputStream stream = new FileOutputStream("/math-formula.png"); | |
try { | |
// Call the render method to fire up the rendering. | |
new PngMathRenderer().render("\\begin{equation*}\r\n" + | |
"e^x = x^{\\color{red}0} + x^{\\color{red}1} " | |
+ "\\frac{x^{\\color{red}2}}{2} " | |
+ "\\frac{x^{\\color{red}3}}{6} " | |
+ "\\cdots = \\sum_{n\\geq 0} " | |
+ "\\frac{x^{\\color{red}n}}{n!}\r\n" | |
+ "\\end{equation*}", stream, options, size); | |
} finally { | |
if (stream != null) | |
stream.close(); | |
} | |
} | |
} |
You can see the output in the image below:

LaTeX 數學方程生成器 - 在線應用
In addition to the LaTeX 到圖片 API, there is a LaTeX 數學方程生成器 工具 to create math in LaTeX online. It is also powered by Aspose.TeX and offers a huge stack of features. It is super simple to use, requires no installation as it is web-based and you can open it in any web browser. Further, there are various options on the user interface that you can use to create LaTeX 數學方程。

LaTeX 中的數學 - 獲取免費許可
You can get a free temporary license to try the LaTeX to image API without evaluation limitations.
結論
We are ending this blog post here. We have gone through how to write LaTeX 數學表達式 and save them in a PNG file format. In addition, we went through the implementation of how to 在 LaTeX 中編寫數學方程 using this LaTeX 到圖片 API. Moreover, you can visit the documentation, API references, and the GitHub repo to learn the other features. This Getting Started guide is the point where you can start the development. Finally, aspose.com is writing new blog posts. So, please stay in touch for regular updates.
幫助隨時可用
You can always let us know about your questions on our forum.
常見問題解答 – FAQs
如何在 LaTeX 中編寫數學公式?
Aspose.TeX for Java provides classes and methods to 以程式方式在 Java 中編寫 LaTeX 的數學。 So, please visit this link to see the code snippet on how to write math equations in LaTeX.
如何在線編寫 LaTeX 方程?
This LaTeX 數學方程 生成器 enables you to 在線編寫 LaTeX 數學方程。 Further, it is free and does not require any fee or subscription.