乳胶数学公式渲染| .NET的Aspose.TeX
如何将乳胶数学公式渲染到PNG
与此主题相关的API参考部分是 此处。实际上,演示乳胶数学公式渲染功能的最简单方法是从示例开始。这里是:
1 // Render LaTeX math formula to PNG image
2
3 // Create rendering options setting the image resolution to 150 dpi.
4 PngMathRendererOptions options = new PngMathRendererOptions();
5 options.Resolution = 150;
6
7 // Specify the preamble.
8 options.Preamble = @"\usepackage{amsmath}
9 \usepackage{amsfonts}
10 \usepackage{amssymb}
11 \usepackage{color}";
12
13 // Specify the scaling factor 300%.
14 options.Scale = 3000;
15
16 // Specify the foreground color.
17 options.TextColor = System.Drawing.Color.Black;
18
19 // Specify the background color.
20 options.BackgroundColor = System.Drawing.Color.White;
21
22 // Specify the output stream for the log file.
23 options.LogStream = new System.IO.MemoryStream();
24
25 // Specify whether to show the terminal output on the console or not.
26 options.ShowTerminal = true;
27
28 // Create the output stream for the formula image.
29 using (System.IO.Stream stream = System.IO.File.Open(
30 System.IO.Path.Combine(OutputDir, "math-formula.png"), System.IO.FileMode.Create))
31 {
32 // Run rendering.
33 System.Drawing.SizeF size = new PngMathRenderer().Render(@"\begin{equation*}
34e^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!}
35\end{equation*}", stream, options);
36
37 // Show other results.
38 System.Console.Out.WriteLine(options.ErrorReport);
39 System.Console.Out.WriteLine();
40 System.Console.Out.WriteLine($"Size: {size}"); // Dimensions of the resulting image.
41 }让我们了解细节。首先,我们创建一个 渲染选项实例,类似于Tex/latex排版。我们在这里同时指定输出图像分辨率。
接下来,我们指定序言。默认序言是:
1\usepackage{amsmath}
2\usepackage{amsfonts}
3\usepackage{amssymb}与基本乳胶相比,它提供了更高级的数学公式支持。例如,如果您想在公式中使用自己的突出显示,则可以添加“颜色”软件包,如我们在代码示例中所示。
然后,我们指示渲染器将输出扩展300%。
接下来的两个选项定义了前景和背景颜色。自定义突出显示的未覆盖(“彩色”)的公式的那些部分将显示在TextColor颜色中。
该示例的下一行没有太多意义。它只是证明您可以将日志输出引导到某些流。
最后一个选项ShowTerminal使您可以切换将终端输出写入控制台。
实际执行渲染的方法是 Mathrenderer.render()。它返回点数中的公式大小。
该方法将要编写图像的流作为第二个参数接受。我们下一步创建流。
最后,我们将MathRenderer.render()方法本身称为第三个参数。公式的乳胶代码作为第一个参数传递。
示例的最后一行打印了数学公式渲染的两个工件 - 公式的大小和简短的错误报告(如果有错误)。
这是渲染的结果。
这是乳胶数学公式渲染功能最通用的用例。
您还可以查看基于 Aspose.TeX for .NET API 中实现的功能构建的免费 web 应用程序。
如何将乳胶数学公式渲染到SVG
以几乎相同的方式,我们可以将乳胶数学公式呈现为SVG格式。
1 // Render LaTeX math formula to SVG image
2
3 // Create rendering options.
4 MathRendererOptions options = new SvgMathRendererOptions();
5
6 // Specify the preamble.
7 options.Preamble = @"\usepackage{amsmath}
8 \usepackage{amsfonts}
9 \usepackage{amssymb}
10 \usepackage{color}";
11
12 // Specify the scaling factor 300%.
13 options.Scale = 3000;
14
15 // Specify the foreground color.
16 options.TextColor = System.Drawing.Color.Black;
17
18 // Specify the background color.
19 options.BackgroundColor = System.Drawing.Color.White;
20
21 // Specify the output stream for the log file.
22 options.LogStream = new System.IO.MemoryStream();
23
24 // Specify whether to show the terminal output on the console or not.
25 options.ShowTerminal = true;
26
27 // Create the output stream for the formula image.
28 using (System.IO.Stream stream = System.IO.File.Open(
29 System.IO.Path.Combine(OutputDir, "math-formula.svg"), System.IO.FileMode.Create))
30 {
31 // Run rendering.
32 System.Drawing.SizeF size = new SvgMathRenderer().Render(@"\begin{equation*}
33e^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!}
34\end{equation*}", stream, options);
35
36 // Show other results.
37 System.Console.Out.WriteLine(options.ErrorReport);
38 System.Console.Out.WriteLine();
39 System.Console.Out.WriteLine($"Size: {size}"); // Dimensions of the resulting image.
40 }差异是:
- 我们使用 SvgMathRendererOptions 类,而不是 PngMathRendererOptions。
- 我们不指定分辨率。
- 我们使用 SvgMathRenderer 类,而不是 PngMathRenderer。
这是结果:
您还可以查看基于 Aspose.TeX for .NET API 中实现的功能构建的免费 web 应用程序。
