jQuery for LaTeX in HTML

143 views
Skip to first unread message

Sean B. Palmer

unread,
Oct 23, 2010, 8:02:23 AM10/23/10
to Gallimaufry of Whits
<script src="http://goo.gl/LISf"></script>
<script>
$(document).ready(function() {
$('code').each(function(i, e) {
e = $(e)
latex = e.text()
chart = 'http://www.google.com/chart?cht=tx&chl='
enc = encodeURIComponent(latex)
e.replaceWith('<img src="' + chart + enc + '" alt="' + latex + '">')
});
})
</script>

Put that in your HTML files and elements like
<code>\frac{\pi}{6}</code> will be rendered as beautiful LaTeX formulæ
using the Google Charts API. The goo.gl script is the minimised
version of jQuery 1.4.3 on Google APIs.

Combined with Markdown and using a Wikipedia short base URI:

<base href="http://enwp.org/">

This can be really beautiful as you can do things like:

The [Leibniz formula](Leibniz_formula_for_pi) for [π](π) is `1 \,-\,
\frac{1}{3} \,+\, \frac{1}{5} \,-\, \frac{1}{7} \,+\, \frac{1}{9}
\,-\, \cdots \;=\; \frac{\pi}{4}`

But there is one problem, in that Markdown sucks. When you put a
<base> or <style> element in at the top of a Markdown document,
Markdown gets confused and thinks that they need to be wrapped in a
<p> block. This happens even if you wrap the whole thing in a <head>
element.

Another slight annoyance is that you have to use <code> elements again
in tables, because Markdown has no native support for tables.

Apart from that, this is a pretty nice combination, and it degrades
gracefully if script isn't supported.

Reply all
Reply to author
Forward
0 new messages