verybadcat / CSharpMath

LaTeX. in C#. (ported from the wonderful iosMath project).
MIT License
384 stars 64 forks source link

How to display a mathematical passage with CSharpMath? #24

Closed sadqiang closed 5 years ago

sadqiang commented 5 years ago

If I am correct, the purpose of CSharpMath is to display math equation without using MathJax or KaTeX (that works only on web view control/widget). My question is how to display a mathematical passage in which there are some texts surrounding equations without a web view control? CSharpMath is not appropriate for this scenario?

Happypig375 commented 5 years ago

You can use TextPainter/TextView. They are only on SkiaSharp/Xamarin.Forms right now, and here is some sample code: For SkiaSharp:

var painter = new CSharpMath.SkiaSharp.TextPainter
{
    Text = @"Some Text $\frac23-5$ Auto line breaks $$\int^4_3$$ Some Text",
    HighlightColor = SkiaSharp.SKColors.LightBlue
};
using (var surface = SkiaSharp.SKSurface.Create(new SkiaSharp.SKImageInfo(200, 200))) {
    painter.Draw(surface.Canvas);
    using (var snapshot = surface.Snapshot())
    using (var pngData = snapshot.Encode())
    using (var png = pngData.AsStream())
    using (var file = System.IO.File.OpenWrite(@"C:\Some\Path\Output.png")) //replace path
        png.CopyTo(file);
}

out For Xamarin.Forms:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:math="clr-namespace:CSharpMath.Forms;assembly=CSharpMath.Forms"
             x:Class="CSharpMathTextPainterDemo.Forms.MainPage"> <!-- Replace namespace -->
    <math:TextView LaTeX="Text $3+4+5+6\cdot7^{5x^2}+a_n$ More text $$\int^5_2x^3+x^2-x-3dx$$ Text"/>
</ContentPage>

image

Happypig375 commented 5 years ago

You can also use text commands such as \par and accent commands. Besides $ and $$ shown above, you can also use \(, \), \[ and \] if you prefer the LaTeX-style commands instead of TeX ones.

sadqiang commented 5 years ago

@Happypig375 : Thank you very much. It is extremely interesting. Do you have a plan to accommodate Asymptote as well so we can draw mathematical diagrams without hustle ? 🥇

Happypig375 commented 5 years ago

Thank you very much. It is extremely interesting.

Thanks for checking CSharpMath out, too!

Asymptote

I don't understand it. The only definition of an "Asymptote" is geometrical, which doesn't make sense in this sentence.

so we can draw mathematical diagrams without hustle

If you mean "plotting" by "draw mathematical diagrams", then I'd use Oxyplot if I were you.

🥇

Okay... Guess I'll take it. Thanks, I guess.

sadqiang commented 5 years ago

Thank you for replying. Here is the good link to know about Asymptote. It is often used in mathematical-oriented web sites. We can enter asymptote code (looks like C++) to input geometrical diagrams.

By the way, I will explore the CSharpMath wether or not it fits to my requirement.

Happypig375 commented 5 years ago

At the bottom of the webpage...

Codes under the terms of the GNU LGPL.

Looks like it has a copyleft license, which I don't like.

Happypig375 commented 5 years ago

Actually, you'd be much, much, much, ..., much better off using SkiaSharp as a drawing library directly. Not only because it supports C# directly and is backed by Xamarin (and by extension, Microsoft), but it covers the same area as Asymptote - a graphics library. There would be no point in porting Asymptote only to compete with SkiaSharp.

Anyways, question answered and accepted. Closing.

sadqiang commented 5 years ago

Sorry. Does SkiaSharp have 3D rendering capabilities?

Happypig375 commented 5 years ago

For 3D drawing instead of 2D, please see UrhoSharp which is another Xamarin-backed project

sadqiang commented 5 years ago

Thank you for replying.

sadqiang commented 5 years ago

I have one more question: Is it also possible to \includegraphics in math:TextView?

charlesroddie commented 5 years ago

No. Again use SkiaSharp directly for this.

sadqiang commented 5 years ago

Ok. Thank you.

sadqiang commented 5 years ago

@charlesroddie : If I have a bunch of images that are already in PDF or PNG, do I have to recreate them with SkiaSharp? (I have no clear idea about SkiaSharp, sorry)

Happypig375 commented 5 years ago

PNG Sample here