LaTeX 图形渲染 | Python via .NET
在某些情况下,您可能需要将 LaTeX 文件中的特定内容提取为一个独立渲染的部分,通常称为 图形,且不与页面布局关联。例如,这在为在线出版物创建插图时可能会很有用。我们的 API 允许您实现此功能。用于渲染图形的目标格式为 PNG 和 SVG,正如 LaTeX 数学公式渲染功能一样。值得注意的是,LaTeX 图形渲染是一个比 LaTeX 数学公式渲染 更通用的功能。
将 LaTeX 图形渲染为 PNG
如有需要,首先查看此主题的 API 参考章节。与公式渲染类似,我们将从一个示例开始。示例代码如下:
将 ASPOSE_TEX_OUTPUT_DIR 设置为输出目录,或使用默认的 output 文件夹。示例包含全部 LaTeX 代码,无需外部输入文件。请先应用许可证。
许可证.
1# Render a LaTeX figure with a triangle and formulas to PNG.
2import os
3from pathlib import Path
4from aspose.pydrawing import Color
5from aspose.tex.features import PngFigureRenderer, PngFigureRendererOptions
6
7# Set an output directory, or use the default output folder.
8output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
9output_dir.mkdir(parents=True, exist_ok=True)
10
11# Create rendering options setting the image resolution to 150 dpi.
12options = PngFigureRendererOptions()
13options.resolution = 150
14# Specify the preamble.
15options.preamble = r"\usepackage{pict2e}"
16# Specify the scaling factor 300%.
17options.scale = 3000
18# Leave space around the rendered fragment.
19options.margin = 5
20# Specify the background color.
21options.background_color = Color.white
22# Specify whether to show the terminal output on the console or not.
23options.show_terminal = False
24
25# Create binary streams for the rendering log and figure image.
26with (output_dir / "text-and-formula.png.log").open("wb") as log_stream, \
27 (output_dir / "text-and-formula.png").open("wb") as stream:
28 options.log_stream = log_stream
29 # Run rendering.
30 size = PngFigureRenderer().render(r"""\setlength{\unitlength}{0.8cm}
31\begin{picture}(6,5)
32\thicklines
33\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$}
34\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}$}
35\end{picture}""", stream, options)
36
37# Show other results.
38print(options.error_report or "No rendering errors.")
39print()
40print(f"Size: {size.width}x{size.height}")首先,我们创建 LaTeX 图形渲染器选项 类的实例,并同时指定输出图像的分辨率。
接下来,需要指定前导内容(preamble)。与 LaTeX 数学公式渲染不同,LaTeX 图形渲染没有默认的前导内容。因此,如果您计划渲染使用 pict2e LaTeX 包创建的图形,例如,需要在前导内容中指定该包:
1\usepackage{pict2e}设置 scale = 3000 将比例设为 300%,并通过 margin = 5 在片段周围留出空白。PNG 示例使用 resolution = 150 DPI。
下一个选项决定背景颜色。与公式渲染不同,我们不需要指定前景颜色,因为我们假设颜色完全由 LaTeX 代码控制。同样,背景颜色也由 LaTeX 代码控制,此选项仅为方便而提供。
示例将二进制文件流赋给 log_stream,在 text-and-formula.png 旁保存日志 text-and-formula.png.log。with 语句会自动关闭这两个流。
show_terminal 控制是否在控制台显示终端输出。示例将其设为 False,但仍将日志写入文件。
负责渲染图形的方法是 FigureRenderer.render() 。该方法返回图形的点数大小。
第二个参数是为图像打开的二进制输出流,第三个参数提供渲染选项。
最后,我们调用 FigureRenderer.render() 方法本身,将选项作为第三个参数传入。图形的 LaTeX 代码作为第一个参数提供。
最后几行输出错误报告;如果报告为空,则输出 No rendering errors.,然后显示以点为单位的宽度和高度。
以下是渲染结果。
这代表了 LaTeX 图形渲染 功能的最通用使用场景。
将 LaTeX 图形渲染为 SVG
同样,我们也可以将 LaTeX 图形渲染为 SVG 格式。
1# Render a LaTeX figure with a triangle and formulas to SVG.
2import os
3from pathlib import Path
4from aspose.pydrawing import Color
5from aspose.tex.features import SvgFigureRenderer, SvgFigureRendererOptions
6
7# Set an output directory, or use the default output folder.
8output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
9output_dir.mkdir(parents=True, exist_ok=True)
10
11# Create rendering options.
12options = SvgFigureRendererOptions()
13# Specify the preamble.
14options.preamble = r"\usepackage{pict2e}"
15# Specify the scaling factor 300%.
16options.scale = 3000
17# Leave space around the rendered fragment.
18options.margin = 5
19# Specify the background color.
20options.background_color = Color.white
21# Specify whether to show the terminal output on the console or not.
22options.show_terminal = False
23
24# Create binary streams for the rendering log and figure image.
25with (output_dir / "text-and-formula.svg.log").open("wb") as log_stream, \
26 (output_dir / "text-and-formula.svg").open("wb") as stream:
27 options.log_stream = log_stream
28 # Run rendering.
29 size = SvgFigureRenderer().render(r"""\setlength{\unitlength}{0.8cm}
30\begin{picture}(6,5)
31\thicklines
32\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$}
33\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}$}
34\end{picture}""", stream, options)
35
36# Show other results.
37print(options.error_report or "No rendering errors.")
38print()
39print(f"Size: {size.width}x{size.height}")此示例在同一输出目录中创建 text-and-formula.svg 和日志 text-and-formula.svg.log。
区别如下:
- 使用 SvgFigureRendererOptions 类,而不是 PngFigureRendererOptions 类。
- 不需要指定分辨率。
- 使用 SvgFigureRenderer 类,而不是 PngFigureRenderer 类。
以下是渲染结果:
