乳胶数学公式渲染|爪哇

如何将乳胶数学公式渲染到PNG

实际上,演示乳胶数学公式渲染功能的最简单方法是从示例开始。这里是:

 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}

与基本乳胶相比,它提供了更高级的数学公式支持。例如,如果您想在公式中使用自己的突出显示,则可以添加“颜色”软件包,如我们在代码示例中所示。

然后,我们指示渲染器将输出扩展300%。

接下来的两个选项定义了前景和背景颜色。自定义突出显示的未覆盖(“彩色”)的公式的那些部分将显示在TextColor颜色中。

该示例的下一行没有太多意义。它只是证明您可以将日志输出引导到某些流。

最后一个选项ShowTerminal使您可以切换将终端输出写入控制台。

实际执行渲染的方法是 Mathrenderer.render()。它返回点数中的公式大小。

该方法将要编写图像的流作为第二个参数接受。我们下一步创建流。

最后,我们将MathRenderer.render()方法本身称为第三个参数。公式的乳胶代码作为第一个参数传递。

示例的最后一行打印了数学公式渲染的两个伪像 - 公式的大小和简短的错误报告(如果有错误)。

这是渲染的结果。

乳胶图渲染到PNG

这是乳胶数学公式渲染最通用的用例。

您还可以查看免费的 网页应用,它基于 Aspose.TeX for .NET API 中实现的功能构建。 此处 是 Java 版本页面。

如何将乳胶数学公式渲染到SVG

以几乎相同的方式,我们可以将乳胶数学公式呈现为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}

差异是:

这是结果:

乳胶数学公式渲染到SVG

您还可以查看基于 Aspose.TeX for .NET API 中实现的功能构建的免费 web 应用程序

Have any questions about Aspose.TeX?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.