乳胶图渲染| .NET的Aspose.TeX
有时,您可能需要从乳胶文件中提取一些内容作为单独渲染的片段或 “图”,而无需与页面布局的任何连接。例如,它可以是您在互联网上出版的例证。您可以使用我们的API进行。至于目标格式,有两种选项-PNG和SVG。与乳胶数学公式渲染功能一样。还值得注意的是,乳胶图渲染是 乳胶数学公式渲染的概括。
如何将乳胶图形渲染到png
与此主题相关的API参考部分是 此处。与公式渲染一样,我们将从一个示例开始。这里是:
1 // Render LaTeX figure to PNG image
2
3 // Create rendering options setting the image resolution to 150 dpi.
4 PngFigureRendererOptions options = new PngFigureRendererOptions();
5 options.Resolution = 150;
6
7 // Specify the preamble.
8 options.Preamble = "\\usepackage{pict2e}";
9
10 // Specify the scaling factor 300%.
11 options.Scale = 3000;
12
13 // Specify the background color.
14 options.BackgroundColor = System.Drawing.Color.White;
15
16 // Specify the output stream for the log file.
17 options.LogStream = new System.IO.MemoryStream();
18
19 // Specify whether to show the terminal output on the console or not.
20 options.ShowTerminal = true;
21
22 // Create the output stream for the figure image.
23 using (System.IO.Stream stream = System.IO.File.Open(
24 System.IO.Path.Combine(OutputDir, "text-and-formula.png"), System.IO.FileMode.Create))
25 {
26 // Run rendering.
27 System.Drawing.SizeF size = new PngFigureRenderer().Render(@"\setlength{\unitlength}{0.8cm}
28\begin{picture}(6,5)
29\thicklines
30\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$}
31\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}$}
32\end{picture}", stream, options);
33
34 // Show other results.
35 System.Console.Out.WriteLine(options.ErrorReport);
36 System.Console.Out.WriteLine();
37 System.Console.Out.WriteLine($"Size: {size}"); // Dimensions of the resulting image.
38 }首先,我们创建 乳胶图选项实例。我们在这里同时指定输出图像分辨率。
接下来,我们指定前导码。LaTeX 图形渲染没有默认的前导码,因此,例如,如果您要渲染一些使用 pict2e LaTeX 包绘制的图形,则需要在前导码中指定它:
1\usepackage{pict2e}然后,我们指示渲染器将输出扩展300%。
下一个选项定义背景颜色。与数学公式渲染不同,我们没有指定前景颜色,因为我们假设颜色完全在乳胶代码的控制之下。实际上,背景颜色也是如此,因此这只是一个便利选择。
该示例的下一行没有太多意义。它只是证明您可以将日志输出引导到某些流。
最后一个选项ShowTerminal使您可以切换将终端输出写入控制台。
实际执行渲染的方法是 figurerenderer.render()。它返回点的大小。
该方法将要编写图像的流作为第二个参数接受。我们下一步创建流。
最后,我们将FigureRenderer.render()方法本身称为第三个参数。该图的乳胶代码作为第一个参数传递。
示例的最后一行打印了图形渲染的两个伪像 - 图的大小和简短的错误报告(如果有错误)。
这是渲染的结果。
这是乳胶图渲染功能最通用的用例。
如何将乳胶数字渲染到SVG
以几乎相同的方式,我们可以以SVG格式渲染乳胶图。
1 // Render LaTeX figure to SVG image
2
3 // Create rendering options.
4 FigureRendererOptions options = new SvgFigureRendererOptions();
5
6 // Specify the preamble.
7 options.Preamble = "\\usepackage{pict2e}";
8
9 // Specify the scaling factor 300%.
10 options.Scale = 3000;
11
12 // Specify the background color.
13 options.BackgroundColor = System.Drawing.Color.White;
14
15 // Specify the output stream for the log file.
16 options.LogStream = new System.IO.MemoryStream();
17
18 // Specify whether to show the terminal output on the console or not.
19 options.ShowTerminal = true;
20
21 // Create the output stream for the figure image.
22 using (System.IO.Stream stream = System.IO.File.Open(
23 System.IO.Path.Combine(OutputDir, "text-and-formula.svg"), System.IO.FileMode.Create))
24 {
25 // Run rendering.
26 System.Drawing.SizeF size = new SvgFigureRenderer().Render(@"\setlength{\unitlength}{0.8cm}
27\begin{picture}(6,5)
28\thicklines
29\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$}
30\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}$}
31\end{picture}", stream, options);
32
33 // Show other results.
34 System.Console.Out.WriteLine(options.ErrorReport);
35 System.Console.Out.WriteLine();
36 System.Console.Out.WriteLine($"Size: {size}"); // Dimensions of the resulting image.
37 }差异是:
- 我们使用 SVGFIGURERENDEREROPTIONS类,而不是 PNGFIGURERENDEREROPTIONS。
- 我们没有指定分辨率。
- 我们使用 svgfigurerenderer类代替 pngfigurerenderer。
这是结果:
