6

I'm searching in vain to find a control or DLL which allows me to render Latex code within a C# winforms project.

(Most search terms seem to assume I'm trying to render C# code within Latex...)

I plan to build Expressions & convert to Latex code then render on screen on the fly.

Any suggestions?

2 Answers2

9

The following might help you. Feel free to edit my code to accommodate all known best practices.

Step 0

Make sure you have installed LaTeX distro (either TeX Live or MikTeX) and ImageMagick. Register the ImageMagick's path to the System Path such that convert command is available everywhere.

Step 1

Create a new WinForm project in C# (or VB). Drag one RichTextBox, one Button and one PictureBox onto the Form. Write the code behind as follows.

using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
namespace LaTeXEditor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            File.WriteAllText("input.tex", richTextBox1.Text);

            Process p1 = new Process();
            p1.StartInfo.FileName = "batch.bat";
            p1.StartInfo.Arguments = "input";
            p1.StartInfo.UseShellExecute = false;

            p1.Start();
            p1.WaitForExit();

            pictureBox1.ImageLocation = "output.png";    
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            richTextBox1.Text = 
@"\documentclass[preview,border=12pt]{standalone}
\usepackage{amsmath}
\usepackage{tikz}

\begin{document}
PSTricks is more powerful than this one\\ \tikz\draw[red] (0,0) circle (1);
\end{document}
";
        }
    }
}

Step 2

Add a new file named batch.bat to the project. The hard-coded literals should be avoided if necessary by defining additional parameters to the batch file.

rem batch.bat
rem %1 represents the file name with no extension.
pdflatex -jobname=output %1
convert -density 200 -alpha on output.pdf output.png

Modify the post build event as follows to copy the batch.bat to the project output directory.

enter image description here

Step 3

Compile and make a try as follows.

enter image description here

3

Step 1 create a new Wpf-app, link Wpf-math, use Nuget Browser

enter image description here

Step 2. ... and modify the MainWindow() as follows

using WpfMath.Converters;

namespace WpfMathExample { public partial class MainWindow : Window { const string latex = @"\frac{2+2}{2}"; const string fileName = @"formula.png";

 public MainWindow()
 {
    InitializeComponent();

    // modification starts here..

    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);
    }
}

} }

After the run, you will have a .png file next to your .exe in your debug directory, which looks as follows:

enter image description here

(tested 05-feb-2022 VS2019, WPF .Net5)

Goodies
  • 103
  • It is unclear how this snippet is useful, could you consider making it detailed (-1)? Nevertheless, welcome to TEXSE. – Raaja_is_at_topanswers.xyz Jul 17 '19 at 12:03
  • 1
    this is c# code, i use all functions with their root libraries so that it is not important any warning such as write using Wpf; or Using System.Windows.Media.Imaging;. Also comment line is not where anyone says welcome as i know, please ask me detailed:) your question if you do not understand in 12 lines – Turan Tuğsan Yeşil Jul 17 '19 at 12:24
  • Is the code compilable as it is now? and welcoming is just so that you feel welcomed to the site :) – Raaja_is_at_topanswers.xyz Jul 17 '19 at 12:28
  • 1
    yes dude it is compilable, just you must copy and paste it a button click event function to test it. i want to just send the basic functions orderly. But i am sorry for i paste it as code but site does not show it colored it seems so plain, i can not figured it out:) – Turan Tuğsan Yeşil Jul 17 '19 at 12:39
  • I do not have extensive knowledge in C#, if you can make a minor edit, I can retract my downvote (that is how the site is designed, sorry). – Raaja_is_at_topanswers.xyz Jul 17 '19 at 12:41
  • 1
    https://github.com/ForNeVeR/wpf-math/issues/118 okay you can look at this link. i found it first. also i'll make a edit for more info – Turan Tuğsan Yeşil Jul 17 '19 at 12:45
  • 1
    Please have a look to the other answer to this question. Take it as example and edit your answer to give an step by step advice what one has to do to be able to use your code. Can you please also add an screenshot of the result? – Mensch Jul 17 '19 at 13:02
  • It still works ! I've put some context in this answer, to improve it. – Goodies Feb 05 '22 at 15:54