WPF-Math is a .NET library for rendering mathematical formulae using the LaTeX typsetting style, for the WPF framework.
The simplest way of using WPF-Math is to render a static formula in a XAML file as follows.
<Window ... xmlns:controls="clr-namespace:WpfMath.Controls;assembly=WpfMath">
<controls:FormulaControl Formula="\left(x^2 + 2 \cdot x + 2\right) = 0" />
</Window>
For a more detailed sample, check out the example project. It shows the usage of data binding and some advanced concepts.
The following example demonstrates usage of TexFormula
API to render the image into a PNG file using the RenderToPng
extension method:
using System.IO;
using WpfMath;
namespace ConsoleApplication2
{
internal class Program
{
public static void Main(string[] args)
{
const string latex = @"\frac{2+2}{2}";
const string fileName = @"T:\Temp\formula.png";
var parser = new TexFormulaParser();
var formula = parser.Parse(latex);
var pngBytes = formula.RenderToPng(20.0, 0.0, 0.0, "Arial");
File.WriteAllBytes(fileName, pngBytes);
}
}
}
If you need any additional control over the image format, consider using the GetRenderer
API:
using System;
using System.IO;
using System.Windows.Media.Imaging;
using WpfMath;
namespace ConsoleApplication2
{
internal class Program
{
public static void Main(string[] args)
{
const string latex = @"\frac{2+2}{2}";
const string fileName = @"T:\Temp\formula.png";
var parser = new TexFormulaParser();
var formula = parser.Parse(latex);
var renderer = formula.GetRenderer(TexStyle.Display, 20.0, "Arial");
var bitmapSource = renderer.RenderToBitmap(0.0, 0.0);
Console.WriteLine($"Image width: {bitmapSource.Width}");
Console.WriteLine($"Image height: {bitmapSource.Height}");
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
using (var target = new FileStream(fileName, FileMode.Create))
{
encoder.Save(target);
Console.WriteLine($"File saved to {fileName}");
}
}
}
}
You may also pass your own IElementRenderer
implementation to TexFormula.RenderFormulaTo
method if you need support for any alternate rendering engines.
The library was originally ported from the JMathTex project, copyright 2004-2007 Universiteit Gent. The port was originally named WPF-TeX and was written and maintained by Alex Regueiro. It was later available as WPF-Math on Launchpad, but was unmaintained from 2011 until it was revived in its current form.
The project code is licensed under the terms of MIT license. The original resources from JMathTeX (DefaultTexFont.xml
, GlueSettings.xml
, PredefinedTexFormulas.xml
, TexFormulaSettings.xml
, TexSymbols.xml
) are taken from the GPLv2-distributed JMathTeX, but JMathTeX authors have granted permission to redistribute these resourses under the MIT license. See the wiki for details.
The fonts cmex10.ttf
, cmmi10.ttf
, cmr10.ttf
, and cmsy10.ttf
and cmtt10.ttf
are under the Knuth License.