问题
Background: Beginner, just started with C#
I'm making a C# WinForms application, on which I'd like to display well laid out mathematical expressions. To me, I would guess using latex would be the best way to do this. The issue I'm having is finding how latex can be rendered inside a winforms application. Searching around, I found FMath, however I can't get a simple working example of it functioning.
I've installed the library and the new control (MathMLFormulaControl) and added a control to the project. Upon the form loading, the following is done:
private void Form1_Load(object sender, EventArgs e)
{
fmath.controls.MathMLFormulaControl.setFolderUrlForFonts(@"C:\Program Files\MiKTeX 2.9\tex\latex");
fmath.controls.MathMLFormulaControl.setFolderUrlForGlyphs(@"C:\Program Files\MiKTeX 2.9\tex\latex");
mathMLFormulaControl1.Contents = "<math><mtext>New Formula</mtext></math>";
}
However, when trying to run this, the last line gives the error: "An unhandled exception of type 'fmath.ZP' occurred in WinMathML.dll". Looking at the details of the error (although not being able to understand it completely), I guessed it was because it could not find the latex font files, since there was the message: "Could not find file 'C:\Program Files\MiKTeX 2.9\tex\latex\fonts.properties'.". I've tried various folder locations, not just the one seen in the example, and still got the same error.
I've been googling for hours and haven't got anywhere, and being new to C# it's difficult to understand what these errors mean and how to rectify them.
Any help or direction in fixing this error, or finding a way to render latex inside a winforms application, would be greatly appreciated.
EDIT 1
Here is the stack trace of the exception:
at fmath.GN.aS(String jaa, String kaa)
at fmath.GN.XR(Boolean gaa, String nX)
at fmath.GN.UR(Z DX)
at fmath.MathMLFormula.drawFormula(String tX)
at fmath.controls.MathMLFormulaControl.BuildCanvas()
at fmath.controls.MathMLFormulaControl.set_Contents(String value)
at FMathTest.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\MyName\Documents\Visual Studio 2015\Projects\FMathTest\FMathTest\Form1.cs:line 24
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
回答1:
I've had the same problem, and here's a hack I used: (Btw I'm using VS 2015.) I got it to work by looking at the C# test project provided on fmath's download page: http://www.fmath.info/java/download.jsp. In the project's root directory, they have the two folders: fonts and glyphs. I copied these to my project's root directory, and my code (very similar to yours) worked.
On another note, when displaying Latex, ensure you set the latex flag to true before you set the Contents string:
mathMLFormulaControl1.latex = true;
mathMLFormulaControl1.Contents = @"\(x^2+3\)";
Otherwise a different exception is thrown!
来源:https://stackoverflow.com/questions/42528904/getting-latex-on-c-sharp-winforms