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.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Noldorin
  • 144,213
  • 56
  • 264
  • 302
  • 1
    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. – dmckee --- ex-moderator kitten Oct 01 '09 at 03:29
  • @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. – Noldorin Oct 01 '09 at 11:32
  • 1
    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. – Stephen Canon Oct 01 '09 at 14:22
  • @stephentyrone: dvipng would be an even better option, in my view. Either way, there's still a good deal of overhead involved. – Noldorin Oct 01 '09 at 16:19
  • 1
    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. – jdehaan Oct 02 '09 at 20:40
  • @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. :) – Noldorin Oct 02 '09 at 21:07

4 Answers4

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

zwcloud
  • 4,546
  • 3
  • 40
  • 69
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.

Noldorin
  • 144,213
  • 56
  • 264
  • 302
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.

nielsm
  • 350
  • 1
  • 8
  • Interesting suggestion. It might be worth compiling to C, and then wrapping in a managed C++/CLI library, I'm thinking. – Noldorin Oct 04 '09 at 10:27
  • Seems you are right. TeX is written in WEB, a variant of Pascal, then compiled into C using http://www.tug.org/web2c/. – Noldorin Oct 04 '09 at 10:50
-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);
  • 4
    you should explain your code and why it should be used, it makes it a lot more useful for future users – Kevin Jul 17 '19 at 12:19