Tuesday, December 22, 2009

jsLaTeX: A jQuery plugin to directly embed LaTeX into your website or blog



jsLaTeX is the latest plugin I developed for jQuery and I wrote it so that I could easily be able to embed LaTeX directly into my blog but anyone can use it for their site.

You can download the normal, unpacked version for development or the packed version for production.

Click here to view a demo

Here is a link to the jQuery plugin page, where you can find all the releases.

Usage Instructions


1. Download the jquery.jslatex.js file from one of the above-mentioned links and add it to your page along with jQuery (if it is not already added):

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="jquery.jslatex.js"></script>


2. The simplest way to invoke the script is by calling the plugin with no parameters:

<script>
$(function () {
    $(".latex").latex();
});
</script>
<div class="latex">
    \int_{0}^{\pi}\frac{x^{4}\left(1-x\right)^{4}}{1+x^{2}}dx =\frac{22}{7}-\pi
</div>

The above will render the following:


\int_{0}^{\pi}\frac{x^{4}\left(1-x\right)^{4}}{1+x^{2}}dx =\frac{22}{7}-\pi


The plugin works by taking the inner HTML of the specified container obviously assuming that it is valid LaTeX, and displays the image using CodeCogs LaTeX engine. The rendered output replaces the equation inside the container.



The plugin also accepts 3 optional parameters:
  • format
  • url
  • callback

[format]
The format parameter is used to specify the format of the returned output. There are currently 3 supported formats:
  • gif
  • png
  • swf

[url]
The url parameter is used to change the engine of the LaTeX generator. Let's take an example with a different engine, and in this case I will be using SITMO's LaTeX engine.
The url SITMO's engine uses to render the output is as follows:

http://www.sitmo.com/gg/latex/latex2png.2.php?z=100&eq=equation


where equation is the the LaTeX equation you wish to render.

Now we must tell the plugin where to put the equation and we do this by using the {e} specifier. This is how it can be used:

$(".latex").latex({url: 'http://www.sitmo.com/gg/latex/latex2png.2.php?z=100&eq={e}'});

As you can see from the above example, we placed the {e} specifier where the equation should be and then the engine takes care of the rest.

Here is an example of using the new engine to render the output:


\int_{0}^{\pi}\frac{x^{4}\left(1-x\right)^{4}}{1+x^{2}}dx =\frac{22}{7}-\pi


The plugin currently supports another specifier: {f} and this is used for those engines that allow you to specify a file-format output. The file-types that are currently supported are the ones mentioned in the [format] section.

If we take the 'original' engine url, we can turn into a url with specifiers like such:

http://latex.codecogs.com/{f}.latex?{e}


Here are examples of Engines you can use (ready with specifiers):


Engines
http://latex.codecogs.com/{f}.latex?{e}
http://www.sitmo.com/gg/latex/latex2png.2.php?z=100&eq={e}
http://www.forkosh.dreamhost.com/mathtex.cgi?{e}
http://chart.apis.google.com/chart?cht=tx&chl={e}


[callback]
The callback parameter allows you to pass a function that will be executed after the image has been retrieved. In your callback function, this will refer to the newly created element (as a jQuery object) that contains the rendered output.

The following example will set a border around your rendered output.

$(".latex").latex({
    callback : function() {
        this.css({ border: '1px solid black'});
    }                        
});

With the above callback, we can now render the following:


\int_{0}^{\pi}\frac{x^{4}\left(1-x\right)^{4}}{1+x^{2}}dx =\frac{22}{7}-\pi



Comments (21)

Loading... Logging you in...
  • Logged in as
Very cool! I am not very familiar w/ latex. What are the advantages to using latex over MathML if you are writing your equations from scratch?
2 replies · active 796 weeks ago
LaTeX is a standard in academia and when it comes to publishing papers. Also if you compare the "source" in LaTeX and MathML you'll also see the obvious advantage: LaTeX is shorter, more expressive and intuitive at the same time.
One problem with MathML is that Google Chrome currently does not support it (Mozilla Firefox does though)
You should look into jsMath at http://www.math.union.edu/~dpvc/jsMath/. Same kind of input, but with output that scales with the font size on the page instead of generating a PNG. Also, there's no dependence on an outside web site.
Hey Budy,

your demo is flawed: http://falconsflyingcircus.com/~dre/jslatex/

Look at the first two formulars and note that in the second on the 1 + x&sup2; under the line of the integral became a 1x&sup2;. Seems to be a problem with html encoding the url parameters of your XHR.

Best wishes,

Benjamin Flesch
1 reply · active 796 weeks ago
Benjamin,
thanks for feedback. I'm gonna look it into that.

<a href="http://plugins.jquery.com/node/12075" target="_blank">http://plugins.jquery.com/node/12075

*Update*
Bug has now been fixed.
Interesting post Dre!
1 reply · active 796 weeks ago
Thank you for the support, Thomas.
nice tutorial. You did a great job. Thanks
1 reply · active 796 weeks ago
Thank you for your support, rana.
very nice ! does it support all latex syntax ?
1 reply · active 796 weeks ago
Thanks for the support, Amre.

Unfortunately, not all of the syntax works; and oddly enough, some equations work on some engines but don't work on others :/

That's why I provided the ability to change the LaTeX engine in the plugin.
I really like this idea. Congratz!!!
1 reply · active 796 weeks ago
Ivo, thanks for the support.
Great!
The z argument in our sitmo url is the zoom-factor.
Have fun
I have been looking for this for long. Thank you for posting such an interesting tutorial. However, I have to say that google chrome does not support it :(
Hello Andreas,

My name is Ananjan Chaudhuri and I work for Packt Publishing, a U.K. based publishing firm specializing in focused IT books. You can read more about us here: http://www.packtpub.com/

I came across your blog via a Google search and noticed a good amount of relevant information and blog updates related to jquery.

I would like to inform you that Packt has recently published 3 new books on jquery:
1.CMS Design Using PHP and jquery
2.PHP jquery Cookbook
3.jquery Plugins Development Beginner's Guide

Keeping in mind your knowledge in this subject and having looked at your contributions, I feel you'd make an excellent reviewer of this book. In case you'd like to write a review about any of the above mentioned books on your website and/or Amazon, simply let me know your shipping details (required for registration of new reviewers) and I'll have the eBook added to your account and would provide you with the download instructions in my next e-mail. You can then download this book instantly.

If you have any queries, do let me know and I'd be happy to assist.

I look forward to hearing your thoughts on this.

Have a good day!

Ananjan.
Awesome plugins, will feature it on our website.
Cool, we can put complex stuff in research papers on websites
Thnx for posting.. it might be helpful for jquery users
Is it posible to change or replace <div clas="latex">frac{a}{b}</div> with one symbol like "$frac{a}{b}$"

Post a new comment

Comments by