Take the 2-minute tour ×

I've been using Markdown for class notes, and it's great. I even do some preprocessing on the Markdown so I can do things like tables. But this term I'm teaching a class with a lot of math, and I'd love to be able to put LaTeX formulas with Markdown, something like this:

The refinement relation is written $a \sqsubseteq b$, which can be 
pronounced "$a$ approximates $b$" or "$b$ is at least as defined as $a$".

I'd like to be able to take each fragment of LaTeX and preprocess it into a nice antialiased PNG file which I could then include in my Markdown via the HTML <img> tag. But I have absolutely no idea how to take a fragment of LaTeX and get a nice image that

  • Has the right bounding box
  • Is antialiased

All I know how to do is get full pages in DVI, PostScript, or PDF formats.

I'm sure this problem has been addressed, but I haven't been able to guess the right search terms. Any suggestions how to solve it or where to look for an existing solution?

EDIT: Having installed mathTeX, I can say that the code is inflexible, that it violates the Linux Filesystem Hierarchy standard, and that it is amateur work—in both the good and bad senses of that word. The code is so complex that there are no obvious faults. I will be looking for alternatives.

Also, it's clear that at bottom, solutions are based on dvipng.

ONE YEAR LATER: I never did get the seamless integration I had been hoping for, but I am limping along on a script of my own devising. It turns out that instead of dvipng it is a little easier to use dvips -E and the convert program of ImageMagick. The benefits are slightly more control of things like scaling, and ease of making a transparent background. The curious can inspect this example.

I can't recommend this solution to anyone. But I can't recommend MathTeX either.

share|improve this question
Good questions. I've done my preparation directly in LaTeX, but it is a little wordy for that purpose, even using auctex... –  dmckee Feb 3 '10 at 0:41
Did you try Pandoc? Not only does it itself convert Markdown+LaTeX to (whatever), but also, with its latest release, it lets you write scripts that work on the parse tree, so you could easily do what you want. –  ShreevatsaR Apr 23 '10 at 21:51
@ShreevatsaR I'm going to try both pandoc and multimarkdown, probably this summer. –  Norman Ramsey Apr 23 '10 at 22:16
so complex that there are no obvious faults - I missed this pleasing reference first time I read it. –  Charles Stewart Jan 18 '11 at 7:13
Don't know if this link is any help: charlietanksley.net/philtex/primarily-pandoc –  Benjol Sep 9 '11 at 6:01
show 4 more comments

closed as off topic by animuson, Peter O., Praveen Kumar, Lafada, Matteo Nov 29 '12 at 5:33


15 Answers

up vote 30 down vote accepted

Have you tried with Pandoc?

share|improve this answer
See also stackoverflow.com/questions/663532/… –  tnotstar Mar 31 '10 at 12:11
add comment

Perhaps mathJAX is the ticket. It's built on jsMath, a 2004 vintage JavaScript library.

share|improve this answer
add comment

I'll answer your question with a counter-question...

What do you think of Org-mode? It's not as pure as Markdown, but it is Markdown-like, and I find it as easy to work with, and it allows embedding of Latex. Cf. http://www.gnu.org/software/emacs/manual/html_node/org/Embedded-LaTeX.html


In case you haven't looked at org-mode, it has one great strength as a general purpose "natural markup language" over Markdown, namely its treatment of tables. The source:

| 1 | 0 | 0 |
| -1 | 1 | 0 |
| -1 | -1 | 1 |

represents just what you think it will...

And the Latex is rendered in pieces using tex-mode's preview-latex.

share|improve this answer
I looked at org-mode, and although the embedded LaTeX seems very well done, it looks like total overkill for what I want (typical of emacs). Interestingly, I note that the underlying rendering engine is again dvipng. The table problem is an important one but I have already written a preprocessor to solve it :-) +1 –  Norman Ramsey Feb 3 '10 at 23:24
add comment

you should look at multimarkdown http://fletcherpenney.net/multimarkdown/

it has support for metadata (headers, keywords, date, author, etc), tables, asciimath, mathml, hell i'm sure you could stick latex math code right in there. it's basically an extension to markdown to add all these other very useful features. It uses XSLT, so you can easily whip up your own LaTeX styles, and have it directly convert. I use it all the time, and I like it a lot.

I wish the markdown would just incorporate multimarkdown. it would be rather nice.

Edit: Multimarkdown will produce html, latex, and a few other formats. html can come with a style sheet of your choice. it will convert into MathML as well, which displays in Firefox and Safari/Chrome, if I remember correctly.

share|improve this answer
Looks more complicated than what I want, and it's less than clear what the expressive power is (other than MathML is incorporated by reference0). –  Norman Ramsey Feb 3 '10 at 23:28
add comment

Add the following code to the top of your Markdown files to get MathJax rendering support

<style TYPE="text/css">
code.has-jax {font: inherit; font-size: 100%; background: inherit; border: inherit;}
    tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'] // removed 'code' entry
MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i = 0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

and then `$x^2$` or `$$x^2$$` will render as expected :-)

You can always install a local version of MathJax if you don't want to use the online distribution, but you might need to host it through a local webserver.

share|improve this answer
add comment

You might find mimeTeX useful.

share|improve this answer
Successor mathTeX looks even better---and it's clear that dvipng is the workhorse. Thanks for a great pointer! +1 –  Norman Ramsey Feb 3 '10 at 1:58
add comment

Hey, this might not be the most ideal solution, but it works for me. I ended up creating a Python-Markdown LaTeX extension.


It adds support for inline math and text expressions using a $math$ and %text% syntax. The extension is a preprocessor that will use latex/dvipng to generate pngs for the respective equations/text and then base64 encode the data to inline the images directly, rather than have external images.

The data is then put in a simple-delimited cache file that encodes the expression to the base64 representation. This limits the number of times latex actually has to be run.

Here is an example:

%Hello, world!% This is regular text, but this: $y = mx + b$ is not.

The output:

$ markdown -x latex test.markdown
<p><img class='latex-inline math-false' alt='Hello, world!' id='Helloworld' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFwAAAAQBAMAAABpWwV8AAAAMFBMVEX///8iIiK6urpUVFTu7u6YmJgQEBDc3NxERESqqqqIiIgyMjJ2dnZmZmbMzMwAAAAbX03YAAAAAXRSTlMAQObYZgAAAVpJREFUKM9jYICDOgb2BwzYAVji8AQg8fb/PZ79u4AMvv0Mrz/gUA6W8F7AmcLAsJuBYT7Y1PcMfLiUgyWYF/B8Z2DYAVReABKrZ2DHpZwdopzrA0nKOeHKj66CKOcKPQJWwJo2NVFhfwCQyymhYwCUYD0avIApgYFh2927/QUcE3gDwMpvMhRCDJzNMIPhKZg7UW8DUOIMg9sCPgGo6e8ZODeAlAP9xLEArNy/IIwhAMx9D3IM+3cgi70BqnxZaNQFkHJWAQbeBrByjgURExaAuc9AyjnB5hjAlEO9ygVXzrplpskEMPchQvkBmGMcGApgjjkAVs7yhyWVAcwFK2f/AlJeAI0m5gMsEK+aMhQ6aDuA1DcDIZirBg7IOwxlB5g2QBJBF8OZVUz95hqfC3hOXWGYrwBSHskwk4EByGXab8QAlOBaGizFKYAtUlgUGEgBTCSpZnDCLQUA+y6MXeYnPDgAAAAASUVORK5CYII='> This is regular text, but this: <img class='latex-inline math-true' alt='y = mx + b' id='ymxb' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFIAAAAOBAMAAABOTlYkAAAAMFBMVEX///9ERETu7u4yMjK6urp2dnZUVFSIiIjMzMwQEBDc3NwiIiJmZmaYmJiqqqoAAADS00rKAAAAAXRSTlMAQObYZgAAAOtJREFUKM9jYCAACsCk4wYGgiABTLInEKuS+QGxKvkVGBj47jBwI8tcffI84e45BoZ7GVcLECo9751iWLeSoRPITBQEggMMDBy9sxj2MDgz8DIE8yCpPMxwjWFBGUMMkpFcbAEMvxjKGLgYxIE8NkHBiYIyQMY+hmoGhi0Mdsi2czawbGCQBTJ+ILvzE0MaA9MHIIWwnWE9A+sBpk8LGDgmMCnAVXJNYPgCJHhRQvUiA/cDXoECZx4DXoSZTBtYgaaEPw5AVnkOGBRc5xTcbsReQrL9+nWwyxbgC88DcJZ+QygDcYD1+QPiFAIAtLA8KPZOGFEAAAAASUVORK5CYII='> is not.</p>

As you can see it is a verbose output, but that really isn't an issue since you're already using Markdown :)

share|improve this answer
add comment

kramdown does exactly what you describe:


And it's way more reliable and well-defined than Markdown.

share|improve this answer
add comment

yes, but you'll have to hack it a little yourself. I've written a filter that replaces latex tags $\some\inline\latex$ or $$\some\equation$$ with appropriate image tags to a mimetex.cgi script. It took all of 5 minutes.

Warning: spectacularly ugly...

#!/usr/bin/env python
import sys, markdown,re


def sanitizeLatex(text):
    return re.sub(r"\\",r"%5C", text)

def wrapLatexBlock(text):
    return '<img alt="equation" class="block" src="%s?%s"></img>'%(MIMETEX_LOC,text)

def wrapLatexInline(text):
    return '<img alt="equation" class="inline" src="%s?%s"></img>'%(MIMETEX_LOC,text)

def prepLatexBlock(matchobj):
    return wrapLatexBlock(sanitizeLatex(matchobj.group()[2:-2]))

def prepLatexInline(matchobj):
    return wrapLatexInline(sanitizeLatex(matchobj.group()[1:-1]))

if __name__ == "__main__":
    # initialise markdown

    # deal with embedded latex
    raw_md=re.sub(r'\$\$(.*?)\$\$',prepLatexBlock, raw_md)
    raw_md=re.sub(r'\$(.*?)\$',prepLatexInline, raw_md)

    # once latex is parsed, convert md to html

    # hey presto!

Of course, you have to define the appropriate css yourself for .block and .inline images...

share|improve this answer
add comment

What language are you using?

If you can use ruby, then maruku can be configured to process maths using various latex->MathML converters. Instiki uses this. It's also possible to extend PHPMarkdown to use itex2MML as well to convert maths. Basically, you insert extra steps in the Markdown engine at the appropriate points.

So with ruby and PHP, this is done. I guess these solutions could also be adapted to other languages - I've gotten the itex2MML extension to produce perl bindings as well.

share|improve this answer
add comment

I came across this discussion only now, so I hope my comment is still useful. I am involved with MathJax and, from how I understand your situation, I think that it would be a good way to solve the problem: you leave your LaTeX code as is, and let MathJax render the mathematics upon viewing.

Is there any reason why you would prefer images?

share|improve this answer
Certain Stack Exchange sites Math.Se, Physics.SE, etc... do use MathJax now, though the position of the management is that MathJax is too heavy to be deployed except where absolutely necessary. But that does not apply to presentations as it requires the works to run in a browser. –  dmckee Jan 17 '11 at 21:54
add comment

I was looking for exactly the same thing when I found teqhtml. It does the conversion of $ and $$ equations to images with the nice bonus of aligning the resulting image vertically with the surrounding text. Not a lot of doc but it's quite straightforward.

Hope it helps some future readers.

share|improve this answer
add comment

RStudio has a good free IDE that allows for Markdown and LaTeX.

share|improve this answer
But does it let you combine them in the same document/sentence, as the OP is asking to do? –  matt Nov 29 '12 at 2:26
You can embed equasions in markdown mode. See examples at rstudio.com/ide/docs/authoring/using_markdown_equations –  Roman Luštrik Dec 6 '12 at 15:40
add comment

And if you will excuse a second flawed answer to your question, this depending on answering another unsolved problem or two...

My question, Convert Tex to Metapost, is concerned with rendering, and if I had a good answer to that, it should be possible to put together a good solution to your question, Metapost having all the abilities to hack bounding boxes you could want, and having your choice of lovely rendering engines, through .eps, SVG, &c.

Maybe it would be possible to make a drop-in replacement/extension to dvipng that is based on SVG? It might be possible to use SVG + HTML technology to present SVG where the browser has that, and have an ALT-tag that presents a PNG if it is not there.

share|improve this answer
add comment

Sorry to rouse a really old thread, but I've been using jemdoc for a couple of years and it is really excellent.

share|improve this answer
add comment
