LaTeX equations in Ruby on Rails - ruby-on-rails

How can I render LaTeX stuff like
\frac{1}{2}
in a Ruby on Rails application? I mean to get an image?
Thanks!

You could always try http://www.mathjax.org/, leaving the rendering to the client!

To render LaTeX for the web, it looks to me like mathTeX is the application of choice. There are many other options, all of which seem to build on dvipng as a foundation. MathTeX appears to be the most polished and capable, and it is easy to configure as a CGI service, so your Rails application can generated ordinary HTML <img> tags with rather complex src attributes.
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.

By invoking an external program that does the processing for you, such as tex2png or tex2png.pl.
Then again, for simple formulas like that, you may also wish to consider using jsmath. The next step then would be to let your users choose which version they want to see …

Katex
Take a look at Katex. It's from a couple of people behind Khan Academy and it looks extremely legit.
This uses Javascript to generate a nice HTML view of the formulas so I'm still working out the best method to turn this into an image conveniently, though.

The katex gem can render these server-side so you don't even need any JavaScript, and it's smaller and better quality than an image.

There is also mathematical gem that converts TeX math to SVGs, PNGs, or MathML.

Related

LaTeX for Chemistry on my website

I am programming a website on the subject of chemistry and for obvious reasons I also have to include structural and molecular formulas on that site. I want to have as few images as possible on the side and would therefore like to know how I can compile LaTeX code on my website, so I can show everything I could do in LaTeX itself.
Thanks in advance.
As outlined in a previous comment, Chemistry.SE has enabled mhchem in MathJax to allow the rendering of simple formula and reaction equations. The MathJax documention actually gives some directions.
As far as structures of organic molecules are concerned, I'm usually draw them using BkChem and export them as the png images.
If I understand you correctly, you would like to avoid the images themselves and not just the act of drawing. Therefore, the idea to generate the drawings from a linear representation (InChi, SMILES) using openbabel will probably not convince you.
As a matter of fact, it is possible to create structure in LaTeX using chemfig and there have been requests to support this package in MathJax. However, it seems that so far, the strong dependance of chemfig on TikZ has prevented this.

How to store math equation/symbol and display them on the web?

I want to build a website where people can create tests with questions and answers . I want people can type in math equation/symbol and equations in a textbox or something like that, and they will be store in database, it'also displayed on the web like image.
My idea is i will store the text user input in latex syntax and store it, then display it using MathJax, i don't know it's possible or will have better way to do this.
And a problem is in user input will have normal text with "math text" (latex), so how can i separate them and only save the latex text? Please give me some idea or suggest the way to solve it, thanks.
p/s: i'm building this site in ruby on rails, i found the gem mathjax-rails but it seem not working.
Consider building off Gollum. It is the backend for the wiki system Github uses and works fairly well with LaTex equations (currently their is a very irritating bug with less/greater than symbols, but is documented and likely will be fixed in the next release). I start using it this summer to take notes in a math classes, an example of a full page of rendered LaTex equations notes is here here.
Note: You must be logged into Github in order for the equation to render.

Anything better than XSL-FO?

I'm looking at technologies for print rendering, and I know there's lots of old print engines around, but I'd like something that is flexible and also gives some sort of standard (which means that rendering gives us options).
XSL-FO seems to stick out like a sore thumb as the technology we should be using, but if there's other options that fit the bill, I'd be interested to hear about them.
Thanks.
The best XSL-FO engine is Antenna House XSL Formatter. RenderX XEP is also pretty good, Apache FOP is pretty average but you can make it work for simple things.
There is no other "standard" for getting XML into PDF. For SGML there used to be DSSSL. I think some people have also implemented XML->TeX conversion and then use a TeX typesetter. The other (commercial) options off the top of my head are:
PrinceXML (XML+CSS)
PTC Arbortext (FOSI, XSL-FO and APP/3B2)
TurnKey TopLeaf (proprietary)
SDL XySoft XPP (proprietary)
Typefi (basd on InDesign Server)
I guess if your print publishing is simple enough you could use something like iText to build the PDF using a Java class or something.

Does anyone know resources for LaTex

I want to use LaTex to write equations faster and if it is possible to export the result as a png or jpg so that it can be used on a website.
Wikipedia (and its opensource wiki engine) uses LaTeX for that, maybe there are some resources available (at least in the code, as it is opensource).
Your question is very broad. You could start with Amazon's List of Latex Books.
You might want to investigate the StackExchange site mathoverflow.net solution - you can read about here. It uses jsMath which supports a lot of LaTeX syntax.
Assuming you already know a little LateX and your primary goal is to get images, a good high-level tool is mathTeX; there are even public servers that will convert to images for you.
If you want to do everything yourself, all the tools use dvipng at bottom.
I like both MathBin.net and Roger's Online Equation Editor. The latter lets you control the quality of the output. See also this question.
try this: http://hausheer.osola.com/latex2png
Here is a small symbol reference for LaTeX. If you are looking for something more as a general introduction, you can look at "The Not So Short Introduction To LaTeX2e". If you use Inkscape, there is built in support for rendering LaTeX and there are also extensions that do the same. You can read some commentary about it here. There are also things like LaTeX to HTML converters; However, at the time I was looking at them, they were somewhat limited in what formulas they could display.
I taught myself LaTeX using the wikibook. It's fairly comprehensive as an initial guide. I've since bought The LaTeX Companion, which is a more advanced guide to in depth typesetting in LaTeX
I use http://www.artofproblemsolving.com/LaTeX/AoPS_L_TeXer.php when I need a quick equation for a web site.
There are packages that will automatically produce images from LaTeX source, but these are often either buggy or used incorrectly. Many people install them on their blogs, for example, and the images show up if you visit the blog directly but they don't show up if you view the page via a blog reader. I'm not saying these problems can't be fixed. They can, but it often takes a few tries.
I prefer just to make a gif and stick it in the page. It's low tech and reliable.
One more tip: it's a good idea to put the LaTeX source in the alt tag of the image. This helps people using screen readers. It helps you too if you need to modify the equation later.
Detextify is a great site that lets you draw a symbol, and it will pop up a list of latex commands that may match your drawing. It's quite accurate! http://detexify.kirelabs.org/classify.html

What is your preferred way to produce charts in a Ruby on Rails web application?

I'd like to add some pie, bar and scatter charts to my Ruby on Rails web application. I want want them to be atractive, easy to add and not introduce much overhead.
What charting solution would you recommend?
What are its drawbacks (requires Javascript, Flash, expensive, etc)?
Google Charts is an excellent choice if you don't want to use Flash. It's pretty easy to use on its own, but for Rails, it's even easier with the gchartrb gem. An example:
GoogleChart::PieChart.new('320x200', "Things I Like To Eat", false) do |pc|
pc.data "Broccoli", 30
pc.data "Pizza", 20
pc.data "PB&J", 40
pc.data "Turnips", 10
puts pc.to_url
end
If you don't need images, and can settle on requiring JavaScript, you could try a client-side solution like the jQuery plugin flot.
I am a fan of Gruff Graphs, but Google Charts is also good if you don't mind relying on an external server.
It requires flash and isn't free (though inexpensive): amcharts.
I've used it successfully and like it. I evaluated a number of options a while back and chose it. At the time, however, Google Charts wasn't as mature as it seems to be now. I would consider that first if I were to re-evaluate now.
There's also Scruffy. I took a look at the code recently and it seemed easy to modify/extend. It produces svg and (by conversion) png.
Have you tried the Google Charts API? - web service APIs don't really come much simpler. It's free to use, simple to implement, and the charts don't look too shoddy.
Open Flash Chart II is a free option that gives very nice output. It does, as you'd expect, require Flash.
Fusion Charts is even nicer, but is $499. In researching this, I found a cut-down free version that might serve your needs.
I 2nd the vote for flot. The latest version lets you do some animations and actions that I previously thought would only be possible via Flash. The documentation is fantastic. It simple to write by hand, but for simple cases it gets even easier with a Rails plugin called flotilla. You should check out the examples page for a better idea of what it's capable of. The zooming and hover capabilities are especially impressive.
The new Google Visualization appears to produce charts that are of more varied type, better looking and interactive than Google Graphs.
http://code.google.com/apis/visualization/
Morris.js is nice and open source. I would like to choose it comparing to highcharts. There is a new great video tutorial from Railscasts
I've just found ZiYa produces some really sexy charts and is Rails specific.
The downsides are it uses Flash and if you don't want the sites to link to XML/SWF page it costs $50 per site.
[I've not decided on it yet, but wanted to throw it out there in case people want to vote it up]
I've used Fusion Charts extensively from within a Java web application, but it should work the same way from Rails since you're just embedding a Flash via HTML or JavaScript and passing it XML data. It's a slick package and their support has always been very responsive.
You should take a look at Dmitry Baranovskiy's Javascript library called Raphaël.
Google charts is very nice, but it's not a rails only solution. You simple use the programming language of your choice to dynamically produce urls that contain the data and google returns you back a nice image with your chart.
http://code.google.com/apis/chart/
In the old days, I decided to roll my own (using RVG/RMagick), mainly because Gruff didn't have everything I wanted. The downside was that finding and eliminating all the bugs in graphing code is a pain. These days Gruff is my choice as it's really gone forward in terms of customization and flexibility.
The standard Gruff templates/color choices suck though, so you'll need to get your hands dirty for best results.
Regarding amcharts, there's a "free" version with a very few restrictions that generates Flash charts including the 'chart by amCharts.com' mention.
And there's a nice plugin, ambling, that provides you with some helper methods to easily add charts to your views. Please note that amCharts.com reference documentation is still a must to tailor the chart to your requirements.
GoogleCharts and Gruff charts are great, but sometimes they lack some features that I need for more scientific plotting. There is a gem for gnuplot which may be helpful for some of these situations.
http://rgplot.rubyforge.org/
I have started using protovis to generate SVG charts with javascript. My basic approach in rails is to have a controller that returns the data to be charted as JSON, and scoop it up with a bit of javascript and protovis.
Only downside, is that full IE support (Since it is based on SVG) is currently unavailable straight out of the box... However, current patches go a fair way to providing IE support, details of which can be found here.
I personally prefer JavaScript-based charts over Flash. If that's ok, also check out High Charts. A Rails plugin is also available.
The gchartrb gem is no longer maintained, it seems. The author points to these gems:
googlecharts
gchart (seems abandoned as well)
We do this by shelling out to gnuplot to generate the charts as PNGs server-side. It's a bit old-school and the charts aren't interactive but it works and is cacheable.
(The other reason we do this is so we can put exactly the same chart in the PDF version of the report).
This isn't specifically RoR however, it is pretty slick port of Gruff to javascript: http://bluff.jcoglan.com/
ChartDirector. Ugly API, but good, server-side image results. Self contained binary.
FWIW, I'm not a fan of using Google Charts when fit & finish is important. I find that the variables for sizing, in particular, are unpredictable - the chart does its own thing.
I haven't yet played with Gruff/Bluff/etc., but for a higher-profile project I won't use Google Charts.
If you want quite sexy charts, easy to generate, and you can enable Flash, then you should definitely have a look at maani.us xml/swf charts.
Some XML builder behind it and you're ready to go.
FusionCharts is a very good charting product. Works well with RoR. Their support and forums are good. The free version of this product has limited number of charts and features, but no watermark.
I just started using googlecharts for my rails 3 project. It is nice and clean, and seems to be the only google visualization api based gem which is alive. Others are inactive and mostly use the old google charts api (released somewhere in 2007-2008).
https://github.com/mattetti/googlecharts
D3 has become my preferred way add great looking charts to web apps. You have to do a little mroe work that some other frameworks, but the appearance and control outweighs that.
I primarily use SVG, which means no IE8, but that is becoming less of an issue.
HighChart - A charting library written in pure JavaScript
Gems like highchart-rails, lazy-high-chart makes the integration with rails easier
gem 'chart' makes it easy to add ChartJS and NVD3 charts to rails.

Resources