Inline GraphViz DOT evaluation for graphs using Emacs, org-mode and org-babel

With Emacs, org mode and org-babel, it’s possible to evaluate source code samples embedded in your org files and have the output of said evaluation appear inline. This makes for a beautiful literate programming environment. It also enables one to include graphs in one’s documents (org mode, PDF, HTML presentations or blog posts) by using for example GraphViz.

This blog post (obviously authored using Emacs and Org mode) contains short instructions for doing so.

First follow the org-babel documentation and enable source code evaluation for dot by adding the following to your Emacs init.el:

‘((dot . t)))

Then create a new document, and add something like the following dot source code sample to it:

#+BEGIN_SRC dot :file dot_success.png :cmdline -Kdot -Tpng
  digraph {
  // graph from left to right
  node [shape=box];

  id [label="Install Graphviz"]
  conf [label="Configure org-babel"]
  dot [label="DOT in org-mode"]

  id -> conf
  conf -> dot
  dot -> "Profit"
  dot -> "Success" [style=dotted]

Now press C-c C-c to evaluate this code. Emacs will generate the configured output file dot_success.png and then link to it in an automatically created #+RESULTS section right below it.

You can press C-c C-x C-v to toggle display of inline images to see it directly in Emacs. Alternatively, use M-x org-display-inline-images to switch this on. Whenever you change the DOT source code (press C-c ‘ to edit the dot source code in a separate buffer), just press C-c C-c to re-execute the updated source.

In any exported documents (for examlpe this blog post), only the output graph itself will appear, like this:

Exported as a PDF with C-e l o it looks like this.

In your Emacs, it should look like this:

This approach should work for any of the many languages supported by org-babel. Let me know in the comments what you come up with!

7 thoughts on “Inline GraphViz DOT evaluation for graphs using Emacs, org-mode and org-babel”

  1. Alternative way of returning result:

    #+BEGIN_SRC python :results file
    import matplotlib
    import matplotlib.pyplot as plt

    plt.plot([1, 2, 3])
    plt.savefig('images/myfig.png', dpi=50)

    return 'images/myfig.png'

  2. When I tried to re-compile the dot source code after modifying it, the result always kept the same as before, unless I change the file name to be saved.

    It seems that emacs does not update the dot src to tmp file and compiles the old one.

    Have you ever met such problem?

    1. No, I did not see that specific behaviour. However, what does happen is that it definitely seems to create a new output file, but the inline image disappears. I have to toggle inline images for the new file to be displayed.

  3. ugly way to get image to re-render as you edit dot block:

    (add-to-list ‘auto-mode-alist ‘(“\\.png\\'” . auto-image-file-mode))
    (add-to-list ‘auto-mode-alist ‘(“\\.png\\'” . image-mode))
    (add-to-list ‘auto-mode-alist ‘(“\\.png\\'” . auto-revert-mode))

Leave a Reply

Your email address will not be published. Required fields are marked *