<?xml version="1.0" encoding="UTF-8"?>
<post>
  <body>&lt;p&gt;[latex]\LaTeX{}[/latex] is the permier typesetting tool of the engineering and math world.  It allows you to create beautifully typeset mathematical documents with a minimal learning curve.  So why would you care about this on the web?  Well for one, there really is no way to typset math in a browser.  Sure there is &lt;a href=&quot;http://www.w3.org/Math/&quot;&gt;MathML&lt;/a&gt; but that is supported by only a few browsers nativly, most will require a plugin (if even available).&lt;/p&gt;
&lt;p&gt;So how should one go about doing this, ensuring that the math formulas will be seen by the widest possible auidence?  Use images!  Every browser can render an image, so if we are able to take the LaTeX and render it as an image, we will be set.  This is exactly the approach taked by &lt;a href=&quot;http://www.mayer.dial.pipex.com/tex.htm&quot;&gt;LatexRender&lt;/a&gt;, a set of scripts for rendering LaTeX in &lt;a href=&quot;http://www.php.net/&quot;&gt;php&lt;/a&gt; programs.  Unfortunatly, as far as I know, there is not drop in solution for Ruby.&lt;/p&gt;
&lt;p&gt;However, there were a few projects that pointed me in the right direction.  Most noteable was redsymbols &lt;a href=&quot;http://redsymbol.net/software/l2p/&quot;&gt;l2p&lt;/a&gt; Perl script and the afformentioned LatexRender.  I have already done the hard stuff for you and put it up on my GitHub &lt;a href=&quot;http://github.com/mpetnuch&quot;&gt;repository&lt;/a&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;&lt;span class=&quot;gv&quot;&gt;$&amp;gt;&lt;/span&gt; git clone git&lt;span class=&quot;sy&quot;&gt;:/&lt;/span&gt;/github.com/mpetnuch/latex-renderer.git vendor/plugins/latex_render&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You also need to make sure that you have the RedCloth Ruby Gem and a LaTeX package for the &lt;a href=&quot;http://github.com/mpetnuch/latex-renderer/tree/master&quot;&gt;Latex Render plugin&lt;/a&gt; installed.  Next we need to write our own custom RedCloth.  I am going to call it &lt;strong&gt;Washcloth&lt;/strong&gt; since it is cleaning up the site.  Create a new file called &lt;strong&gt;washcloth.rb&lt;/strong&gt; in your projects &lt;strong&gt;lib&lt;/strong&gt; directory with the following contents:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;&lt;span class=&quot;r&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;cl&quot;&gt;Washcloth&lt;/span&gt; &amp;lt; &lt;span class=&quot;co&quot;&gt;RedCloth&lt;/span&gt;

  &lt;span class=&quot;co&quot;&gt;LATEX_START_CODE&lt;/span&gt;  = &lt;span class=&quot;rx&quot;&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;latex&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\]&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mod&quot;&gt;i&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;co&quot;&gt;LATEX_END_CODE&lt;/span&gt;    = &lt;span class=&quot;rx&quot;&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\[&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;latex&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\]&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mod&quot;&gt;i&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;co&quot;&gt;LATEX_RE&lt;/span&gt;          = &lt;span class=&quot;rx&quot;&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;latex&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\]&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;(.*)&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\[&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;latex&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\]&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mod&quot;&gt;i&lt;/span&gt;&lt;/span&gt;

  &lt;span class=&quot;co&quot;&gt;RULES&lt;/span&gt; = [&lt;span class=&quot;sy&quot;&gt;:block_textile_table&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:block_textile_lists&lt;/span&gt;,
           &lt;span class=&quot;sy&quot;&gt;:block_textile_prefix&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:inline_textile_image&lt;/span&gt;, 
           &lt;span class=&quot;sy&quot;&gt;:inline_latex&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:inline_textile_link&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:inline_textile_code&lt;/span&gt;, 
           &lt;span class=&quot;sy&quot;&gt;:inline_textile_span&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:glyphs_textile&lt;/span&gt;]

  &lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;inline_latex&lt;/span&gt;( text )

    &lt;span class=&quot;c&quot;&gt;# Don't do anything unless we have same number of start/end tags&lt;/span&gt;
    &lt;span class=&quot;r&quot;&gt;if&lt;/span&gt; text.scan(&lt;span class=&quot;co&quot;&gt;LATEX_START_CODE&lt;/span&gt;).size == text.scan(&lt;span class=&quot;co&quot;&gt;LATEX_END_CODE&lt;/span&gt;).size
      text.gsub!( &lt;span class=&quot;co&quot;&gt;LATEX_RE&lt;/span&gt; ) &lt;span class=&quot;r&quot;&gt;do&lt;/span&gt; |m|
        latex = &lt;span class=&quot;co&quot;&gt;LatexRenderer&lt;/span&gt;.new
        latex.formula = &lt;span class=&quot;gv&quot;&gt;$1&lt;/span&gt;
        &lt;span class=&quot;r&quot;&gt;begin&lt;/span&gt;
          filename = latex.process
          md5 = latex.md5hash
          &lt;span class=&quot;c&quot;&gt;# if the processing was successful we just display the rendered image&lt;/span&gt;
          sprintf &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;&amp;lt;img src=&amp;quot;%s&amp;quot; alt=&amp;quot;latex image&amp;quot; /&amp;gt;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, 
                         filename.gsub(&lt;span class=&quot;rx&quot;&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;.*?&lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;\/&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;images&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;/&lt;/span&gt;&lt;/span&gt;, &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;/images&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;)
        &lt;span class=&quot;r&quot;&gt;rescue&lt;/span&gt; =&amp;gt; e
          sprintf(&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, latex.formula)
        &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
      &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;to_html&lt;/span&gt;
    &lt;span class=&quot;r&quot;&gt;super&lt;/span&gt;(*&lt;span class=&quot;co&quot;&gt;RULES&lt;/span&gt;)
  &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Let&amp;#8217;s test it out.  Fire up the console:&lt;br /&gt;
 &lt;br /&gt;
&lt;pre&gt;&lt;code class=&quot;multiline_code&quot;&gt;&amp;gt;&amp;gt; latex = &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;[latex]x^2 + y^2 = z^2[/latex]&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
=&amp;gt; &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;[latex]x^2 + y^2 = z^2[/latex]&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&amp;gt;&amp;gt; r = &lt;span class=&quot;co&quot;&gt;WashCloth&lt;/span&gt;.new latex
=&amp;gt; &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;[latex]x^2 + y^2 = z^2[/latex]&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&amp;gt;&amp;gt; r.to_html
=&amp;gt; &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;&amp;lt;p&amp;gt;&amp;lt;img src=&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;/images/latex/image.png&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt; /&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;So we see that our [latex]\LaTeX{}[/latex] is replaced with a link to a rendered image of the code.  All that is left is to replace any instance of RedCloth in your program with WashCloth and it will automatically render the LaTeX in your views.  What if you use something like Err the Blog&amp;#8217;s &lt;a href=&quot;http://errtheblog.com/posts/12-actsastextiled&quot;&gt;acts_as_textiled&lt;/a&gt;?  You going to have to edit the plugins source and change RedCloth to WashCloth, but that&amp;#8217;s it! So what have we accomplished?  We can now render latex commands inline with our Textiled HTML.  For instance, the following command:&lt;/p&gt;
&lt;pre&gt;
[latex]
$$\Gamma(s) \zeta(s) =
  \int_0^{\infty} \frac{x^{s-1}e^{-x}}{1-e^{-x}}\,dx$$
[/latex]
&lt;/pre&gt;&lt;p&gt;Will render as: [latex]$$\Gamma(s) \zeta(s) = \int_0^{\infty} \frac{x^{s-1}e^{-x}}{1-e^{-x}}\,dx$$[/latex]&lt;/p&gt;      <script type="text/javascript">
      var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
      document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
      </script>
      <script type="text/javascript">
      var pageTracker = _gat._getTracker("UA-4701682-1");
      pageTracker._initData();
      pageTracker._trackPageview();
      </script>
</body>
  <created-at type="datetime">2008-06-16T03:37:49Z</created-at>
  <id type="integer">7</id>
  <introduction>&lt;p&gt;&lt;a href=&quot;http://whytheluckystiff.net/ruby/redcloth/&quot;&gt;RedCloth&lt;/a&gt; is Ruby&amp;#8217;s Textile module.  Textile is a lightweight markup language originally developed by Dean Allen that allows a user to create well-formed styled XHTML.  This site employes the RedCloth Textile module through the &lt;a href=&quot;http://errtheblog.com/posts/12-actsastextiled&quot;&gt;acts_as_textiled&lt;/a&gt; plugin.  The nice thing about RedCloth is it is faily easy to extend the markup which it recognies.  In this tutorial, I am going to show you how to render [latex]\LaTeX{}[/latex] in your site by extending RedCloth.&lt;/p&gt;</introduction>
  <permalink>extending-redcloth</permalink>
  <title>Extending RedCloth</title>
  <topic-id type="integer">1</topic-id>
  <updated-at type="datetime">2008-12-01T00:15:21Z</updated-at>
  <user-id type="integer">1</user-id>
  <version type="integer">48</version>
</post>
