Adding equations to Incanter charts

We've seen how to add a title to the charts and labels on the axes, but so far, they've all used only plain, unformatted text. Sometimes, we might want to use a formula instead.

Incanter lets you add a formula to a chart using LaTeX's mathematical notation. LaTeX (http://www.latex-project.org/) is a professional grade document typesetting system. We won't go into the details of its math notation—there are plenty of books and tutorials out there already. Instead, we'll just see how to use it with Incanter.

In this recipe, we'll take the chart from the last recipe, Creating function plots with Incanter, and add the function as a subtitle.

Getting ready

We'll use the same dependencies in our project.clj file as we did in Creating scatter plots with Incanter.

We'll use this set of imports in our script or REPL:

(require '[incanter.core :as i]
         '[incanter.charts :as c]
         '[incanter.latex :as latex])

We'll also use the chart that we made in Creating function plots with Incanter. We'll still use the variable f-plot for it.

How to do it...

The LaTeX string that we want to add is fairly simple, and it gives us a good taste of what the syntax for this notation is:

  1. We bind the LaTeX string to inv-log:
    (def inv-log "f(x)=\frac{1.0}{\log x}")
  2. We apply that to a chart using the incanter.latex/add-latex-subtitle function:
    (latex/add-latex-subtitle f-plot inv-log)

    The chart reflects the change immediately, as shown in the following screenshot:

    How to do it...

There's more...

Full information about LaTeX and its math notation is beyond the scope of this recipe. The website for LaTeX is http://www.latex-project.org/. The LaTeX wikibook has a good chapter on typesetting mathematical equations, at http://en.wikibooks.org/wiki/LaTeX/Mathematics, and The Not So Short Introduction to LaTeX 2ε (http://tobi.oetiker.ch/lshort/lshort.pdf) is another good resource for learning LaTeX, including the math syntax.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.34.39