# C# tool to render latex in Winforms or PNG/BMP/etc?

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?

-
It seems that MathJax or native MathML is suitable. –  Leo Liu Feb 8 '14 at 5:35
Can't you write a wrapper DLL, which encapsules the LaTeX calls and takes care of any necessary format conversions? –  Uwe Ziegenhagen Feb 8 '14 at 7:53

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.

## Step 3

Compile and make a try as follows.

-
It is just a modification of my previous answer several decades ago. –  kiss my armpit Feb 8 '14 at 8:05
Thanks for the comprehensive answer! I am going to try integrating Mathjax in an embedded browser first but will definitely try this next. –  Brendan Hill Feb 8 '14 at 9:09