Blogging and Babel
In my last post, I included a picture of a binary tree. There's
nothing surprising about that, of course. Just fire up graphviz
,
say, and in just a few lines you have a png image of your tree. What is
surprising is that everything was done in the org file for that post.
Here is the relevant part of the previous post's org file
#+BEGIN_SRC dot :file tree.png :cmdline -Kdot -Tpng graph G { size="2,3"; edge [dir=none, style=line]; node [shape=circle]; 5 -- 4; 5 -- 3; 4 -- 9; 3 -- 1; 4 -- 8; 3 -- 6; } #+END_SRC
When I put the point inside the block between the #+BEGIN_SRC and the #+END_SRC and typed C-c C-c the code was executed producing the tree
dot
produced, of course, and if I type C-c C-x
C-v, the picture is toggled off and the link to the file is revealed.
#+results file:tree.png
When the org file is exported to HTML, the picture is replaced by an <img …> tag.
All this is made possible by Eric Schulte's and Dan Davison's Org-babel, which is now part of the Org-mode distribution. You can find complete documentation for Babel in the Org Manual and there's a nice tutorial at the Worg page.
Babel supports a number of languages as shown on the languages page of the Babel documentation. For example, you can use ditaa to turn ASCII art into nicely rendered diagrams. This
#+BEGIN_SRC ditaa :file tunnel.png :cmdline -r -s 0.65 +---------+ +---------+ | | | | | Host 1 +<----+ +------->+ Host 3 | | cBLU | | | | cYEL | +---------+ | | +---------+ | | | | | +--------+ +--------+ | +---->+ | | +<--+ | GW 1 +<--=--->+ GW 2 | +---->+ cRED | | cRED +<--+ | +--------+ +--------+ | | | | | +---------+ | | +---------+ | | | | | | | Host 2 +<----+ +------->+ Host 4 | | cBLU | | cYEL | +---------+ +---------+ #+END_SRC
results in this
Finally, here's an example of using gnuplot with Babel. Given this table
Year | Posts |
---|---|
2009 | 39 |
2010 | 10 |
2011 | 9 |
and these gnuplot commands
#+BEGIN_SRC gnuplot :var data=posts :file bar-chart.png set size .5, 1 set boxwidth .5 relative set xtics 2008,1,2012 set style fill solid 1.0 plot data using 1:2 with boxes title 'Posts by Year' #+END_SRC
We end up with
All of these examples produced pictures but Babel supports 29 languages including C, Scheme, elisp, Python, Ruby, and many others that do general computation rather than producing graphics. This is perfect for research because all the data, processing code, and final paper can be kept in a single file—the ultimate example of reproducible research.
No comments:
Post a Comment