# How to incorporate TeX into a website?

How should I incorporate TeX in a website to render mathematical formulas? If there are multiple ways, what is the easiest way?

-
What do you mean? You want LaTeX-rendered maths on a website? You want to be able to compile LaTeX online? –  Seamus Jul 22 '11 at 12:30
Yeah @Seamus, I want to render math on a website. :) –  Saeed Neamati Jul 22 '11 at 12:32
For WordPress blogs have a look at QuickLaTeX. It seems also support other forms of online sites. –  Martin Scharrer Jul 22 '11 at 12:45
Exactly duplicate How to use Latex on blogspot? –  xport Jul 22 '11 at 12:55
@xport: Not really. There's a difference between blogspot (i.e. no access to the server in any way) and general websites (i.e. possibly ftp/ssh or even physical access to the server). –  You Jul 22 '11 at 13:03

To display mathematics on the web, you have a number of options. There is the MathML standard which has the advantage of being endorsed by the same body that maintains the HTML and CSS standards. This isn't however, LaTeX based. There are tools to translate LaTeX code into MathML.

There is the "heavy duty" MathJax option. You can either install this on your server or use their CDN.

The simplest approach might be to use something like codecogs to produce images that you just include with html img tags...

-
You don't have to install MathJax on your own server; you could use the MathJax CDN instead. –  You Jul 22 '11 at 13:01
Using CDN does simply your server settings as well where you don't need to setup MIME for fonts used in the MathJax library. –  xport Jul 22 '11 at 13:09

here you found an example for MathJaX:

http://latex.userpage.fu-berlin.de/math.html

the source code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<title>Mathedemo</title>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\$','\$']]}});
</script>
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

<body>
<h2>Math in TeX notation</h2>

When $a \ne 0$, there are two solutions to $ax^2 + bx + c = 0$ and they are
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$
$$\begin{array}{rcll} y & = & x^{2}+bx+c\\ & = & x^{2}+2\times\dfrac{b}{2}x+c\\ & = & \underbrace{x^{2}+2\times\dfrac{b}{2}x+\left(\frac{b}{2}\right)^{2}}- {\left(\dfrac{b}{2}\right)^{2}+c}\\ & & \qquad\left(x+{\dfrac{b}{2}}\right)^{2}\\ & = & \left(x+\dfrac{b}{2}\right)^{2}-\left(\dfrac{b}{2}\right)^{2}+c & \left|+\left({\dfrac{b}{2}}\right)^{2}-c\right.\\ y+\left(\dfrac{b}{2}\right)^{2}-c & = & \left(x+ \dfrac{b}{2}\right)^{2} & \left|\strut(\textrm{vertex form})\right.\\ y-y_{S} & = & (x-x_{S})^{2}\\ S(x_{S};y_{S}) & \,\textrm{or}\, & S\left(-\dfrac{b}{2};\,\left(\dfrac{b}{2}\right)^{2}-c\right) \end{array}$$

<h2>Math in MathML notation</h2>

When $<mi>a</mi><mo>&#x2260;</mo><mn>0</mn>$,
there are two solutions to $<mi>a</mi><msup><mi>x</mi><mn>2</mn></msup> <mo>+</mo> <mi>b</mi><mi>x</mi> <mo>+</mo> <mi>c</mi> <mo>=</mo> <mn>0</mn>$ and they are
<math mode="display">
<mi>x</mi> <mo>=</mo>
<mrow>
<mfrac>
<mrow>
<mo>&#x2212;</mo>
<mi>b</mi>
<mo>&#x00B1;</mo>
<msqrt>
<msup><mi>b</mi><mn>2</mn></msup>
<mo>&#x2212;</mo>
<mn>4</mn><mi>a</mi><mi>c</mi>
</msqrt>
</mrow>
<mrow> <mn>2</mn><mi>a</mi> </mrow>
</mfrac>
</mrow>
<mtext>.</mtext>
[/itex]
</body>
</html>

-
Unfortunately, \cancel, \bcancel, etc don't exist in MathJax. :-( –  xport Jul 22 '11 at 13:05
there are no such commands in the default math mode –  Herbert Jul 22 '11 at 13:07

I'll add two math rendering alternatives only for reference purposes. Personally, I prefer to stick with either MathML or MathJaX.

Both MimeTeX and MathTeX are cgi programs written in C.

MimeTeX

MimeTeX, licensed under the gpl, lets you easily embed LaTeX math in your html pages. It parses a LaTeX math expression and immediately emits the corresponding gif image, rather than the usual TeX dvi. And mimeTeX is an entirely separate little program that doesn't use TeX or its fonts in any way. It's just one cgi that you put in your site's cgi-bin/ directory, with no other dependencies. So mimeTeX is very easy to install. And it's equally easy to use. Just place an html <img> tag in your document wherever you want to see the corresponding LaTeX expression. For example,

<img src="../cgi-bin/mimetex.cgi?f(x)=\int_{-\infty}^xe^{-t^2}dt"
alt="" border=0 align=middle>


immediately generates the corresponding gif image on-the-fly, displaying wherever you put that <img> tag. MimeTeX doesn't need intermediate dvi-to-gif conversion, and doesn't create separate gif files for each converted expression. (But you can cache images with mimeTeX's -DCACHEPATH=\"path/\" compile option.)

MathTeX

MathTeX, licensed under the gpl, is a cgi program that lets you easily embed LaTeX math in your own html pages, blogs, wikis, etc. It parses a LaTeX math expression and immediately emits the corresponding gif (or png) image, rather than the usual TeX dvi. So just place an html <img> tag in your document wherever you want to see the corresponding LaTeX expression. For example,

<img src="/cgi-bin/mathtex.cgi?f(x)=\int_{-\infty}^xe^{-t^2}dt"
alt="" border=0 align="middle">


immediately generates the corresponding gif, displaying wherever you put that <img> tag.

mathTeX dependencies

MathTeX's uses the latex and dvipng programs, along with all necessary fonts, etc, from your TeX distribution. Occasionally, you may need to download dvipng separately. If you can't, or don't want to, install dvipng, then you may optionally specify the –DDVIPS and –DCONVERT switches when compiling mathTeX. Then mathTeX uses dvips from your TeX distribution, and convert from the ImageMagick package, instead of dvipng.

[...]

These dependencies - always latex and either dvipng or dvips/convert - must all be installed on your server before you can run mathTeX. Ask your ISP or sysadmin if you have any questions or problems installing them. Or see mimeTeX if you can't install them.

-
1. If you just want to render LaTeX commands as HTML output, then Herbert's answer is the correct way to go, i.e., using MathJax library provided by some CDN. Using CDN is very useful since you don't need to host MathJax library by yourself, you don't need to update MathJax library, you don't need to setup MIME on your server to allow MathJax fonts passing through the web server pipeline, etc.
2. But if you want to render LaTeX commands as PDF output, then MathJax will not help you! Depending on the content, whether you use EPS, JPEG, PNG, PDF in you LaTeX input file, you need to setup a server script to execute latex.exe or pdflatex.exe and other tools to get PDF output. This workflow is not for a newbie because you have to know a programming language for writing server script. If you are a C# user and know ASP.NET MVC, the following incomplete code snippet may help you to get the rough idea. But it has not been optimized and analyzed for any security vulnerability.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Problem problem )
{
ViewBag.Message = "LaTeX to PDF Converter";

string dir = Server.MapPath("~/Content/");
name = Path.GetRandomFileName() + MvcApplication.rnd.Next(int.MaxValue);
string inputpath = Path.Combine(dir, name + ".tex");

using (StreamWriter sw = new StreamWriter(inputpath))
{
sw.Write(problem.Description);
}

Process p = new Process();

p.EnableRaisingEvents = true;
p.Exited += new EventHandler(p_Exited);

p.StartInfo.Arguments = "-interaction=nonstopmode " + inputpath;
p.StartInfo.WorkingDirectory = dir;

p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "pdflatex.exe";

p.Start();
p.WaitForExit();

if (p.ExitCode == 0)
{

TempData["outputpath"] = Url.Content("~/Content/" + name + ".pdf");

return RedirectToAction("Result");
}
else
{
return View(problem);
}
}

void p_Exited(object sender, EventArgs e)
{
Process p = sender as Process;
string dir = Server.MapPath("~/Content/");
using (StreamWriter sw = new StreamWriter(Path.Combine(dir, "log.txt"), true))
{
sw.WriteLine(DateTime.Now);
}

var filenames = Directory.GetFiles(dir, name + "*");
for (int x = 0; x < filenames.Length; x++)
{
if (Path.GetExtension(filenames[x]) != ".pdf" || p.ExitCode != 0)
System.IO.File.Delete(filenames[x]);
}

}


You need to create a separate process to invoke pdflatex to compile the input file.

-

There is also the possibility to use LateXMathML, which converts LaTeX Math Mode to MathML with JavaScript only. No serverside installation needed.

-