How can I mix LaTeX in with Markdown? [closed]
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this questionI'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 con开发者_JS百科trol 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.
Have you tried with Pandoc?
EDIT:
Although the documentation has become a bit complex, pandoc has supported inline LaTeX and LaTeX templates for 10 years.
Documents like the following one can be written in Markdown:
--- title: Just say hello! author: My Friend header-includes: | \usepackage{tikz,pgfplots} \usepackage{fancyhdr} \pagestyle{fancy} \fancyhead[CO,CE]{This is fancy} \fancyfoot[CO,CE]{So is this} \fancyfoot[LE,RO]{\thepage} abstract: This is a pandoc test with Markdown + inline LaTeX --- Just say hello! =============== This could be a good example or inlined \LaTeX: \begin{tikzpicture} \begin{axis} \addplot[color=red]{exp(x)}; \end{axis} \end{tikzpicture} %Here ends the furst plot \hskip 5pt %Here begins the 3d plot \begin{tikzpicture} \begin{axis} \addplot3[ surf, ] {exp(-x^2-y^2)*x}; \end{axis} \end{tikzpicture} And now, just a few words to terminate: > Goodbye folks!
Which can be converted to LaTeX using commands like this:
pandoc -s -i Hello.md -o Hello.tex
Following is an image of the converted
Hello.md
toHello.pdf
file using MiKTeX as LaTeX processor with the command:pandoc -s -i Hello.md -o Hello.pdf
Finally, there are some open source LaTeX templates like this one: https://github.com/Wandmalfarbe/pandoc-latex-template, that can be used for better formatting.
As always, the reader should dig deeper if he has less trivial use cases than presented here.
Perhaps mathJAX is the ticket. It's built on jsMath, a 2004 vintage JavaScript library.
As of 5-Feb-2015 I'd switch to recommend KaTeX - most performant Javascript LaTeX library from Khan Academy.
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;}
</style>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [['$','$'], ['\\(','\\)']],
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>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-AMS_HTML-full"></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.
UPDATE: these days I just use pandoc instead of canonical markdown, but the above is still useful.
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
Postscript
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.
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.
RStudio has a good free IDE that allows for Markdown and LaTeX.
kramdown does exactly what you describe:
https://kramdown.gettalong.org/syntax.html#math-blocks
And it's way more reliable and well-defined than Markdown.
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:
%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 :)
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
MIMETEX_LOC="http://some.server.com/cgi-bin/mimetex.cgi"
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
md=markdown.Markdown()
raw_md=open(sys.argv[1],"r").read()
##
# 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
##
main_html=md.convert(raw_md)
# hey presto!
print(main_html)
Of course, you have to define the appropriate css yourself for .block and .inline images...
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?
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.
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.
Sorry to rouse a really old thread, but I've been using jemdoc for a couple of years and it is really excellent.
It is possible to parse Markdown in Lua using the Lunamark code (see its Github repo), meaning that Markdown may be parsed directly by macros in Luatex and supports conversion to many of the formats supported by Pandoc (i.e., the library is well-suited to use in lualatex
, context
, Metafun, Plain Luatex, and texlua
scripts).
The project was started by John MacFarlane, author of Pandoc, and the tool's development tracks that of Pandoc quite closely and is of similar (i.e., excellent) quality.
Khaled Hosny wrote a Context module, providing convenient macro support. Michal's answer to the Is there any package with Markdown support? question gives code providing similar support for Latex.
精彩评论