LaTeX renderer for .NET?
Asked Answered
B

4

17

I'm curious as to whether a native .NET renderer for TeX/LaTeX exists. The closest match I have been able to find is a Java implementation, JMathTeX. I am tempted to port this to C#, but before I do so, I would simply like to check whether anyone is aware of a .NET implementation out there.

My current thoughts are to use MiKTeX along with dvipng to compile the TeX source and render the generated DVI as a PNG, but I'm still worrying this may incur an unacceptable amount of overhead, not to mention the need to bundle MiKTeX with the given program.

Binetta answered 1/10, 2009 at 0:32 Comment(6)
I find myself wondering why you would do this. Knuth's version is pretty near bug-free (he offers a cash prize for confirmed bugs, and it hasn't been claimed in quite a while). A re-write is not likely to be that good.Bulimia
@dmckee: The alternative for rendering within .NET apps is to use a compiler such as MiKTeX and then dvipng to convert it into a PNG. This is a rather indirect method, and I was hoping for something simpler.Binetta
I would expect it to be a better alternative to use the (excellent) pdf[la]tex, as the result would then already be a (relatively) easily displayed format.Interrelated
@stephentyrone: dvipng would be an even better option, in my view. Either way, there's still a good deal of overhead involved.Binetta
One use case would be to display mathematical formula inside an application window without installing a hundred MB of latex stuff :-), a lightweight online math formatting. If you decide to go on with porting, I volunteer to help.Snuffle
@jdehann: Indeed, that would be the main reason. Bundling MiKTeX with your program isn't terribly convenient. I'm still mulling this over - but thanks for your offer of help - will let you know what I decide. :)Binetta
B
2

If I'm not mistaken, TeX is written in a dialect of Pascal, and when compiled today it's generally first compiled into standard C, then compiled with a C compiler to produce the final binary. It might be feasible to instead compile the original Pascal code into C# and perhaps write a wrapper around it in C# to be able to use it as a library.

Of course this is a rather large project to take on and is probably overkill for your problem at hand.

Bjorn answered 4/10, 2009 at 1:14 Comment(2)
Interesting suggestion. It might be worth compiling to C, and then wrapping in a managed C++/CLI library, I'm thinking.Binetta
Seems you are right. TeX is written in WEB, a variant of Pascal, then compiled into C using tug.org/web2c.Binetta
M
6

A pure C# implementation of Latex by verybadcat.

This is a C# port of the wonderful iosMath LaTeX engine.

It is now working in most cases. Some examples are below. Ironically enough, the first front end is iOS. However, if you want to add a front end, such as Xamarin.Forms or a Windows environment, it should be possible. 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. Hopefully, you would not need to touch the core typesetting engine. (If you do, I would consider that a bug.)

CSharpMath example

Mathewson answered 5/2, 2018 at 5:18 Comment(0)
B
5

This is very overdue, but I thought I'd post a link to a revived and expanded port of the WPF-Math project, which I started not too long after this original question, and still help maintain in a minor capacity. It was originally a port of JMathTex, but has since expanded to include a lot more. At the moment, it's in the process of gaining renderers other than WPF.

Binetta answered 6/2, 2018 at 1:16 Comment(0)
B
2

If I'm not mistaken, TeX is written in a dialect of Pascal, and when compiled today it's generally first compiled into standard C, then compiled with a C compiler to produce the final binary. It might be feasible to instead compile the original Pascal code into C# and perhaps write a wrapper around it in C# to be able to use it as a library.

Of course this is a rather large project to take on and is probably overkill for your problem at hand.

Bjorn answered 4/10, 2009 at 1:14 Comment(2)
Interesting suggestion. It might be worth compiling to C, and then wrapping in a managed C++/CLI library, I'm thinking.Binetta
Seems you are right. TeX is written in WEB, a variant of Pascal, then compiled into C using tug.org/web2c.Binetta
S
-2

use this code!!!

        const string latex = @"\frac{2+2}{2}";
        const string fileName = @"formula.png";

        File.Open(fileName, FileMode.OpenOrCreate).Close();

        var parser = new WpfMath.TexFormulaParser();
        var formula = parser.Parse(latex);
        var renderer = formula.GetRenderer(WpfMath.TexStyle.Display, 20.0, "Arial");
        var bitmapSource = renderer.RenderToBitmap(0, 0);

        var encoder = new System.Windows.Media.Imaging.PngBitmapEncoder();
        encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bitmapSource));
        using (var target = new FileStream(fileName, FileMode.Create))
        {
            encoder.Save(target);
        }

        pictureBox2.Image = Image.FromFile(fileName);
Synchroscope answered 17/7, 2019 at 12:14 Comment(1)
you should explain your code and why it should be used, it makes it a lot more useful for future usersGrunion

© 2022 - 2024 — McMap. All rights reserved.