乳胶数学公式渲染| Python
将乳胶数学公式渲染为PNG
如果需要,请查看 此主题的API参考部分。实际上,演示乳胶数学公式渲染功能的最佳方法是从示例开始。这里是:
1# Create rendering options setting the image resolution to 150 dpi.
2options = PngMathRendererOptions()
3options.resolution = 150 # Specify the preamble.
4options.preamble = r"""\usepackage{amsmath}
5\usepackage{amsfonts}
6\usepackage{amssymb}
7\usepackage{color}"""
8# Specify the scaling factor 300%.
9options.scale = 3000
10# Specify the foreground color.
11options.text_color = Color.black
12# Specify the background color.
13options.background_color = Color.white
14# Specify the output stream for the log file.
15options.log_stream = BytesIO()
16# Specify whether to show the terminal output on the console or not.
17options.show_terminal = True
18
19# Create the output stream for the formula image.
20with open(path.join(Util.output_directory, "math-formula.png"), "wb") as stream:
21 # Run rendering.
22 size = PngMathRenderer().render(r"""\begin{equation*}
23e^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!}
24\end{equation*}""", stream, options)
25
26# Show other results.
27print(options.error_report)
28print()
29print(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()
方法,其中选项通过作为第三个参数。公式的乳胶代码作为第一个参数提供。
该示例的最后一行显示数学公式渲染过程的两个输出 - 公式的大小和简明错误报告,如果发生任何错误。
这是渲染的结果。
这代表了乳胶数学公式渲染功能的最常见和广泛应用。
您还可以探索免费的 网页应用,它利用了 Aspose.TeX for .NET API的实现功能。
将乳胶数学公式渲染为SVG
同样,我们可以使用相同的方法将乳胶数学公式转换为SVG格式。
1# Create rendering options.
2options = SvgMathRendererOptions()
3# Specify the preamble.
4options.preamble = r"""\usepackage{amsmath}
5\usepackage{amsfonts}
6\usepackage{amssymb}
7\usepackage{color}"""
8# Specify the scaling factor 300%.
9options.scale = 3000
10# Specify the foreground color.
11options.text_color = Color.black
12# Specify the background color.
13options.background_color = Color.white
14# Specify the output stream for the log file.
15options.log_stream = BytesIO()
16# Specify whether to show the terminal output on the console or not.
17options.show_terminal = True
18
19# Create the output stream for the formula image.
20with open(path.join(Util.output_directory, "math-formula.svg"), "wb") as stream:
21 # Run rendering.
22 size = SvgMathRenderer().render(r"""\begin{equation*}
23e^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!}
24\end{equation*}""", stream, options)
25
26# Show other results.
27print(options.error_report)
28print()
29print(f"Size: {size.width}x{size.height}")
差异是:
- 我们使用 svgmathrendereroptions类,而不是使用 pngmathrendereroptions类。
- 我们没有指定分辨率。
- 我们不使用 pngmathrenderer类,而是使用 svgmathrenderer类。
这是结果:
您还可以探索免费的 web 应用程序,它利用了 Aspose.TeX for .NET API 的实现功能。