XamlMath.Shared
2.0.0
See the version list below for details.
dotnet add package XamlMath.Shared --version 2.0.0
NuGet\Install-Package XamlMath.Shared -Version 2.0.0
<PackageReference Include="XamlMath.Shared" Version="2.0.0" />
paket add XamlMath.Shared --version 2.0.0
#r "nuget: XamlMath.Shared, 2.0.0"
// Install XamlMath.Shared as a Cake Addin #addin nuget:?package=XamlMath.Shared&version=2.0.0 // Install XamlMath.Shared as a Cake Tool #tool nuget:?package=XamlMath.Shared&version=2.0.0
XAML-Math
XAML-Math is a collection of .NET libraries for rendering mathematical formulae using the LaTeX typesetting style, for the WPF and Avalonia XAML-based frameworks.
- For WPF, use NuGet package WPF-Math:
- For Avalonia, use Avalonia-Math:
WPF-Math supports the following .NET variants:
- .NET Framework 4.6.2 or later
- .NET 6 or later
Avalonia-Math supports:
- .NET Framework 4.6.2 or later
- .NET Standard 2.0 or later
- .NET 6 or later
A part of XAML-Math independent of the UI frameworks is published on NuGet as XAML-Math Shared Code:
Getting Started
The simplest way of using XAML-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>
<Window … xmlns:controls="clr-namespace:AvaloniaMath.Controls;assembly=AvaloniaMath">
<controls:FormulaBlock 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.
Using the rendering API
The following example demonstrates usage of TexFormula
API to render the image into a PNG file using the RenderToPng
extension method:
using System;
using System.IO;
using WpfMath.Parsers;
using WpfMath;
using XamlMath.Exceptions;
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";
try
{
var parser = WpfTeXFormulaParser.Instance;
var formula = parser.Parse(latex);
var pngBytes = formula.RenderToPng(20.0, 0.0, 0.0, "Arial");
File.WriteAllBytes(fileName, pngBytes);
}
catch (TexException e)
{
Console.Error.WriteLine("Error when parsing formula: " + e.Message);
}
}
}
}
Note that XamlMath.TexFormulaParser::Parse
may throw a XamlMath.Exceptions.TexException
if it was unable to parse a formula.
If you need any additional control over the image format, consider using the extension methods from the WpfTeXFormulaExtensions
class:
using System;
using System.IO;
using System.Windows.Media.Imaging;
using WpfMath.Parsers;
using WpfMath.Rendering;
using XamlMath;
const string latex = @"\frac{2+2}{2}";
const string fileName = @"T:\Temp\formula.png";
var parser = WpfTeXFormulaParser.Instance;
var formula = parser.Parse(latex);
var environment = WpfTeXEnvironment.Create(TexStyle.Display, 20.0, "Arial");
var bitmapSource = formula.RenderToBitmap(environment);
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 TeXFormulaExtensions::RenderTo
method if you need support for any alternate rendering engines.
Documentation
Build and Maintenance Instructions
Build the project using .NET SDK 7.0 or later. Here's the build and test script:
$ dotnet build XamlMath.All.sln --configuration Release
$ dotnet test XamlMath.All.sln
To approve the test results if they differ from the existing ones, execute the scripts/approve-all.ps1
script using PowerShell or PowerShell Core.
To publish the package, execute the following command:
$ dotnet pack XamlMath.All.sln --configuration Release
For non-Windows systems, substitute XamlMath.Portable.sln
instead of XamlMath.All.sln
.
History
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 2017, when was revived in its current form.
In 2023, after adding the Avalonia support, the WPF-Math project was renamed to XAML-Math.
License Notes
The project code and all the resources are distributed under the terms of MIT license.
The fonts cmex10.ttf
, cmmi10.ttf
, cmr10.ttf
, and cmsy10.ttf
and cmtt10.ttf
are under the Knuth License.
The font file jlm_msam10.ttf
(taken from JLaTeXMath project) is licensed under the Open Font License.
XAML-Math (named WPF-Math at the time) started as a direct port of JMathTeX project written in Java, reusing both code and resources. JMathTeX is distributed under the terms of GNU GPL v2 license. XAML-Math, being a derived work, has a permission from JMathTeX authors to be redistributed under the MIT license. See the Licensing history for the details.
We're very grateful to JMathTeX authors for their work and allowing to redistribute the derived library. JMathTeX is written by:
- Kris Coolsaet
- Nico Van Cleemput
- Kurt Vermeulen
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
-
net6.0
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on XamlMath.Shared:
Package | Downloads |
---|---|
WpfMath
.NET library for rendering mathematical formulae using the LaTeX typsetting style, for the WPF framework. |
|
AvaloniaMath
.NET library for rendering mathematical formulae using the LaTeX typsetting style, for the Avalonia framework. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Although a major release with certain formally breaking changes, these changes should hopefully not break any normal usage of the library (if the user code doesn't implement own IElementRenderer).
[Changed]
- (Breaking!) Avalonia: rename AvaloniaBrushExtensions to AvaloniaExtensions.
- (Breaking!) WPF: rename WpfBrushExtensions to WpfExtensions.
- (Breaking!) IElementRenderer is now required to implement a new method, RenderLine (required for the new \cancel* commands).
[Added]
- jlm_msam10.ttf font with a lot of new characters (https://github.com/ForNeVeR/xaml-math/pull/400), thanks @Orace, that closes the following requests:
- angle, measuredangle, and square symbols are not rendered (https://github.com/ForNeVeR/xaml-math/issues/127),
- An unsupported command \geqslant (https://github.com/ForNeVeR/xaml-math/issues/313).
- New commands: \cancel, \bcancel, \xcancel (partially addressing issue #349 (https://github.com/ForNeVeR/xaml-math/issues/349)), thanks @Orace.
[Fixed]
- #409: Exception on empty matrix row (https://github.com/ForNeVeR/xaml-math/issues/409).