Adding LaTeX equations to Incanter charts

I’ve added a new library to Incanter called incanter.latex that adds the ability to include LaTeX formatted equations as annotations and subtitles in charts. The library is based on the fantastically useful JLaTeXMath library.

The following examples require Incanter version 1.2.2-SNAPSHOT or greater. Add the following dependency to your project.clj file:

[incanter "1.2.2-SNAPSHOT"]

Load the necessary libraries.

(use '(incanter core stats charts latex))

Define the latex-formatted equation; I’ll use the str function so I can break the equation across multiple lines. Notice that I have to use two backslashes where I would only need one if I were were working directly in LaTeX; this is because the backslash is an escape character in Clojure/Java strings.

(def eq (str "f(x)=\\frac{1}{\\sqrt{2\\pi \\sigma^2}}" 
             "e^{\\frac{-(x - \\mu)^2}{2 \\sigma^2}}"))

The equation can be rendered as an image with the latex function. The rendered equation can then be viewed in a window or saved as a png file with the view and save functions respectively.

(view (latex eq))
(save (latex eq) filename)

Use the add-latex function to add an annotation to a chart. The following example adds the above equation to a function-plot of the Normal PDF.

(doto (function-plot pdf-normal -3 3)
  (add-latex 0 0.1 eq)

Use the add-latex-subtitle function to add a rendered LaTeX equation as a subtitle to the chart (this particular chart does not have a main title).

(doto (function-plot pdf-normal -3 3)
  (add-latex-subtitle eq)

The complete code for the above examples can be found here.

6 responses to “Adding LaTeX equations to Incanter charts

  1. Chris Burroughs

    Hooray for LaTex! It would be cool if there was a way to specify raw strings (a la Python) so all of those extra backslashes were not needed.

  2. Hello,

    I released a new version of JLaTeXMath :


  3. Hello David,

    If you have any questions or requests (or if you meet bugs), don’t hesitate to contact me. Thanks to you too…


  4. I am grateful for latex support :)

    Actually to use “save” function on latex equation I had to (use ‘(incanter io)). Otherwise I had “java.lang.IllegalArgumentException: No method in multimethod ‘save’ for dispatch value: class java.awt.image.BufferedImage”.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s