乳胶数学公式渲染| Python

将乳胶数学公式渲染为PNG

如果需要,请查看 此主题的API参考部分。实际上,演示乳胶数学公式渲染功能的最佳方法是从示例开始。这里是:

 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}")

让我们了解细节。首先,我们启动一个 渲染选项对象,类似于Tex/latex排版,其中包括所需的输出图像分辨率的规范。

接下来,我们指定序言。默认序言是:

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

与标准乳胶相比,它为数学公式提供了增强的支持。例如,您可以包含“颜色”软件包来自定义公式中的突出显示,如提供的代码示例所示。

然后,我们指示渲染器将输出扩展300%。

接下来的两个选项确定了前景和背景颜色。不受自定义突出显示影响的公式的任何部分都将显示在“ TextColor”颜色中。

该示例中的以下行是您可以选择将日志输出将其重定向到特定流的例证。

最后,show -terminal选项使您能够控制终端输出是否应在控制台中显示。

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

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

最后,调用了mathrender.render()方法,其中选项通过作为第三个参数。公式的乳胶代码作为第一个参数提供。

该示例的最后一行显示数学公式渲染过程的两个输出 - 公式的大小和简明错误报告,如果发生任何错误。

这是渲染的结果。

乳胶数学公式渲染到PNG

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

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

将乳胶数学公式渲染为SVG

同样,我们可以使用相同的方法将乳胶数学公式转换为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}")

差异是:

这是结果:

乳胶数学公式渲染到SVG

您还可以探索免费的 web 应用程序,它利用了 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.