乳胶图渲染| Aspose.TeX for Python
在某些情况下,您可能需要从 LaTeX 文件中提取特定内容作为独立的渲染片段,通常称为“图形”,而无需与页面布局建立任何关联。例如,在为在线出版物创建插图时,这可能很有用。我们的 API 允许您实现这一点。图形渲染的目标格式是 PNG 和 SVG,就像 LaTeX 数学公式渲染功能一样。值得注意的是,与 LaTeX 数学公式渲染 相比,LaTeX 图形渲染 是一项更通用的功能。
将乳胶图形呈现到png
如果需要,请首先查看 此主题的API参考部分。与公式渲染类似,我们将以一个示例开始。这里是:
1# Create rendering options setting the image resolution to 150 dpi.
2options = PngFigureRendererOptions()
3options.resolution = 150 # Specify the preamble.
4options.preamble = r"\usepackage{pict2e}"
5# Specify the scaling factor 300%.
6options.scale = 3000
7# Specify the background color.
8options.background_color = Color.white
9# Specify the output stream for the log file.
10options.log_stream = BytesIO()
11# Specify whether to show the terminal output on the console or not.
12options.show_terminal = True
13
14# Create the output stream for the figure image.
15with open(path.join(Util.output_directory, "text-and-formula.png"), "wb") as stream:
16 # Run rendering.
17 size = PngFigureRenderer().render(r"""\setlength{\unitlength}{0.8cm}
18\begin{picture}(6,5)
19\thicklines
20\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$}
21\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}$}
22\end{picture}""", stream, options)
23
24# Show other results.
25print(options.error_report)
26print()
27print(f"Size: {size.width}x{size.height}")
首先,我们创建一个 乳胶图渲染器选项类的实例,我们同时指定输出图像分辨率。
接下来,我们需要指定序言。与乳胶数学公式渲染不同,乳胶图渲染没有默认的序言。因此,如果您打算使用“ Pict2e”乳胶软件包创建图形,则需要在序言中指定它:
1\usepackage{pict2e}
然后,我们指示渲染器将输出扩展300%。
下一个选项确定背景颜色。与数学公式渲染不同,我们不需要指定前景颜色,因为我们假设颜色完全由乳胶代码控制。同样,背景颜色也由乳胶代码控制,因此为方便起见提供此选项。
示例中的以下行可能不是特别有意义。只是旨在证明您可以选择将日志输出引导到特定流。
最终选项“ Show -Terminal”使您可以控制终端输出是否应显示在控制台中。
负责渲染该图的方法是 figurerenderer.render()。它返回点的大小。
该方法接受流作为第二个参数,这是图像写的流。我们在下一行中创建流。
最后,我们调用figurerender.render()
方法本身,将选项作为第三个参数传递。该图的乳胶代码作为第一个参数提供。
该示例的最后几行显示了与图形渲染相关的两个信息 - 图的大小和简短的错误报告(如果有任何错误)。
这是渲染的结果。
这代表了乳胶图渲染功能最通用的用例。
将乳胶数字渲染为SVG
同样,我们也可以以SVG格式渲染乳胶图。
1# Create rendering options.
2options = SvgFigureRendererOptions()
3# Specify the preamble.
4options.preamble = r"\usepackage{pict2e}"
5# Specify the scaling factor 300%.
6options.scale = 3000
7# Specify the background color.
8options.background_color = Color.white
9# Specify the output stream for the log file.
10options.log_stream = BytesIO()
11# Specify whether to show the terminal output on the console or not.
12options.show_terminal = True
13
14# Create the output stream for the figure image.
15with open(path.join(Util.output_directory, "text-and-formula.svg"), "wb") as stream:
16 # Run rendering.
17 size = SvgFigureRenderer().render(r"""\setlength{\unitlength}{0.8cm}
18\begin{picture}(6,5)
19\thicklines
20\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$}
21\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}$}
22\end{picture}""", stream, options)
23
24# Show other results.
25print(options.error_report)
26print()
27print(f"Size: {size.width}x{size.height}")
差异是:
- 我们使用 svgfigurerendereroptions类,而不是使用 pngfigurerendereroptions类。
- 我们不需要指定分辨率。
- 我们使用 svgfigurererer类,而不是使用 pngfigurerenderer类。
这是结果: