乳胶数学公式渲染| .NET
如何将乳胶数学公式渲染到PNG
与此主题相关的API参考部分是 此处。实际上,演示乳胶数学公式渲染功能的最简单方法是从示例开始。这里是:
1// Create rendering options setting the image resolution to 150 dpi.
2PngMathRendererOptions options = new PngMathRendererOptions();
3options.Resolution = 150;
4// Specify the preamble.
5options.Preamble = @"\usepackage{amsmath}
6\usepackage{amsfonts}
7\usepackage{amssymb}
8\usepackage{color}";
9// Specify the scaling factor 300%.
10options.Scale = 3000;
11// Specify the foreground color.
12options.TextColor = System.Drawing.Color.Black;
13// Specify the background color.
14options.BackgroundColor = System.Drawing.Color.White;
15// Specify the output stream for the log file.
16options.LogStream = new System.IO.MemoryStream();
17// Specify whether to show the terminal output on the console or not.
18options.ShowTerminal = true;
19
20// Create the output stream for the formula image.
21using (System.IO.Stream stream = System.IO.File.Open(
22 System.IO.Path.Combine(RunExamples.OutputDirectory, "math-formula.png"), System.IO.FileMode.Create))
23{
24 // Run rendering.
25 System.Drawing.SizeF size = new PngMathRenderer().Render(@"\begin{equation*}
26e^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!}
27\end{equation*}", stream, options);
28
29 // Show other results.
30 System.Console.Out.WriteLine(options.ErrorReport);
31 System.Console.Out.WriteLine();
32 System.Console.Out.WriteLine("Size: " + size); // Dimensions of the resulting image.
33}
让我们了解细节。首先,我们创建一个 渲染选项实例,类似于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// Create rendering options.
2MathRendererOptions options = new SvgMathRendererOptions();
3// Specify the preamble.
4options.Preamble = @"\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.TextColor = System.Drawing.Color.Black;
12// Specify the background color.
13options.BackgroundColor = System.Drawing.Color.White;
14// Specify the output stream for the log file.
15options.LogStream = new System.IO.MemoryStream();
16// Specify whether to show the terminal output on the console or not.
17options.ShowTerminal = true;
18
19// Create the output stream for the formula image.
20using (System.IO.Stream stream = System.IO.File.Open(
21 System.IO.Path.Combine(RunExamples.OutputDirectory, "math-formula.svg"), System.IO.FileMode.Create))
22{
23 // Run rendering.
24 System.Drawing.SizeF size = new SvgMathRenderer().Render(@"\begin{equation*}
25e^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!}
26\end{equation*}", stream, options);
27
28 // Show other results.
29 System.Console.Out.WriteLine(options.ErrorReport);
30 System.Console.Out.WriteLine();
31 System.Console.Out.WriteLine("Size: " + size); // Dimensions of the resulting image.
32}
差异是:
- 我们使用 SvgMathRendererOptions 类,而不是 PngMathRendererOptions。
- 我们不指定分辨率。
- 我们使用 SvgMathRenderer 类,而不是 PngMathRenderer。
这是结果:
您还可以查看基于 Aspose.TeX for .NET API 中实现的功能构建的免费 web 应用程序。