LaTeX数学公式渲染 | Aspose.TeX for Java
如何将 LaTeX 数学公式渲染为 PNG
事实上,演示 LaTeX数学公式 渲染功能的最简单方法是从示例开始。如下所示:
1// Create rendering options setting the image resolution 150 dpi.
2PngMathRendererOptions options = new PngMathRendererOptions();
3options.setResolution(150);
4// Specify the preamble.
5options.setPreamble("\\usepackage{amsmath}\r\n\\usepackage{amsfonts}\r\n\\usepackage{amssymb}\r\n\\usepackage{color}");
6// Specify the scaling factor 300%.
7options.setScale(3000);
8// Specify the foreground color.
9options.setTextColor(Color.BLACK);
10// Specify the background color.
11options.setBackgroundColor(Color.WHITE);
12// Specify the output stream for the log file.
13options.setLogStream(new ByteArrayOutputStream());
14// Specify whether to show the terminal output on the console or not.
15options.showTerminal(true);
16
17// Create the output stream for the formula image.
18final OutputStream stream = new FileOutputStream(Utils.getOutputDirectory() + "math-formula.png");
19try {
20 // Run rendering.
21 com.aspose.tex.Size2D size = MathRenderer.render("\\begin{equation*}\r\n" +
22 "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" +
23 "\\end{equation*}", stream, options);
24
25 // Show other results.
26 System.out.println(options.getErrorReport());
27 System.out.println();
28 System.out.println("Size: " + size.getWidth() + "x" + size.getHeight()); // Dimensions of the resulting image.
29} finally {
30 if (stream != null)
31 stream.close();
32}让我们进入细节。首先,我们创建一个 渲染选项实例,类似于 TeX/LaTeX 排版。我们在此同时指定输出图像的分辨率。
接下来,指定前导宏。默认前导宏为:
1\usepackage{amsmath}
2\usepackage{amsfonts}
3\usepackage{amssymb}它提供比基础 LaTeX 稍微高级一点的数学公式支持。例如,您可以添加 color 包,以便在公式中使用自定义高亮,正如代码示例中所示。
然后我们指示渲染器将输出放大 300%。
接下来两个选项定义前景色和背景色。公式中未被自定义高亮覆盖(“着色”)的部分将使用 TextColor 颜色显示。
示例中的下一行并没有太大意义。它仅演示您可以将日志输出定向到某个流。
最后一个选项 ShowTerminal 允许您切换是否将终端输出写入控制台。
实际执行渲染的方法是 MathRenderer.render()。它返回公式的点数大小。
图像写入的流作为第二个参数传递给该方法。我们随后创建该流。
最后,我们调用 MathRenderer.render() 方法本身,将选项作为第三个参数传入。公式的 LaTeX 代码作为第一个参数传入。
示例的最后几行打印了两个渲染产物——公式的大小以及简要的错误报告(如果有错误的话)。
以下是渲染结果。
这是 LaTeX数学公式 渲染的最通用使用场景。
您也可以查看基于 Aspose.TeX for .NET API 实现的免费 Web 应用。 这里 是 Java 版本页面。
如何将 LaTeX 数学公式渲染为 SVG
以相同的方式,我们可以将 LaTeX 数学公式渲染为 SVG 格式。
1// Create rendering options.
2MathRendererOptions options = new SvgMathRendererOptions();
3// Specify the preamble.
4options.setPreamble("\\usepackage{amsmath}\r\n\\usepackage{amsfonts}\r\n\\usepackage{amssymb}\r\n\\usepackage{color}");
5// Specify the scaling factor 300%.
6options.setScale(3000);
7// Specify the foreground color.
8options.setTextColor(Color.BLACK);
9// Specify the background color.
10options.setBackgroundColor(Color.WHITE);
11// Specify the output stream for the log file.
12options.setLogStream(new ByteArrayOutputStream());
13// Specify whether to show the terminal output on the console or not.
14options.showTerminal(true);
15
16// Create the output stream for the formula image.
17final OutputStream stream = new FileOutputStream(Utils.getOutputDirectory() + "math-formula.svg");
18try {
19 // Run rendering.
20 com.aspose.tex.Size2D size = new SvgMathRenderer().render("\\begin{equation*}\r\n" +
21 "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" +
22 "\\end{equation*}", stream, options, size);
23
24 // Show other results.
25 System.out.println(options.getErrorReport());
26 System.out.println();
27 System.out.println("Size: " + size.getWidth() + "x" + size.getHeight()); // Dimensions of the resulting image.
28} finally {
29 if (stream != null)
30 stream.close();
31}区别如下:
- 我们使用 SvgMathRendererOptions 类,而不是 PngMathRendererOptions。
- 我们不指定分辨率。
- 我们使用 SvgMathRenderer 类,而不是 PngMathRenderer。
以下是结果:
您也可以查看基于 Aspose.TeX for .NET API 实现的免费 Web 应用。
