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.
The icon's text and numbers are generated with this library itself.
Current release | Current pre-release | Commits since last version | Number of contributors |
---|---|---|---|
NuGet: GitHub: Date: |
NuGet: GitHub: Date: |
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 Example 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 |
---|---|---|
Typography project | CSharpMath.Rendering | MIT |
latin-modern-math font | CSharpMath.Ios, CSharpMath.Rendering | GUST Font License |
AMS-Capital-Blackboard-Bold font (extracted by @Happypig375 from the amsfonts package) | CSharpMath.Rendering | SIL Open Font License |
Thanks for reading.