C# 如何在 WPF 或 WinForms 中呈现公式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8899204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to render a formula in WPF or WinForms
提问by MajesticRa
Update 2018TL;DR; LaTEX for WPF https://github.com/ForNeVeR/wpf-math
2018 年更新TL;DR; 用于 WPF 的 LaTEX https://github.com/ForNeVeR/wpf-math
Original questionI need to have a way to draw a mathematical formula in Windows Forms or WPF. Some "FormulaTextBox" control would be exactly what I need.
原始问题我需要一种方法来在 Windows 窗体或 WPF 中绘制数学公式。一些“FormulaTextBox”控件正是我所需要的。
I'm not asking about full implementation of LaTEX, but at least something beyond RichTextBox possibilities, with division lines, square roots, etc.
我不是在问 LaTEX 的完整实现,而是至少在 RichTextBox 可能性之外的东西,包括分割线、平方根等。
采纳答案by BlueRaja - Danny Pflughoeft
Here's a list of options, pulled from several webpages online, as well as a fewsimilarquestionson SO
这是从几个在线网页中提取的选项列表,以及一些关于 SO 的类似问题
- WPF-Math, a WPF library for rendering math-related TeX markup.
- gNumeratoris a WinForms control that renders MathML. It is native C#, but appears to be quite old.
- Math Expressions, a commercial WinForms control for displaying and editing math equations. Note: Not free
- There's an unofficial port of JMathTexto a C# WPF control
- The Windows version of the LaTex editor Lyxuses a native library called MikTexyou could take a look at. I saw mention somewhere that the
tex4htpackage renders math equations to images - MimeTex/MathTex, as you already mentioned
- You could also use a
WebBrowsercontrol, and just locally include oneofmanyjavascriptlibrariesfor rendering LaTex. - You could pawn off the work onto Microsoft Word (Example- requires users to have MS Word installed!)
- WPF-Math,用于呈现与数学相关的 TeX 标记的 WPF 库。
- gNumerator是一个呈现MathML的 WinForms 控件。它是本机 C#,但似乎很旧。
- Math Expressions,一个用于显示和编辑数学方程的商业 WinForms 控件。 注意:不是免费的
- 有一个非官方的 JMathTex 端口到 C# WPF 控件
- LaTex 编辑器Lyx的 Windows 版本使用名为MikTex的本机库,您可以查看一下。我看到某处提到该
tex4ht包将数学方程渲染为图像 - MimeTex/ MathTex,正如你已经提到的
- 你也可以使用一个
WebBrowser控制,只是在本地有一个的很多的JavaScript库,用于渲染胶乳。 - 您可以将工作转移到 Microsoft Word 上(示例-要求用户安装 MS Word!)
回答by TJHeuvel
Perhaps you can use the Wolfram Alpha APIto retrieve the images.
也许您可以使用Wolfram Alpha API来检索图像。
回答by Jeremy Thompson
If you want something beyond a RichTextBox abilities to render pie, divisions and sqr roots and etc, you could just use WebBrowserControl(s) as textbox's. Then to render formula's you could leverage techniques shown in this webpage, save it to your desktop and open the html file:
如果你想要一些超越 RichTextBox 能力的东西来呈现饼图、分区和平方根等,你可以只使用 WebBrowserControl(s) 作为文本框的。然后要渲染公式,您可以利用此网页中显示的技术,将其保存到桌面并打开 html 文件:
http://www.wjagray.co.uk/maths/ASCIIMathTutorial.html
http://www.wjagray.co.uk/maths/ASCIIMathTutorial.html
Obviously you'll need a special syntax (or special calculator buttons) to enter the formula's and I'm guessing from looking at the customisations.js file driving that webpage that you could make your own list of operators and functions too.
显然,您需要一个特殊的语法(或特殊的计算器按钮)来输入公式,我猜测通过查看驱动该网页的 customisations.js 文件,您也可以制作自己的运算符和函数列表。
回答by JP Richardson
Here's what I'd do if none of the .NET specific solutions work for you:
如果 .NET 特定解决方案都不适合您,我会这样做:
It's a bit hacky, but it'll work and your users won't know the difference. Download the Mathjaxlibrary. When your user enters in the equation, you could convert the equation to LaTex or MathML. You would then take the LaTex or MathML and generate an HTML file that references Mathjax and display the file in your tiny WebBrowser window (valid for both WinForms and WPF). This shouldn't require an internet connection.
这有点 hacky,但它会起作用并且您的用户不会知道其中的区别。下载Mathjax库。当您的用户输入方程式时,您可以将方程式转换为 LaTex 或 MathML。然后,您将使用 LaTex 或 MathML 并生成一个引用 Mathjax 的 HTML 文件,并在您的微型 WebBrowser 窗口中显示该文件(对 WinForms 和 WPF 均有效)。这应该不需要互联网连接。
Like I said, your users won't be any the wiser.
就像我说的,你的用户不会更聪明。

