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