Merge commit 'origin/master'
[org-mode/org-tableheadings.git] / org-babel-worg.org
blobd4c44222b72a2f21c30eda7f051601f1ff5caf2a
1 #+OPTIONS:    H:3 num:nil toc:2 \n:nil @:t ::t |:t ^:{} -:t f:t *:t TeX:t LaTeX:t skip:nil d:(HIDE) tags:not-in-toc
2 #+STARTUP:    align fold nodlcheck hidestars oddeven lognotestate hideblocks
3 #+SEQ_TODO:   TODO(t) INPROGRESS(i) WAITING(w@) | DONE(d) CANCELED(c@)
4 #+TAGS:       Write(w) Update(u) Fix(f) Check(c) 
5 #+TITLE:      Org-babel
6 #+AUTHOR:     Eric Schulte, Dan Davison
7 #+EMAIL:      schulte.eric at gmail dot com, davison at stats dot ox dot ac dot uk
8 #+LANGUAGE:   en
9 #+CATEGORY:   worg
11 # #+INFOJS_OPT: view:content
13 #+begin_html
14   <div id="subtitle">
15     <p>executable source code blocks in org-mode</p>
16   </div>
17   <div id="logo">
18     <p>
19       <img src="images/tower-of-babel.png"  alt="images/tower-of-babel.png"/>
20       <div id="attr">
21         by
22         <a href="http://commons.wikimedia.org/wiki/Pieter_Brueghel_the_Elder" title="">
23           <b>Pieter Brueghel the Elder</b>
24         </a>
25       </div>
26       <p>
27         And the Lord said, Behold, the people is one, and they have all
28         one language; and this they begin to do; and now nothing will be
29         restrained from them, which they have imagined to do. Genesis
30         11:1-9
31       </p>
32     </p>
33   </div>
34 #+end_html
36 #+begin_html
37   <p>
38   </p>
39 #+end_html
41 * Introduction
42   :PROPERTIES:
43   :CUSTOM_ID: introduction
44   :END:
45   Org-babel is an extension to the very excellent [[http://orgmode.org/][Org-mode]], providing
46   the ability to execute source code in many different languages
47   within org-mode documents. The results of code execution --- text,
48   tables and graphics --- can be integrated into the powerful
49   publishing facilities of org-mode. Org-mode is an [[http://www.gnu.org/software/emacs/][Emacs]] major mode
50   for doing almost anything with plain text.  If you are not familiar
51   with Org-mode please take a moment to read [[http://orgmode.org/][the Org-mode homepage]]
52   before continuing.
54   Org-babel provides the following modifications to [[http://orgmode.org/manual/Literal-examples.html][the existing
55   support]] for blocks of source code examples in the org-mode core.
57   1. Interactive source code execution
58   2. Arguments to source code blocks
59   3. Exportation of source code blocks to files (literate programming)
61 * Getting started
62   :PROPERTIES:
63   :CUSTOM_ID: getting-started
64   :END:
66   1) Grab the latest code from the git repo at [[http://github.com/eschulte/org-babel/tree/master][github/org-babel]]
67      #+begin_src sh
68      git clone git://github.com/eschulte/org-babel.git
69      #+end_src
71   2) Add the following lines to your .emacs, replacing the path as
72      appropriate. A good place to check that things are up and running
73      would the examples in [[* Basic org-babel functionality][Basic org-babel functionality]].
74      #+begin_src emacs-lisp
75        (add-to-list 'load-path "/path/to/org-babel/lisp")
76        (require 'org-babel-init)
77      #+end_src
79   3) Finally, activate the subset of supported Org-babel languages
80      which you want to be able to execute on your system. As an
81      example, the following activates python, ruby and R. For a full
82      list of languages, with notes on their dependencies see the
83      [[#reference-and-documentation][Reference / Documentation]] section below.
84      #+begin_src emacs-lisp
85        (require 'org-babel-python)
86        (require 'org-babel-ruby)
87        (require 'org-babel-R)
88        ;;
89        ;; Once you've activated languages, load the library of babel to
90        ;; make pre-built helper functions available in the languages you will be using.
91        (org-babel-load-library-of-babel)
92      #+end_src
93   
94 * Basic org-babel functionality
95   :PROPERTIES:
96   :CUSTOM_ID: basic-functionality
97   :END:
98 *** Source code blocks
99     :PROPERTIES:
100     :CUSTOM_ID: source-code-blocks
101     :END:
103     Org-babel is all about *source code blocks* in org mode. These are
104     blocks of code (in whatever language), that can occur anywhere in
105     an org-mode file. For example, the following is a source block
106     containing [[http://www.ruby-lang.org/][ruby]] code:
108 : #+begin_src ruby
109 : "This file was last evaluated on #{Date.today}"
110 : #+end_src
112 If you are unfamiliar with the notion of a source code block in
113 org-mode, please have a look at the [[http://orgmode.org/manual/Literal-examples.html][relevant manual section]] before
114 proceding.
116 Note that above is what the source block looks like in the org-mode
117 file. We had to take [[http://orgmode.org/manual/Literal-examples.html#Literal-examples][special steps]] to make it look that way in the
118 HTML output. Normally, when exported to HTML, source blocks are
119 fontified according to their language, and the begin_src...end_src
120 mark-up is omitted, like this:
122 #+begin_src ruby
123 "This file was last evaluated on #{Date.today}"
124 #+end_src
126 From now on, if you are viewing the HTML version, you will see the
127 HTML output only. However, much of this document consists of
128 interactive examples, and therefore in order to get a feeling for the
129 mechanics of Org-babel it might make most sense to grab the plain text
130 version of this file
131 #+HTML: <a href="org-babel-worg.org">org-babel-worg.org</a>
132 and work through it in Emacs. Alternatively the htmlized
133 version of the plain text of this file at
134 #+HTML: <a href="org-babel-worg.org.html">org-babel-worg.html</a>
135 allows the plain text version to be viewed (non-interactively) in a web browser.
136 *** Source code execution
137     :PROPERTIES:
138     :CUSTOM_ID: source-code-execution
139     :END:
140 For interpreted languages such as shell, python, R, etc, org-babel
141 allows source blocks to be executed: the code is passed to the
142 interpreter and you have control over what is done with the results of
143 execution. Here are three examples of code blocks in three different
144 languages, followed by their output. If you are viewing the plain text
145 version of this document in emacs, place point anywhere inside the
146 blocks and use =C-c C-c= to run the code[fn:1] (and feel free to alter
147 it!).
149 **** Ruby
150 #+begin_src ruby
151 "This file was last evaluated on #{Date.today}"
152 #+end_src
154 #+resname:
155 : This file was last evaluated on 2009-08-09
157 **** [[http://www.r-project.org/][R]] 
158 #+begin_src R :results value
159 matrix(rnorm(6), nrow=2)
160 #+end_src
162 #+resname:
163 | -0.138279734486552 |   -2.2476234005706 | -0.0839549402407832 |
164 | 0.0730510956002737 | 0.0634015508602321 |   0.174013159381603 |
166 **** [[http://ditaa.sourceforge.net/][ditaa]]
167 #+begin_src ditaa :file images/blue.png :cmdline -r
168 +---------+
169 | cBLU    |
170 |         |
171 |    +----+
172 |    |cPNK|
173 |    |    |
174 +----+----+
175 #+end_src
177 #+resname:
178 [[file:images/blue.png]]
180 *** Source code block syntax
182 The basic syntax of source-code blocks in Org-babel is as follows:
184 : #+srcname: name(arguments)
185 : #+begin_src language header-arguments
186 : body
187 : #+end_src
189 - name :: This name is associated with the source-code block.  This is
190      similar to the =#+tblname= lines which can be used to name tables
191      in org-mode files.  By referencing the srcname of a source-code
192      block it is possible to evaluate the block from other places,
193      files, or from inside tables.
194 - arguments :: Code blocks can have arguments (see [[#arguments-to-source-code-blocks][below]]) which are
195                provided using a familiar function-call syntax similar
196                to (e.g.)  python or R.
197 - language :: The language of the code in the source-code block. Valid
198      values must be members of `org-babel-interpreters'.
199 - header-arguments :: Header arguments control many facets of the
200      evaluation and output of source-code blocks.  See the [[* Header Arguments][Header
201      Arguments]] section for a complete review of available header
202      arguments.
203 - body :: The actual source code which will be evaluated.  An
204           important key-binding to become familiar with is =C-c
205           '=. This calls `org-edit-special' which brings up an edit
206           buffer containing the code using the emacs major mode
207           appropriate to the language.
209 *** What happens to the results?
210     :PROPERTIES:
211     :CUSTOM_ID: results
212     :END:
213     Org-babel provides two fundamentally different modes for capturing
214     the results of code evaluation, specified by the =:results= header
215     argument.
216 **** =:results value= (functional mode)
217      This means that the 'result' of code evaluation is defined to be
218      the *value* of the last statement in the block. Thus with this
219      setting, one can view the code block as a function with a return
220      value. And not only can you view it that way, but you can
221      actually use the return value of one source block as input for
222      another (see [[meta-programming-language]]). This setting is the
223      default.
224      
225      As an example, consider the following block of python code and its
226      output.
228 #+begin_src python :results value
229 import time
230 print("Hello, today's date is %s" % time.ctime())
231 print('Two plus two is')
232 2 + 2
233 #+end_src
235 #+resname:
236 : 4
238 Notice that in functional mode, the output consists of the value of
239 the last statement, and nothing else.
241 **** =:results output= (scripting mode)
242      With this setting, org-babel captures all the text output of the
243      code block and places it in the org buffer. One can think of this
244      as a 'scripting' mode: the code block contains a series of
245      commands, and you get the output of all the commands. Unlike in
246      the 'functional' mode, the code block has no return value. (This
247      mode will be more familiar to Sweave users).
249      Now consider the result of evaluating the same source block as
250      before, but under scripting mode.
252 #+srcname: name
253 #+begin_src python :results output
254 import time
255 print("Hello, today's date is %s" % time.ctime())
256 print('Two plus two is')
257 2 + 2
258 #+end_src
260 #+resname: name
261 : Hello, today's date is Fri Sep  4 19:49:06 2009
262 : Two plus two is
264 Again, we got what we asked for: all the text output (stdout) from
265 python. Since we didn't print the last value (2 + 2), we didn't get it
266 in our output.
268 *** Arguments to source code blocks
269     :PROPERTIES:
270     :CUSTOM_ID: arguments-to-source-code-blocks
271     :END:
272     In addition to evaluation of code blocks, org-babel allows them to
273     be parameterised (i.e. have arguments). Thus source code blocks
274     now have the status of *functions*. Arguments to code blocks can
275     be used in both functional and scripting mode.
277 **** Simple example of using a source block as a function
279      First let's look at a very simple example. The following source
280      block defines an org-babel function that will square its input.
282 #+srcname: square(x)
283 #+begin_src python
285 #+end_src
287 In the org-mode file that looks like this:
288 : #+srcname: square(x)
289 : #+begin_src python
290 : x*x
291 : #+end_src
294 Now we use the source block:
296 : #+lob: square(x=6)
297 (/for information on the/ =lob= /syntax see [[library-of-babel]]/)
299 #+lob: square(x=6)
301 #+resname: square(x=6)
302 : 36
304 **** A more complex example: using an org-table as input
306      In this example we're going to define a function to compute a
307      Fibonacci sequence, and we're going to make it take its input
308      from a table in the org-mode buffer.
310      Here are the inputs for fibonacci-seq:
312 #+tblname: fibonacci-inputs
313 | 1 | 2 | 3 | 4 |  5 |  6 |  7 |  8 |  9 | 10 |
314 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 |
316 in the Org-mode buffer this looks like
317 : #+tblname: fibonacci-inputs
318 : | 1 | 2 | 3 | 4 |  5 |  6 |  7 |  8 |  9 | 10 |
319 : | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 |
321 [[http://www.gnu.org/software/emacs/manual/elisp.html][Emacs Lisp]] source code
322 #+srcname: fibonacci-seq(fib-inputs=fibonacci-inputs)
323 #+begin_src emacs-lisp
324   (defun fibonacci (n)
325     (if (or (= n 0) (= n 1))
326         n
327       (+ (fibonacci (- n 1)) (fibonacci (- n 2)))))
328   
329   (mapcar (lambda (row)
330             (mapcar #'fibonacci row)) fib-inputs)
331 #+end_src
333 in the Org-mode buffer this looks like
334 : #+srcname: fibonacci-seq(fib-inputs=fibonacci-inputs)
335 : #+begin_src emacs-lisp
336 :   (defun fibonacci (n)
337 :     (if (or (= n 0) (= n 1))
338 :         n
339 :       (+ (fibonacci (- n 1)) (fibonacci (- n 2)))))
340 :   
341 :   (mapcar (lambda (row)
342 :             (mapcar #'fibonacci row)) fib-inputs)
343 : #+end_src
345 Results of Emacs Lisp code evaluation
346 #+resname:
347 | 1 | 1 | 2 |  3 |  5 |   8 |  13 |  21 |   34 |   55 |
348 | 1 | 3 | 8 | 21 | 55 | 144 | 377 | 987 | 2584 | 6765 |
350 * A meta-programming language for org-mode
351   :PROPERTIES:
352   :CUSTOM_ID: meta-programming-language
353   :END:
355 Since information can pass freely between source-code blocks and
356 org-mode tables you can mix and match languages using each language
357 for those tasks to which it is suited.  This makes Org-mode files with
358 Org-babel into a kind of meta-functional programming language in which
359 functions from many languages can work together.
361 As an example, lets take some system diagnostics in the shell, and
362 then graph them with R.
364 1. Shell source code
365 #+srcname: directories
366    #+begin_src bash :results replace
367    cd ~ && du -sc * |grep -v total
368    #+end_src
369 2. Results of the shell source code (on my system, grab this org-mode
370    files and try running it on your own)
371 #+resname: directories
372 |       72 | "Desktop"   |
373 | 12156104 | "Documents" |
374 |  3482440 | "Downloads" |
375 |  2901720 | "Library"   |
376 |    57344 | "Movies"    |
377 | 16548024 | "Music"     |
378 |      120 | "News"      |
379 |  7649472 | "Pictures"  |
380 |        0 | "Public"    |
381 |   152224 | "Sites"     |
382 |        8 | "System"    |
383 |       56 | "bin"       |
384 |  3821872 | "mail"      |
385 | 10605392 | "src"       |
386 |     1264 | "tools"     |
387 3. R source code (which calls the previous shell source code)
388 #+srcname: directory-pie
389    #+begin_src R :var dirs = directories :session R-pie-example
390    pie(dirs[,1], labels = dirs[,2])
391    #+end_src
392 4. Results of R code [[file:images/dirs.png]]
394 * Spreadsheet plugins for org-mode in any language
395   :PROPERTIES:
396   :CUSTOM_ID: spreadsheet
397   :END:
399 *NOTE*: Maybe in-addition-to/in-stead-of this example we should do a
400 more traditional "spreadsheet" example with R [Eric]
402 Not only can Org-babel pass entire tables of data to source code
403 blocks (see [[arguments-to-source-code-blocks]]), Org-babel can also be
404 used to call source code blocks from *within* tables using the
405 Org-mode's [[http://orgmode.org/manual/The-spreadsheet.html#The-spreadsheet][existing spreadsheet functionality]].
407 In fact the functional test suite for Org-babel is implemented as a
408 large Org-mode table.  To run the entire test suite you simple
409 evaluate the table =C-u C-c C-c=, and all of the tests are run
410 updating the table with pass/fail statistics.
412 Here's a sample of our test suite.
414 #+TBLNAME: org-babel-tests
415 | functionality    | block        | arg |    expected |     results | pass |
416 |------------------+--------------+-----+-------------+-------------+------|
417 | basic evaluation |              |     |             |             | pass |
418 |------------------+--------------+-----+-------------+-------------+------|
419 | emacs lisp       | basic-elisp  |   2 |           4 |           4 | pass |
420 | shell            | basic-shell  |     |           6 |           6 | pass |
421 | ruby             | basic-ruby   |     |   org-babel |   org-babel | pass |
422 | python           | basic-python |     | hello world | hello world | pass |
423 | R                | basic-R      |     |          13 |          13 | pass |
424 #+TBLFM: $5='(if (= (length $3) 1) (progn (message (format "running %S" '(sbe $2 (n $3)))) (sbe $2 (n $3))) (sbe $2))::$6='(if (string= $4 $5) "pass" (format "expected %S but was %S" $4 $5))
425 #+TBLFM: $5=""::$6=""
427 *** code blocks for tests
429 #+srcname: basic-elisp
430 #+begin_src emacs-lisp :var n=7
431 (* 2 n)
432 #+end_src
434 #+srcname: basic-shell
435 #+begin_src sh :results silent
436 expr 1 + 5
437 #+end_src
439 #+srcname: date-simple
440 #+begin_src sh :results silent
441 date
442 #+end_src
444 #+srcname: basic-ruby
445 #+begin_src ruby :results silent
446 "org-babel"
447 #+end_src
449 #+srcname: basic-python
450 #+begin_src python :results silent
451 'hello world'
452 #+end_src
454 #+srcname: basic-R
455 #+begin_src R :results silent
456 b <- 9
457 b + 4
458 #+end_src
460 * Library of Babel
461   :PROPERTIES:
462   :CUSTOM_ID: library-of-babel
463   :END:
464   What about those source code blocks which are so useful you want to
465   have them available in every org-mode buffer?
467   The [[file:library-of-babel.org][Library of Babel]] is an extensible collection of ready-made and
468   easily-shortcut-callable source-code blocks for handling common
469   tasks.  Org-babel comes pre-populated with the source-code blocks
470   located in the [[file:library-of-babel.org][library-of-babel.org]] file. It is possible to add
471   source-code blocks from any org-mode file to the library by calling
473   #+srcname: add-file-to-lob
474   #+begin_src emacs-lisp 
475   (org-babel-lob-ingest "path/to/file.org")
476   #+end_src
478   Once a function has been loaded into the Library of Babel it can be
479   called using the following short =lob= notation.
480   : #+lob: square(x=6)
482   Note that it is also possible to pass table values or the output of
483   a source-code block to lob functions, and it is possible to
484   reference lob functions in source block arguments.
486 * Reproducible Research
487   :PROPERTIES:
488   :CUSTOM_ID: reproducable-research
489   :END:
490 #+begin_quote 
491 An article about computational science in a scientific publication is
492 not the scholarship itself, it is merely advertising of the
493 scholarship. The actual scholarship is the complete software
494 development environment and the complete set of instructions which
495 generated the figures.
497 -- D. Donoho
498 #+end_quote
500 [[http://reproducibleresearch.net/index.php/Main_Page][Reproducible Research]] (RR) is the practice of distributing along with
501 an article of research all data, code, and tools required to reproduce
502 the results discussed in the paper.  As such the paper becomes not
503 only a document describing the research but a complete laboratory in
504 which the research can be reproduced and extended.
506 Org-mode already has exceptional support for [[http://orgmode.org/manual/Exporting.html#Exporting][exporting to html and
507 LaTeX]].  Org-babel makes Org-mode a tool for RR by *activating* the
508 data and source code embedded into Org-mode documents making the
509 entire document executable.  This makes it not only possible, but
510 natural to distribute research in a format that encourages readers to
511 recreate your results, and perform their own analysis.
513 One notable existing RR tool is [[http://en.wikipedia.org/wiki/Sweave][Sweave]] which provides for the
514 embedding of [[http://www.r-project.org/][R]] code into LaTeX documents.  While Sweave is a mature
515 and very useful tool, we believe that Org-babel has several
516 advantages:
517  - It supports multiple languages (we're not aware of other RR tools that do this)
518  - The [[http://orgmode.org/manual/Exporting.html#Exporting][export process]] is flexible and powerful, including HTML as a target in addition to LaTeX
519  - The document can make native use of all the features of Org-mode,
520    such as those for [[http://orgmode.org/manual/Agenda-Views.html#Agenda-Views][project planning]] and [[http://orgmode.org/manual/TODO-Items.html#TODO-Items][task management]]
522 * Literate programming
523   :PROPERTIES:
524   :CUSTOM_ID: literate-programming
525   :END:
527 #+begin_quote 
528 Let us change our traditional attitude to the construction of
529 programs: Instead of imagining that our main task is to instruct a
530 /computer/ what to do, let us concentrate rather on explaining to
531 /human beings/ what we want a computer to do.
533 The practitioner of literate programming can be regarded as an
534 essayist, whose main concern is with exposition and excellence of
535 style. Such an author, with thesaurus in hand, chooses the names of
536 variables carefully and explains what each variable means. He or she
537 strives for a program that is comprehensible because its concepts have
538 been introduced in an order that is best for human understanding,
539 using a mixture of formal and informal methods that reinforce each
540 other.
542  -- Donald Knuth
543 #+end_quote
545 Org-babel supports [[http://en.wikipedia.org/wiki/Literate_programming][Literate Programming]] (LP) by allowing the act of
546 programming to take place inside of Org-mode documents.  The Org-mode
547 file can then be exported (*woven* in LP speak) to html or LaTeX for
548 consumption by a human, and the embedded source code can be extracted
549 (*tangled* in LP speak) into structured source code files for
550 consumption by a computer.
552 To support these operations Org-babel relies on Org-mode's [[http://orgmode.org/manual/Exporting.html#Exporting][existing
553 exporting functionality]] for *weaving* of documentation, and on the
554 =org-babel-tangle= function which makes use of [[http://www.cs.tufts.edu/~nr/noweb/][Noweb]] [[reference-expansion][reference syntax]]
555 for *tangling* of code files.
557 The [[literate-programming-example][following example]] demonstrates the process of *tangling* in
558 Org-babel.
560 *** Simple Literate Programming Example (Noweb syntax)
561     :PROPERTIES:
562     :CUSTOM_ID: literate-programming-example
563     :END:
565 Tangling functionality is controlled by the =tangle= family of
566 [[header-arguments]].  These arguments can be used to turn tangling on or
567 off (the default) on the source code block, or the outline heading
568 level.
570 The following demonstrates the combination of three source code blocks
571 into a single source code file using =org-babel-tangle=.
573 The following two blocks will not be tangled by default since they
574 have no =tangle= header arguments.
576 #+srcname: hello-world-prefix
577 #+begin_src sh :exports none
578   echo "/-----------------------------------------------------------\\"
579 #+end_src
581 : #+srcname: hello-world-prefix
582 : #+begin_src sh :exports none
583 :   echo "/-----------------------------------------------------------\\"
584 : #+end_src
586 #+srcname: hello-world-postfix
587 #+begin_src sh :exports none
588   echo "\-----------------------------------------------------------/"
589 #+end_src
591 : #+srcname: hello-world-postfix
592 : #+begin_src sh :exports none
593 :   echo "\-----------------------------------------------------------/"
594 : #+end_src
597 The third block does have a =tangle= header argument indicating the
598 name of the file to which it should be written.  It also has [[http://www.cs.tufts.edu/~nr/noweb/][Noweb]]
599 style references to the two previous source code blocks which will be
600 expanded during tangling to include them in the output file as well.
602 #+srcname: hello-world
603 #+begin_src sh :tangle hello :exports none
604   # <<hello-world-prefix>>
605   echo "|                       hello world                         |"
606   # <<hello-world-postfix>>
607 #+end_src
609 : #+srcname: hello-world
610 : #+begin_src sh :tangle hello :exports none
611 :   # <<hello-world-prefix>>
612 :   echo "|                       hello world                         |"
613 :   # <<hello-world-postfix>>
614 : #+end_src
616 Calling =org-babel-tangle= will result in the following being written
617 to the =hello.sh= file.
619 #+srcname: hello-world-output
620 #+begin_src sh 
621   #!/usr/bin/env sh
622   # generated by org-babel-tangle
623   
624   # [[file:~/src/org-babel/org-babel-worg.org::#literate-programming-example][block-16]]
625   # <<hello-world-prefix>>
626   echo "/-----------------------------------------------------------\\"
627   
628   echo "|                       hello world                         |"
629   # <<hello-world-postfix>>
630   echo "\-----------------------------------------------------------/"
631   # block-16 ends here
632 #+end_src
634 *** Emacs Initialization with Org-babel
635 Org-babel has special support for embedding your emacs initialization
636 into Org-mode files.  The =org-babel-load-file= function can be used
637 to load the emacs lisp embedded in a literate Org-mode file in the
638 same way that you might load a regular elisp file.
640 This allows you to have all the niceness of Org-mode (folding, tags,
641 notes, html export, etc...) available in your emacs initialization.
643 To try this out either see the simple [[literate-emacs-init][Literate Emacs Initialization]]
644 example directly below, or check out the Org-babel Literate
645 Programming version of Phil Hagelberg's excellent [[http://github.com/technomancy/emacs-starter-kit/tree/master][emacs-starter-kit]]
646 available at [[http://github.com/eschulte/emacs-starter-kit/tree/master][Org-babel-emacs-starter-kit]].
648 ***** Literate Emacs Initialization
649       :PROPERTIES:
650       :CUSTOM_ID: literate-emacs-init
651       :END:
653 For a simple example of usage follow these 4 steps.
655 1) create a directory named =.emacs.d= in the base of your home
656    directory.
657    #+begin_src sh 
658    mkdir ~/.emacs.d
659    #+end_src
660 2) checkout the latest versions of Org-mode and Org-babel into the src
661    subdirectory of this new directory
662    #+begin_src sh
663    cd ~/.emacs.d
664    mkdir src
665    cd src
666    git clone git://repo.or.cz/org-mode.git
667    git clone git://github.com/eschulte/org-babel.git
668    #+end_src
669 3) place the following in a file called =init.el= in your emacs
670    initialization directory (=~/.emacs.d=).
671    #+srcname: emacs-init
672    #+begin_src emacs-lisp 
673      ;;; init.el --- Where all the magic begins
674      ;;
675      ;; This file loads both
676      ;; - Org-mode : http://orgmode.org/ and
677      ;; - Org-babel: http://eschulte.github.com/org-babel/
678      ;;
679      ;; It then loads the rest of our Emacs initialization from Emacs lisp
680      ;; embedded in literate Org-mode files.
681      
682      ;; Load up Org Mode and Org Babel for elisp embedded in Org Mode files
683      (setq dotfiles-dir (file-name-directory (or (buffer-file-name) load-file-name)))
684      (add-to-list 'load-path (expand-file-name
685                               "lisp" (expand-file-name
686                                       "org" (expand-file-name
687                                              "src" dotfiles-dir))))
688      (add-to-list 'load-path (expand-file-name
689                               "lisp" (expand-file-name
690                                       "org-babel" (expand-file-name
691                                                    "src" dotfiles-dir))))
692      (require 'org-babel-init)
693      
694      ;; load up all literate org-mode files in this directory
695      (mapc #'org-babel-load-file (directory-files dotfiles-dir t "\\.org$"))
696      
697      ;;; init.el ends here
698    #+end_src
699 4) Implement all of your emacs customizations inside of elisp
700    source-code blocks located in Org-mode files in this directory.
701    They will be loaded by emacs on startup.
703 * Reference / Documentation
704   :PROPERTIES:
705   :CUSTOM_ID: reference-and-documentation
706   :END:
707 *** Languages
708     The following can be added to your .emacs and used to activate
709     languages.  It includes a brief list of the requirements for each
710     language.  *Note*: this also serves as the list of languages
711     currently supported by Org-babel.
712      #+begin_src emacs-lisp 
713        ;; Uncomment each of the following require lines if you want org-babel
714        ;; to support that language.  Each language has a comment explaining
715        ;; it's dependencies.  See the related files in lisp/langs for more
716        ;; detailed explanations of requirements.
717        ;; (require 'org-babel-R)         ;; R and ess-mode
718        ;; (require 'org-babel-asymptote) ;; asymptote
719        ;; (require 'org-babel-css)       ;; none
720        ;; (require 'org-babel-ditaa)     ;; ditaa
721        ;; (require 'org-babel-dot)       ;; dot
722        ;; (require 'org-babel-gnuplot)   ;; gnuplot, and gnuplot-mode
723        ;; (require 'org-babel-haskell)   ;; haskell, haskell-mode, inf-haskell
724        ;; (require 'org-babel-ocaml)     ;; ocaml, and tuareg-mode
725        ;; (require 'org-babel-python)    ;; python, and python-mode
726        ;; (require 'org-babel-ruby)      ;; ruby, irb, ruby-mode, and inf-ruby
727        ;; (require 'org-babel-sass)      ;; sass, sass-mode
728        ;; (require 'org-babel-sql)       ;; none
729      #+end_src
731 *** Header Arguments
732      :PROPERTIES:
733      :CUSTOM_ID: header-arguments
734      :END:
736 - results :: results arguments specify what should be done with the
737              output of source-code blocks
738   - The following options are mutually exclusive, and specify how the
739     results should be collected from the source-code block
740     - value ::
741     - output :: 
742   - The following options are mutually exclusive and specify what type
743     of results the code block will return
744     - vector :: specifies that the results should be interpreted as a
745                 multidimensional vector (even if the vector is
746                 trivial), and will be inserted into the org-mode file
747                 as a table
748     - scalar :: specifies that the results should be interpreted as a
749                 scalar value, and will be inserted into the org-mode
750                 file as quoted text
751     - file :: specifies that the results should be interpreted as the
752               path to a file, and will be inserted into the org-mode
753               file as a link
754   - The following options specify how the results should be inserted
755     into the org-mode file
756     - replace :: the current results replace any previously inserted
757                  results from the code block
758     - silent :: rather than being inserted into the org-mode file the
759                 results are echoed into the message bar
760 - exports :: exports arguments specify what should be included in html
761              or latex exports of the org-mode file
762   - code :: the body of code is included into the exported file
763   - results :: the results of evaluating the code is included in the
764                exported file
765   - both :: both the code and results are included in the exported
766             file
767   - none :: nothing is included in the exported file
768 - tangle :: tangle arguments specify whether or not the source-code
769             block should be included in tangled extraction of
770             source-code files
771   - yes :: the source-code block is exported to a source-code file
772            named after the basename (name w/o extension) of the
773            org-mode file
774   - no :: (default) the source-code block is not exported to a
775           source-code file
776   - other :: any other string passed to the =tangle= header argument
777              is interpreted as a file basename to which the block will
778              be exported
780 *** Noweb reference syntax
781 The [[http://www.cs.tufts.edu/~nr/noweb/][Noweb]] Literate Programming system allows named blocks of code to
782 be referenced by using a =<<code-block-name>>= syntax.  When a
783 document is tangled these references are replaced with the named code.
784 An example is provided in the [[literate-programming-example]] in this
785 document.
787 * Footnotes
789 [fn:1] Calling =C-c C-o= on a source-code block will open the
790 block's results in a separate buffer.