乳胶图渲染| 用于 Java 的 Aspose.TeX

可能会从乳胶文件中提取一些内容(图形,写作,绘图等)作为单独渲染的片段或 figure,而不管其在输出文档页面上,无论其位置如何。例如,您在互联网上出版的插图就是这种情况。我们的API可以帮助您完成任务。有两种可用的目标格式-PNG和SVG。就像乳胶数学公式渲染一样。我们还必须注意,乳胶图渲染乳胶数学公式渲染的概括。

如何将乳胶图形渲染到png

与公式渲染一样,我们将从一个示例开始。这里是:

 1// Create rendering options setting the image resolution to 150 dpi.
 2PngFigureRendererOptions options = new PngFigureRendererOptions();
 3options.setResolution(150);
 4// Specify the preamble.
 5options.setPreamble("\\usepackage{pict2e}");
 6// Specify the scaling factor 300%.
 7options.setScale(3000);
 8// Specify the background color.
 9options.setBackgroundColor(Color.WHITE);
10// Specify the output stream for the log file.
11options.setLogStream(new ByteArrayOutputStream());
12// Specify whether to show the terminal output on the console or not.
13options.showTerminal(true);
14
15// Create the output stream for the figure image.
16final OutputStream stream = new FileOutputStream(Utils.getOutputDirectory() + "text-and-formula.png");
17try {
18    // Run rendering.
19    com.aspose.tex.Size2D size = new PngFigureRenderer().render("\\setlength{\\unitlength}{0.8cm}\r\n" +
20    "\\begin{picture}(6,5)\r\n" +
21    "\\thicklines\r\n" +
22    "\\put(1,0.5){\\line(2,1){3}} \\put(4,2){\\line(-2,1){2}} \\put(2,3){\\line(-2,-5){1}} \\put(0.7,0.3){$A$} \\put(4.05,1.9){$B$} \\put(1.7,2.95){$C$}\r\n" +
23    "\\put(3.1,2.5){$a$} \\put(1.3,1.7){$b$} \\put(2.5,1.05){$c$} \\put(0.3,4){$F=\\sqrt{s(s-a)(s-b)(s-c)}$} \\put(3.5,0.4){$\\displaystyle s:=\\frac{a+b+c}{2}$}\r\n" +
24    "\\end{picture}", stream, options);
25    
26    // Show other results.
27    System.out.println(options.getErrorReport());
28    System.out.println();
29    System.out.println("Size: " + size.getWidth() + "x" + size.getHeight()); // Dimensions of the resulting image.
30} finally {
31    if (stream != null)
32        stream.close();
33}

首先,我们创建一个 渲染选项实例。我们在此处执行此操作,并指定输出图像分辨率。

接下来,我们指定前导码。LaTeX 图形渲染没有默认的前导码,因此,例如,如果你想渲染一些使用 pict2e LaTeX 包绘制的图形,则需要在前导码中指定它:

1\usepackage{pict2e}

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

下一个选项定义背景颜色。与数学公式渲染不同,我们没有指定前景颜色,因为我们假设颜色完全在乳胶代码的控制之下。实际上,背景颜色也是如此,因此这只是一个便利选择。

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

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

实际执行渲染的方法是 figurerenderer.render()。它返回点的大小。

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

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

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

这是渲染的结果。

乳胶图渲染到PNG

这是乳胶图渲染功能最通用的用例。

如何将乳胶数字渲染到SVG

以几乎相同的方式,我们可以将乳胶数字呈现为SVG格式。

 1// Create rendering options.
 2SvgFigureRendererOptions options = new SvgFigureRendererOptions();
 3// Specify the preamble.
 4options.setPreamble("\\usepackage{pict2e}");
 5// Specify the scaling factor 300%.
 6options.setScale(3000);
 7// Specify the background color.
 8options.setBackgroundColor(Color.WHITE);
 9// Specify the output stream for the log file.
10options.setLogStream(new ByteArrayOutputStream());
11// Specify whether to show the terminal output on the console or not.
12options.showTerminal(true);
13
14// Create the output stream for the figure image.
15final OutputStream stream = new FileOutputStream(Utils.getOutputDirectory() + "text-and-formula.svg");
16try {
17    // Run rendering.
18    com.aspose.tex.Size2D size = new SvgFigureRenderer().render("\\setlength{\\unitlength}{0.8cm}\r\n" +
19    "\\begin{picture}(6,5)\r\n" +
20    "\\thicklines\r\n" +
21    "\\put(1,0.5){\\line(2,1){3}} \\put(4,2){\\line(-2,1){2}} \\put(2,3){\\line(-2,-5){1}} \\put(0.7,0.3){$A$} \\put(4.05,1.9){$B$} \\put(1.7,2.95){$C$}\r\n" +
22    "\\put(3.1,2.5){$a$} \\put(1.3,1.7){$b$} \\put(2.5,1.05){$c$} \\put(0.3,4){$F=\\sqrt{s(s-a)(s-b)(s-c)}$} \\put(3.5,0.4){$\\displaystyle s:=\\frac{a+b+c}{2}$}\r\n" +
23    "\\end{picture}", 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}

差异是:

这是结果:

乳胶图渲染到SVG

Have any questions about Aspose.TeX?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.