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

将 LaTeX 数学公式渲染为 PNG

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

 1from aspose.tex.features import *
 2from aspose.pydrawing import Color
 3from util import Util
 4from io import BytesIO
 5from os import path
 6###############################################
 7###### Class and Method declaration here ######
 8###############################################
 9
10# Create rendering options setting the image resolution to 150 dpi.
11options = PngMathRendererOptions() 
12options.resolution = 150             # Specify the preamble.
13options.preamble = r"""\usepackage{amsmath}
14\usepackage{amsfonts}
15\usepackage{amssymb}
16\usepackage{color}"""
17# Specify the scaling factor 300%.
18options.scale = 3000
19# Specify the foreground color.
20options.text_color = Color.black
21# Specify the background color.
22options.background_color = Color.white
23# Specify the output stream for the log file.
24options.log_stream = BytesIO()
25# Specify whether to show the terminal output on the console or not.
26options.show_terminal = True
27
28# Create the output stream for the formula image.
29with open(path.join(Util.output_directory, "math-formula.png"), "wb") as stream:
30    # Run rendering.
31    size = PngMathRenderer().render(r"""\begin{equation*}
32e^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!}
33\end{equation*}""", stream, options)
34
35# Show other results.
36print(options.error_report)
37print()
38print(f"Size: {size.width}x{size.height}")

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

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

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

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

然后我们指示渲染器将输出放大 300%。

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

示例中的下一行说明了您可以将日志输出重定向到特定流的选项。

最后,ShowTerminal 选项使您能够控制是否在控制台显示终端输出。

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

该方法接受图像将被写入的流作为第二个参数。随后,我们继续创建该流。

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

示例的最后几行显示了渲染过程的两个输出——公式的大小以及(如果出现错误)简要的错误报告。

下面是渲染结果。

LaTeX Math Formula rendering to PNG

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

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

将 LaTeX 数学公式渲染为 SVG

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

 1from aspose.tex.features import *
 2from aspose.pydrawing import Color
 3from util import Util
 4from io import BytesIO
 5from os import path
 6###############################################
 7###### Class and Method declaration here ######
 8###############################################
 9
10# Create rendering options.
11options = SvgMathRendererOptions()
12# Specify the preamble.
13options.preamble = r"""\usepackage{amsmath}
14\usepackage{amsfonts}
15\usepackage{amssymb}
16\usepackage{color}"""
17# Specify the scaling factor 300%.
18options.scale = 3000
19# Specify the foreground color.
20options.text_color = Color.black
21# Specify the background color.
22options.background_color = Color.white
23# Specify the output stream for the log file.
24options.log_stream = BytesIO()
25# Specify whether to show the terminal output on the console or not.
26options.show_terminal = True
27
28# Create the output stream for the formula image.
29with open(path.join(Util.output_directory, "math-formula.svg"), "wb") as stream:
30    # Run rendering.
31    size = SvgMathRenderer().render(r"""\begin{equation*}
32e^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!}
33\end{equation*}""", stream, options)
34
35# Show other results.
36print(options.error_report)
37print()
38print(f"Size: {size.width}x{size.height}")

区别如下:

下面是结果:

LaTeX Math Formula rendering to SVG

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

Have any questions about Aspose.TeX?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.