乳胶图渲染| .NET的Aspose.TeX

有时,您可能需要从乳胶文件中提取一些内容作为单独渲染的片段或 “图”,而无需与页面布局的任何连接。例如,它可以是您在互联网上出版的例证。您可以使用我们的API进行。至于目标格式,有两种选项-PNG和SVG。与乳胶数学公式渲染功能一样。还值得注意的是,乳胶图渲染乳胶数学公式渲染的概括。

如何将乳胶图形渲染到png

与此主题相关的API参考部分是 此处。与公式渲染一样,我们将从一个示例开始。这里是:

 1// Create rendering options setting the image resolution to 150 dpi.
 2PngFigureRendererOptions options = new PngFigureRendererOptions();
 3options.Resolution = 150;
 4// Specify the preamble.
 5options.Preamble = "\\usepackage{pict2e}";
 6// Specify the scaling factor 300%.
 7options.Scale = 3000;
 8// Specify the background color.
 9options.BackgroundColor = System.Drawing.Color.White;
10// Specify the output stream for the log file.
11options.LogStream = new System.IO.MemoryStream();
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.
16using (System.IO.Stream stream = System.IO.File.Open(
17    System.IO.Path.Combine(RunExamples.OutputDirectory, "text-and-formula.png"), System.IO.FileMode.Create))
18{
19    // Run rendering.
20    System.Drawing.SizeF size = new PngFigureRenderer().Render(@"\setlength{\unitlength}{0.8cm}
21\begin{picture}(6,5)
22\thicklines
23\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$}
24\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}$}
25\end{picture}", stream, options);
26    
27    // Show other results.
28    System.Console.Out.WriteLine(options.ErrorReport);
29    System.Console.Out.WriteLine();
30    System.Console.Out.WriteLine("Size: " + size); // Dimensions of the resulting image.
31}

首先,我们创建 乳胶图选项实例。我们在这里同时指定输出图像分辨率。

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

1\usepackage{pict2e}

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

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

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

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

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

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

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

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

这是渲染的结果。

乳胶图渲染到PNG

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

如何将乳胶数字渲染到SVG

以几乎相同的方式,我们可以以SVG格式渲染乳胶图。

 1// Create rendering options.
 2FigureRendererOptions options = new SvgFigureRendererOptions();
 3// Specify the preamble.
 4options.Preamble = "\\usepackage{pict2e}";
 5// Specify the scaling factor 300%.
 6options.Scale = 3000;
 7// Specify the background color.
 8options.BackgroundColor = System.Drawing.Color.White;
 9// Specify the output stream for the log file.
10options.LogStream = new System.IO.MemoryStream();
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.
15using (System.IO.Stream stream = System.IO.File.Open(
16    System.IO.Path.Combine(RunExamples.OutputDirectory, "text-and-formula.svg"), System.IO.FileMode.Create))
17{
18    // Run rendering.
19    System.Drawing.SizeF size = new SvgFigureRenderer().Render(@"\setlength{\unitlength}{0.8cm}
20\begin{picture}(6,5)
21\thicklines
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$}
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}$}
24\end{picture}", stream, options);
25    
26    // Show other results.
27    System.Console.Out.WriteLine(options.ErrorReport);
28    System.Console.Out.WriteLine();
29    System.Console.Out.WriteLine("Size: " + size); // Dimensions of the resulting image.
30}

差异是:

这是结果:

乳胶图渲染到SVG

Have any questions about Aspose.TeX?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.