LaTeX 数学公式渲染 | Python via .NET

将 LaTeX 数学公式渲染为 PNG

如果需要,请查看本主题的 API 参考章节。事实上,演示 LaTeX 数学公式 渲染功能的最佳方式是从一个示例开始。下面是示例:

将 ASPOSE_TEX_OUTPUT_DIR 设置为输出目录,或使用默认的 output 文件夹。示例包含全部 LaTeX 代码,无需外部输入文件。请先应用许可证。 许可证.

 1# Render a highlighted LaTeX math formula to PNG.
 2import os
 3from pathlib import Path
 4from aspose.pydrawing import Color
 5from aspose.tex.features import PngMathRenderer, PngMathRendererOptions
 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 = PngMathRendererOptions()
13options.resolution = 150
14# Specify the preamble.
15options.preamble = r"""\usepackage{amsmath}
16\usepackage{amsfonts}
17\usepackage{amssymb}
18\usepackage{color}"""
19# Specify the scaling factor 300%.
20options.scale = 3000
21# Leave space around the rendered fragment.
22options.margin = 5
23# Specify the foreground color.
24options.text_color = Color.black
25# Specify the background color.
26options.background_color = Color.white
27# Specify whether to show the terminal output on the console or not.
28options.show_terminal = False
29
30# Create binary streams for the rendering log and formula image.
31with (output_dir / "math-formula.png.log").open("wb") as log_stream, \
32        (output_dir / "math-formula.png").open("wb") as stream:
33    options.log_stream = log_stream
34    # Run rendering.
35    size = PngMathRenderer().render(r"""\begin{equation*}
36e^x = x^{\color{red}0} + x^{\color{red}1} + \frac{x^{\color{red}2}}{2} + \frac{x^{\color{red}3}}{6} + \cdots = \sum_{n\geq 0} \frac{x^{\color{red}n}}{n!}
37\end{equation*}""", stream, options)
38
39# Show other results.
40print(options.error_report or "No rendering errors.")
41print()
42print(f"Size: {size.width}x{size.height}")

让我们进入细节。首先,我们实例化一个 rendering options 对象,这类似于 TeX/LaTeX 排版,并包含所需输出图像分辨率的规格。

接下来,我们指定前导内容。默认的前导内容是:

1\usepackage{amsmath}
2\usepackage{amsfonts}
3\usepackage{amssymb}

与标准 LaTeX 相比,这提供了对数学公式的增强支持。例如,您可以包含 color 包来自定义公式中的高亮显示,如提供的代码示例所示。

设置 scale = 3000 将比例设为 300%,并通过 margin = 5 在片段周围留出空白。PNG 示例使用 resolution = 150 DPI。

接下来的两个选项决定前景色和背景色。公式中未受到自定义高亮影响的任何部分将以 text_color 颜色显示。

示例将二进制文件流赋给 log_stream,在 math-formula.png 旁保存日志 math-formula.png.log。with 语句会自动关闭这两个流。

show_terminal 控制是否在控制台显示终端输出。示例将其设为 False,但仍将日志写入文件。

实际的渲染过程由 MathRenderer.render() 方法执行,该方法返回公式的点数大小。

第二个参数是为图像打开的二进制输出流,第三个参数提供渲染选项。

最后,调用 MathRenderer.render() 方法,第三个参数传入选项。公式的 LaTeX 代码作为第一个参数提供。

最后几行输出错误报告;如果报告为空,则输出 No rendering errors.,然后显示以点为单位的宽度和高度。

下面是渲染结果。

LaTeX Math Formula rendering to PNG

这代表了 LaTeX 数学公式渲染 功能最常见且最广泛的应用。

您还可以探索免费的 web app,它利用了 Aspose.TeX for .NET API 实现的功能。

将 LaTeX 数学公式渲染为 SVG

同样,我们可以使用相同的方法将 LaTeX 数学公式转换为 SVG 格式。

 1# Render a highlighted LaTeX math formula to SVG.
 2import os
 3from pathlib import Path
 4from aspose.pydrawing import Color
 5from aspose.tex.features import SvgMathRenderer, SvgMathRendererOptions
 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 = SvgMathRendererOptions()
13# Specify the preamble.
14options.preamble = r"""\usepackage{amsmath}
15\usepackage{amsfonts}
16\usepackage{amssymb}
17\usepackage{color}"""
18# Specify the scaling factor 300%.
19options.scale = 3000
20# Leave space around the rendered fragment.
21options.margin = 5
22# Specify the foreground color.
23options.text_color = Color.black
24# Specify the background color.
25options.background_color = Color.white
26# Specify whether to show the terminal output on the console or not.
27options.show_terminal = False
28
29# Create binary streams for the rendering log and formula image.
30with (output_dir / "math-formula.svg.log").open("wb") as log_stream, \
31        (output_dir / "math-formula.svg").open("wb") as stream:
32    options.log_stream = log_stream
33    # Run rendering.
34    size = SvgMathRenderer().render(r"""\begin{equation*}
35e^x = x^{\color{red}0} + x^{\color{red}1} + \frac{x^{\color{red}2}}{2} + \frac{x^{\color{red}3}}{6} + \cdots = \sum_{n\geq 0} \frac{x^{\color{red}n}}{n!}
36\end{equation*}""", stream, options)
37
38# Show other results.
39print(options.error_report or "No rendering errors.")
40print()
41print(f"Size: {size.width}x{size.height}")

此示例在同一输出目录中创建 math-formula.svg 和日志 math-formula.svg.log。

区别如下:

下面是结果:

LaTeX Math Formula rendering to SVG

您还可以探索免费的 web app,它利用了 Aspose.TeX for .NET API 实现的功能。