Ever wanted to display LaTeX math formulae in C# but cannot find any tool to help you? Here is what you may be looking for.
CSharpMath is a C# port of the wonderful iosMath LaTeX engine.
Current NuGet version | Current stable version | Commits since last version |
---|---|---|
Ironically enough, the first front end was iOS (CSharpMath.Ios). As development continued, Xamarin.Forms (CSharpMath.Forms) is now supported via SkiaSharp (CSharpMath.SkiaSharp) as of 0.1.0.
To get started, do something like this:
var latexView = IosMathLabels.MathView(@"x = -b \pm \frac{\sqrt{b^2-4ac}}{2a}", 15);
latexView.ContentInsets = new UIEdgeInsets(10, 10, 10, 10);
var size = latexView.SizeThatFits(new CoreGraphics.CGSize(370, 180));
latexView.Frame = new CoreGraphics.CGRect(0, 20, size.Width, size.Height);
someSuperview.Add(latexView);
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:math="clr-namespace:CSharpMath.Forms;assembly=CSharpMath.Forms"
x:Class="Namespace.Class">
<math:FormsMathView x:Name="View" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
\frac\sqrt23
</math:FormsMathView>
</ContentPage>
or you can use the prehistoric way:
var view = new FormsMathView();
view.HorizontalOptions = view.VerticalOptions = LayoutOptions.FillAndExpand;
view.LaTeX = @"\frac\sqrt23";
someLayout.Children.Add(view);
iOS | Android | Windows UWP |
---|---|---|
There are a few ways to extend this to more platforms: (Hopefully, you would not need to touch the core typesetting engine. If you do, we would consider that a bug.)
This path would require the most effort to implement, but allows you to plug in any font library and graphics library.
You would have to define your own TypesettingContext and write an implementation of IGraphicsContext.
The TypesettingContext in turn has several components, including choosing a font.
As CSharpMath.Rendering provides font lookup through the Typography library, you would only need to write adapter classes to connect this library to your chosen graphics library.
You would have to implement ICanvas and feed it into the Draw method of MathPainter.
You can extend this library to other SkiaSharp-supported platforms by feeding the SKCanvas given in the OnPaintSurface override of a SkiaSharp view into the Draw method of SkiaMathPainter.
You can use this library on other appleOSes by making use of AppleMathView.
We need more contributors! Maybe you can contribute something to this repository. Whether they are bug reports, feature proposals or pull requests, you are welcome to send them to us. We are sure that we will take a look at them!
Here is an idea list if you cannot think of anything right now:
- A new example for the Exmple projects (please open pull requests straight away)
- A new LaTeX command (please link documentation of it)
- A new front end (please describe what it is and why should it be supported)
- A new math syntax (please describe what it is and why should it be supported)
CSharpMath is licensed by the MIT license.
Dependency | Used by | License |
---|---|---|
latin-modern-math font | CSharpMath.Ios, CSharpMath.Rendering | GUST Font License |
Typography project | CSharpMath.Rendering | MIT |
Thanks for reading.
You can take a look at the code now.
Really, there is nothing here.
I bet you scrolled past and came back to read me.
Will you stop scrolling?
Ok, fine... I give up.
Shhh... Don't tell anybody!
0.2.0: MathML? 0.3.0: AsciiMath? 0.4.0: Infix?
0.4.0: Math evaluation?? 0.5.0: Handwritten math recognition???