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.
RStudio has a good free IDE that allows for Markdown and LaTeX.
Add the following code to the top of your Markdown files to get MathJax rendering support
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.
UPDATE: these days I just use pandoc instead of canonical markdown, but the above is still useful.
Hey, this might not be the most ideal solution, but it works for me. I ended up creating a Python-Markdown LaTeX extension.
https://github.com/justinvh/Markdown-LaTeX
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:
The output:
As you can see it is a verbose output, but that really isn't an issue since you're already using Markdown :)