Render Equations and Math Formulas using C#

LaTeX 是一种特殊的数学内容分类工具,使您能够轻松地嵌入公式和方程式。 德克斯 此源文档,由 LaTeX 创建,提供了一系列的命令格式化文本,符号,数学表达和图形. 在本文中,您将学习如何有效地使用 C# 进行 LaTex 方程式和数學公式,使用 Aspose TeX API,被广泛认可为 最好的 C # 圖書館的 LaTEX rendering。

内容表

C# API to Render LaTeX 方程式和数学公式

要做拉特克斯数学公式,我们将使用 ASPOSE.TEX 为 .NET API. 这个强大的 .NET LaTeX 方程式 rendering 图书馆允许您将 TeX 的文件分成各种格式,包括 PDF、XPS 或图像。

你也可以 下载DLL 通过 API 或安装它 诺基亚:

PM> Install-Package Aspose.TeX

Render LaTeX Inline Math Formulas 使用 C#

排序简单的数学公式或方程式是简单的,遵循以下步骤:

  • 创建 MathRendererOptions 类的例子。
  • 指定 LaTeX 文件提前列表。
  • 可选,调整特性如 尺寸, TextColor, BackgroundColor, 等等。
  • 创建公式图像的输出流。
  • 呼叫 Render() 方法,以便将公式交换,通过公式行、流、MathRenders 选项以及输出图像大小作为论点。

下面是一个代码样本展示 如何通过C#编程完成数学公式:

// This code example demonstrates how to render math formulas and equations
// Create rendering options specifying the image resolution 150 dpi
MathRendererOptions options = new PngMathRendererOptions() { Resolution = 150 };
// Specify the preamble.
options.Preamble = @"\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{color}";
// Specify the scaling factor 300%.
options.Scale = 3000;
// Specify the foreground color.
options.TextColor = System.Drawing.Color.Black;
// Specify the background color.
options.BackgroundColor = System.Drawing.Color.White;
// Specify the output stream for the log file.
options.LogStream = new MemoryStream();
// Specify whether to show the terminal output on the console or not.
options.ShowTerminal = true;
// The variable in which the dimensions of the resulting image will be written.
System.Drawing.SizeF size = new System.Drawing.SizeF();
// Create the output stream for the formula image.
using (Stream stream = File.Open(@"D:\Files\Tex\math-formula.png", FileMode.Create))
// Run rendering.
MathRenderer.Render(@"This is a sample formula $f(x) = x^2$ example.", stream, options, out size);
// Show other results.
System.Console.Out.WriteLine(options.ErrorReport);
System.Console.Out.WriteLine();
System.Console.Out.WriteLine("Size: " + size);
Render Inline Math Formulas using C#

Render Inline Math Formulas using C#

Render Complex LaTeX 等式在 C#

Rendering 复杂的 LaTeX 方程式跟随与以前相同的步骤,但在步骤 #4 中具有更复杂的公式序列:

MathRenderer.Render(@"\begin{equation*}
                e^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!}
                \end{equation*}", stream, options, out size);
Render Complex Equations in C#

Render Complex Equations in C#

显示长方程式在 C# {# 显示长方程式在 CSharp}

要在多个线上显示长方程式,请使用 LaTeX Multline 环境中使用以下方法:

MathRenderer.Render(@"\begin{document}
                    \begin{multline*}
	            p(x) = x^1+x^2+x^3+x^4\\ 
	            - x^4 - x^3 - x^2 - x
	            \end{multline*}
                    \end{document}", stream, options, out size);
Display Long Equations

Display Long Equations in C#

使用 C# 调整多个方程式

您可以通过在步骤 #4 中修改公式行来调整多个方程式:

MathRenderer.Render(@"\begin{document}
                         \begin{align*}
                         a+b   &  a-b   &  (a+b)(a-b)\\
                         x+y   &  x-y   &  (x+y)(x-y)\\
                         p+q   &  p-q   &  (p+q)(p-q)
                         \end{align*}
                         \end{document}", stream, options, out size);
Align Several Equations using C#

Align Several Equations using C#

组和中心方程式使用 C# {# 组和中心方程式使用 CSharp}

要集成和中心多个方程式,使用下列代码:

MathRenderer.Render(@"\begin{gather*} 
                (a+b)=a^2+b^2+2ab \\ 
                (a-b)=a^2+b^2-2ab \\ 
                (a-b)=a^2+b^2-2ab
                \end{gather*}", stream, options, out size);
Group and Center Equations using C#

Group and Center Equations using C#

Render Matrices, Parentheses, and Brackets in C# {#Render-Matrices,-Parenthesis,-and-Brackets-in-CSharp}

您可以以类似的方式进行矩阵、偏见和布拉克特:

MathRenderer.Render(@"\begin{document}
	            [ 
	            \left \{
	              \begin{tabular}{ccc}
	              1 & 4 & 7 \\
	              2 & 5 & 8 \\
	              3 & 6 & 9 
	              \end{tabular}
	            \right \}
	            ]
	            \end{document}", stream, options, out size);
Render Matrices, Parenthesis, and Brackets in C#

Render Matrices, Parenthesis, and Brackets in C#

Render Fractions and Binomials 使用 C#

要分裂和二元化,请遵循此结构:

MathRenderer.Render(@"\begin{document}
	            [
	                \binom{n}{k} = \frac{n!}{k!(n-k)!}
	            ]
	            \end{document}", stream, options, out size);
Render Fractions and Binomials using C#

Render Fractions and Binomials using C#

获得免费许可证

你可以 获得免费的临时许可证 查看图书馆,无限制。

结论

在这篇文章中,我们研究了如何:

  • 简单而复杂的数学公式和方程式在C#。
  • 以编程的方式调整和组方程。
  • Render matrices, parentheses, brackets, fractions, 和 binomials。

除了在 C# 中的 LaTeX 方程式和数学公式之外,您还可以深入了解。 ASPOSE.TEX 为 .NET 火焰,提到 人们在说什么如果您有任何疑问,请自由地在我们的 免费支持论坛.

使用 Aspose 插件,您可以在 C# 中实现高品质的 LaTeX 播放,并将 LaTeX 方程式集成到您的应用程序中,无论您使用 WinForms、 WPF 或 .NET MAUI。

此外,了解如何提供LaTeX是开发人员工作数学内容至关重要的。 使用适当的工具,如 C# LaTex,您可以轻松地将复杂的公式集成到您的项目中. 无论您对 La TeX 组方程式 感兴趣还是需要提供 Latex 长方位 的需求,本指南应为您提供有关 ** C# LateX** 。

More in this category