#+TITLE: org-babel examples and tests #+OPTIONS: num:nil ^:nil #+STYLE: * tests and examples ** simple gnuplot plotting of Org-mode tables #+tblname: gnuplot-testing | x | y | |---+----| | 1 | 2 | | 2 | 4 | | 3 | 6 | | 4 | 8 | | 5 | 10 | | 6 | 12 | | 7 | 14 | | 8 | 16 | #+begin_src gnuplot :var data=gnuplot-testing :file output.eps :exports both set term postscript set title "test" set auto x set style data histogram set style fill solid border -1 set boxwidth 0.9 plot data using 2:xtic(1) #+end_src #+results: [[file:output.eps]] ** simple indexing #+data: list-o-numbers | 1 | 2 | 3 | | 4 | 5 | 6 | | 7 | 8 | 9 | #+begin_src sh :var column=list-o-numbers[,0] echo $column #+end_src #+results: : 1 4 7 ** convert results to all string #+source: hetero-table #+begin_src emacs-lisp '((1 2 3 4) ("a" "b" "c" "d")) #+end_src #+source: all-to-string #+begin_src emacs-lisp :var tbl='() (defun all-to-string (tbl) (if (listp tbl) (mapcar #'all-to-string tbl) (if (stringp tbl) tbl (format "%s" tbl)))) (all-to-string tbl) #+end_src #+begin_src emacs-lisp :var tbl=hetero-table (mapcar (lambda (row) (mapcar (lambda (cell) (stringp cell)) row)) tbl) #+end_src #+results: | nil | nil | nil | nil | | t | t | t | t | #+begin_src emacs-lisp :var tbl=all-to-string(hetero-table) (mapcar (lambda (row) (mapcar (lambda (cell) (stringp cell)) row)) tbl) #+end_src #+results: | t | t | t | t | | t | t | t | t | ** simple short R block #+BEGIN_SRC R c(1,23,54,5) #+END_SRC #+results: | 1 | | 23 | | 54 | | 5 | ** two blocks and a table #+source: stuff #+begin_src sh echo 1 echo 2 echo 3 #+end_src #+source: last-of-stuff #+begin_src sh :var input=stuff echo "$input" |tail -1 #+end_src | one | | two | | 3 | #+TBLFM: @3$1='(sbe last-of-stuff) ** inheriting the file property :PROPERTIES: :FILE: something.png :END: #+begin_src ditaa +-----------------------------+ | | | +-----+ | | | | +---------+ | | | | | | | | +-----+ | | | | | | | | file | | | | inheritance +---------+ | | | +-----------------------------+ #+end_src #+results: [[file:something.png]] ** a table with tags #+TBLNAME: sandbox :noexport: | 1 | 2 | 3 | | 4 | org-babel | 6 | #+begin_src emacs-lisp :var table=sandbox (message "%S" table) #+end_src #+results: : ((1 2 3) (4 "org-babel" 6)) ** shell script output not in table #+begin_src sh :results scalar echo 1 echo 2 echo 3 #+end_src #+results: : 1 : 2 : 3 ** inline code block and downstream src blocks AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA something src_sh{echo eric} CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC #+begin_src sh echo schulte #+end_src EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE *** with R AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA blah blah src_R[:results output]{cat(rnorm(2))} CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC #+begin_src R :eval never :exports none 1+2 a <- b + c xyz #+end_src DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD ** updating a table #+results: this-is-another-table | 0 | 0 | 0 | | 0 | 0 | 0 | | 0 | 0 | 0 | #+source: this-is-another-table #+begin_src emacs-lisp :var table=this-is-another-table (setf (nth 1 table) '(2 2 2)) table #+end_src ** space around exported code blocks try evaluating the following blocks, then removing their results with M-x `org-babel-remove-result' Verbiage to begin the paragraph #+begin_src sh echo eric #+end_src and verbiage to end the same paragraph. #+begin_src sh echo 1 echo 2 #+end_src and verbiage to end the same paragraph. ** simple ditaa block #+begin_src ditaa :file work-flow.png +-------+ +--------+ | | | | | Org |------------>| Tex | | | | | +-------+ +--------+ #+end_src #+results: [[file:work-flow.png]] ** replacing a table #+results: this-is-the-table | 1 | 4 | 7 | | 2 | 5 | 8 | | 3 | 6 | 9 | #+source: this-is-the-table #+begin_src emacs-lisp :var table=this-is-the-table (mapcar (lambda (row) (mapcar (lambda (cell) (* cell 2)) row)) table) #+end_src ** ruby code block #+begin_src ruby 1+2 #+end_src #+results: : 3 ** bug with undefined function copy-seq #+begin_src R :session :exports both 1:10 #+end_src ** tangle before evaluating a code block #+begin_src sh :var TANGLED=(org-babel-tangle) :tangle no wc $TANGLED #+end_src #+results: : 2 2 11 it.sh #+begin_src sh :tangle it.sh echo TEST #+end_src ** plot results with org-plot #+source: disk-usage #+begin_src sh :exports both df #+end_src #+PLOT: title:"Disk Usage" ind:6 deps:(5) type:2d with:histograms set:"yrange [0:100]" #+results: disk-usage | Filesystem | 1K-blocks | Used | Available | Use% | Mounted | on | | /dev/sda6 | 28835836 | 8447712 | 18923344 | 31% | / | | | none | 2997072 | 676 | 2996396 | 1% | /dev | | | none | 3006056 | 0 | 3006056 | 0% | /dev/shm | | | none | 3006056 | 96 | 3005960 | 1% | /var/run | | | none | 3006056 | 0 | 3006056 | 0% | /var/lock | | | /dev/sda7 | 144176824 | 72225604 | 64627420 | 53% | /home | | ** premature truncation of emacs-lisp results #+begin_src emacs-lisp '(nil nil nil nil) #+end_src #+results: : (nil nil nil nil) ** non-defined code blocks can still tangle #+begin_src text :tangle somewhere.txt This will still tangle out to a file, and it opens in text mode, which may be nice. #+end_src ** expand noweb refs #+source: def-something #+begin_src sh SOMETHING=nothing #+end_src #+begin_src sh <> echo $SOMETHING #+end_src ** returning file names -- interpreted as lists #+begin_src sh :results scalar echo "[[file:./cv.cls]]" #+end_src #+results: : [[file:./cv.cls]] #+begin_src sh :results org echo "[[file:./cv.cls]]" #+end_src #+results: #+BEGIN_ORG [[file:\./cv\.cls]] #+END_ORG ** java code block #+begin_src java :classname myfirstjavaprog class myfirstjavaprog { public static void main(String args[]) { System.out.println("Hello World!"); } } #+end_src #+results: : Hello World! ** exporting a ditaa example #+begin_src ditaa :file blue.png :cmdline -r /---------------\ | test | | {cBLU} | \---------------/ #+end_src #+results: [[file:blue.png]] ** including noweb refs w/o last newline #+begin_src sh :noweb yes <> |\ <> #+end_src #+source: my-name #+begin_src sh echo "eric schulte" #+end_src #+source: capitalize-name #+begin_src sh sed 's/^e/E/;s/ s/ S/' #+end_src ** simple sbe example | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 3.5 | #+TBLFM: @7$1='(sbe mean (lst @1..@6)) #+source: mean #+begin_src emacs-lisp :var lst=() (let ((num (car lst)) (nums (cdr lst))) (/ (float (+ num (apply #'+ nums))) (1+ (length nums)))) #+end_src ** eval never #+begin_src emacs-lisp :eval (if org-export-current-backend "never" "yes") :exports results (message "launch missles") #+end_src #+results: : launch missles ** indexing into a list variable #+begin_src emacs-lisp :var lst='(0 1 2) (first lst) #+end_src #+results: : 0 or as a noweb reference #+data: external-list - 0 - 1 - 2 #+begin_src sh :noweb yes echo <> #+end_src #+results: : 0 ** data alias for resname #+data: blah : blahcontent #+begin_src emacs-lisp :var it=blah it #+end_src #+results: : blahcontent ** define a block with a name for noweb expansion :PROPERTIES: :tangle: yes :noweb: yes :END: #+source: simple(something="something") #+begin_src emacs-lisp something #+end_src another block including the first block #+begin_src emacs-lisp <> #+end_src ** find a resource by global id #+begin_src emacs-lisp :var it=990f3218-6fce-44fb-bd0c-5f6076c0dadc it #+end_src #+results: : : here it is *** I'm the resource :PROPERTIES: :ID: 990f3218-6fce-44fb-bd0c-5f6076c0dadc :END: here it is ** longtable label and attr lines on code block results #+source: faz #+begin_src emacs-lisp :exports results '((foo foo) (bar baz)) #+end_src #+LABEL: Foo #+results: faz | foo | foo | | bar | baz | ** another test #+source: square #+begin_src emacs-lisp :var it=0 (* it it) #+end_src Here is a call_square(it=4), stuck in the middle of some prose. Here is another 25^2=call_square(it=25). ** multiple variables Should work call_concat(1, 2, 3) =123= in order. #+source: concat #+begin_src emacs-lisp :var a=0 :var b=0 :var c=0 (format "%S%S%S" a b c) #+end_src Should be positive call_lob-minus(4, 3) =1= by order. ** un-named variables #+source: square #+begin_src emacs-lisp :var it=0 (* it it) #+end_src #+call: square(8) first we can name the argument with call_square(it=4) =16= then we can pass the argument unnamed with call_square(4) =16= #+source: minus #+begin_src emacs-lisp :var a=0 :var b=0 (- a b) #+end_src To ensure that these arguments are passed in the correct order we can use the following call_minus(8, 4) =-4= ** inline call line #+source: double #+begin_src emacs-lisp :var it=0 (* 2 it) #+end_src This is the number src_sh[:var it=double(it=1)]{echo $it} in the middle The following exports as a normal call line #+call: double(it=1) Now here is an inline call call_double(it=1) stuck in the middle of some prose. This one should not be exported =call_double(it=2)= because it is quoted. Finally this next one should export, even though it starts a line call_double(it=3) because sometimes inline blocks fold with a paragraph. And, a call with raw results call_double(4)[:results raw] should not have quoted results. ** text and graphics from R #+begin_src R :results output :session print(seq(1,10)) #+end_src #+begin_src R :file example.png :results graphics :session plot(seq(1,10)) #+end_src #+results: [[file:example.png]] ** large code in inline blocks #+source: big-block #+begin_src emacs-lisp :exports none "something complex" #+end_src Here is some text with src_emacs-lisp[:var it=big-block]{it} in the middle. ** clojure =:results scalar= #+begin_src clojure :results scalar '(1 2 3) #+end_src ** expand variable during tangling :PROPERTIES: :tangle: yes :END: #+begin_src sh :var VER=(vc-working-revision (buffer-file-name)) echo $VER #+end_src ** python session #+begin_src python :results output :session mypy x = 1 for i in range(1,5): x = x + i print x print "I throw an error" #+end_src #+results: : : ... ... ... 2 : 4 : 7 : 11 : I throw an error #+begin_src python :results output :session print y #+end_src #+results: : Traceback (most recent call last): : File "", line 1, in : NameError: name 'y' is not defined ** scalar emacs lisp results #+begin_src emacs-lisp :results scalar '(1 2 3) #+end_src #+results: : (1 2 3) ** named code block export This has a name which is not exported. #+source: rand(n) #+begin_src R runif(n) #+end_src ** continued code blocks :PROPERTIES: :tangle: yes :comments: yes :END: #+source: foo #+begin_src emacs-lisp (message "foo:%S" 1) #+end_src #+begin_src emacs-lisp (message "un-named") #+end_src #+source: bar #+begin_src emacs-lisp (message "bar:%S" 1) #+end_src #+source: foo #+begin_src emacs-lisp (message "foo:%S" 2) #+end_src #+source: bar #+begin_src emacs-lisp (message "bar:%S" 2) #+end_src #+begin_src emacs-lisp :tangle no :results silent (with-temp-buffer (insert-file-contents "scraps.el") (eval-buffer)) #+end_src ** ruby with xmpfilter #+begin_src ruby :results xmp code 2 + 2 # => 3.times{ puts :hello } #+end_src #+results: #+BEGIN_SRC ruby 2 + 2 # => 4 3.times{ puts :hello } # >> hello # >> hello # >> hello #+END_SRC ** tangle test #+begin_src R :tangle test.R :shebang #!/bin/cat :padline no This is a test #+end_src ** quick testing new session code #+begin_src sh :session test :results output echo foo #+end_src #+results: : foo #+begin_src ruby :results output :session simple puts "foo" #+end_src #+results: : foo ** =:file= and python #+begin_src python :file /tmp/test.png return 1 #+end_src #+results: [[file:/tmp/test.png]] ** simple shell #+begin_src sh sleep 10 && ls #+end_src #+results: | _config.yml | | data | | development.org | | elsevier | | index.org | | paper | | publish.org | | scraps | | scraps.html | | scraps.org | | scraps.tex | #+begin_src ruby :session eric puts [1..4] #+end_src #+results: : nil ** testing new data names #+data: simple-123 : 123 #+begin_src emacs-lisp :var simple=simple-123 :exports both (* simple 2) #+end_src results data my data is results #+results: : 246 ** default directory examples in lisp #+begin_src lisp *default-pathname-defaults* #+end_src #+begin_src sh pwd #+end_src #+begin_src lisp :dir *default-pathname-defaults* #+end_src #+results: : #P"" ** lisp body with multiple forms #+begin_src lisp :results value (format t "~&eric") (+ 1 2) #+end_src #+results: : 3 ** example =#+call= line expansion this code block peeks inside of the `params' variable which is used by babel during code block evaluation #+source: callee #+begin_src emacs-lisp ;; this is cheating and shouldn't be done in user code :) (or (cdr (assoc :foo params)) 'unset) #+end_src this code block evaluates to the following, #+results: callee : unset this call line, #+call: callee[:foo bar]() :results org expands into the following which is evaluated as a normal code block by Babel. #+begin_src emacs-lisp :var results=callee[:foo bar]() :results org results #+end_src this code block evaluates to the following, #+results: #+BEGIN_ORG nothing #+END_ORG ** awk example #+results: simple-table | 1 | 2 | 3 | | 4 | 5 | 6 | | 7 | 8 | 9 | #+begin_src awk :stdin simple-table {print $1} #+end_src #+results: | 1 | | 4 | | 7 | ** passing values through to STDIN of shell code blocks #+results: square-table | 1 | 2 | 3 | | 4 | 5 | 6 | | 7 | 8 | 9 | #+source: first-col #+begin_src sh :stdin square-table awk '{print $1}' #+end_src #+begin_src sh :stdin first-col sed 's/4/middle/g' #+end_src #+results: | 1 | | middle | | 7 | ** don't match end_src inside of a block #+srcname: the_issue #+begin_src sh :results output echo '#+end_src' #+end_src #+results: the_issue : #+end_src block ** append tables #+data: table-names - first-table - second-table - third-table #+data: first-table | a | 1 | | b | 2 | #+data: second-table | c | 3 | | d | 4 | #+data: third-table | e | 5 | | f | 6 | #+begin_src emacs-lisp :var table-names=table-names (mapcar #'org-babel-ref-resolve table-names) #+end_src #+results: | (a 1) | (b 2) | | (c 3) | (d 4) | | (e 5) | (f 6) | ** new names for results #+results: simple : 1 #+begin_src emacs-lisp :var data=simple data #+end_src #+results: : 1 changing the variable used to label data #+begin_src emacs-lisp :results silent (setq org-babel-result-fmt "^[ \t]*#\\+\\(TBLNAME\\|RESNAME\\|RESULTS\\|DATA\\):[ \t]*%s[ \t]*$") #+end_src #+data: not-so-simple : 2 #+begin_src emacs-lisp :var data=not-so-simple data #+end_src #+results: : 2 ** ocaml #+begin_src ocaml [3;2;3] @ [3;2;3;4;5];; #+end_src #+results: | 3 | 2 | 3 | 3 | 2 | 3 | 4 | 5 | ** simple latex verbatim wrap example #+begin_src emacs-lisp :results silent (setq org-export-latex-verbatim-wrap '("{\\scriptsize\n\\begin{verbatim}\n" . "\\end{verbatim}\n}\n")) #+end_src #+begin_src sh echo eric schulte echo another #+end_src ** inserting complicated results #+begin_src emacs-lisp :results scalar ((lambda (result) (condition-case nil (read result) (error result))) "(:return (:ok \"{:model #, :v #, :z nil}\") 13)") #+end_src #+results: | :return | (:ok {:model #, :v #, :z nil}) | 13 | ** automatic org-mode formatting #+source: raw-results #+begin_src sh :results output tabular echo "| 1 |" echo "| 2 |" #+end_src #+begin_src emacs-lisp :var in=raw-results (stringp in) #+end_src #+results: : t ** units in R plot #+begin_src R :results graphics :file test.png :width 8 :height 8 :res 200 :units cm x <- -10:10 y <- x^2 plot(x, y, type="l", col="red", lty=1) #+end_src #+results: [[file:test.png]] ** Ocaml appending blocks block #+begin_src ocaml [|1;2;3|];; #+end_src #+results: | 1 | 2 | 3 | ** simple Oz example #+begin_src oz :results output {Browse 'Hello'} #+end_src ** complex numbers in tables and python, reference in table formula #+source: parameter-variation(data=0) #+begin_src python :result values return 'text' #+end_src |---------------------------------------| | "(0.0331901438056,0.000535222885197)" | | "(0.0333434157791,0.000537930174356)" | | "(0.0345727512157,0.000559346040457)" | | "(0.0353146483908,0.000571501584524)" | | "(0.0355522909393,0.000574387067408)" | | "(0.0356575682336,0.000574851263615)" | | "(0.0357806926897,0.000575051685084)" | |---------------------------------------| | text | #+TBLFM: @8$1='(sbe parameter-variation (nums @1$1..@7$1)) | '(1 2 3 4) | |------------| | 4 | #+TBLFM: @2$1='(sbe quote (it @1$1)) | (1 2 3 4) | |-----------| | #ERROR | #+TBLFM: @2$1='(sbe quote (it @1$1)) *** using vectors to represent complex number is lisp | [1 2] | |------------------| | real:1 complex:2 | #+TBLFM: @2$1='(sbe real (it @1$1)) #+source: real(it='()) #+begin_src emacs-lisp (format "real:%d complex:%d" (aref it 0) (aref it 1)) #+end_src *** reference the table in a table formula #+results: complex-data |-------------------------------------| | (0.0331901438056,0.000535222885197) | |-------------------------------------| | 4 | #+TBLFM: @2$1='(sbe quote (it "complex-data")) *** externally referencing the table #+results: complex-data |-------------------------------------| | (0.0331901438056,0.000535222885197) | | (0.0333434157791,0.000537930174356) | | (0.0345727512157,0.000559346040457) | | (0.0353146483908,0.000571501584524) | | (0.0355522909393,0.000574387067408) | | (0.0356575682336,0.000574851263615) | | (0.0357806926897,0.000575051685084) | #+TBLFM: @8$1='(sbe parameter-variation (nums @1$1..@7$1)) #+begin_src python :var data=complex-data return data #+end_src #+results: | (0.0331901438056,0.000535222885197) | | (0.0333434157791,0.000537930174356) | | (0.0345727512157,0.000559346040457) | | (0.0353146483908,0.000571501584524) | | (0.0355522909393,0.000574387067408) | | (0.0356575682336,0.000574851263615) | | (0.0357806926897,0.000575051685084) | ** emacs-lisp printing with output to string #+begin_src emacs-lisp :results output (let ((dog (sqrt 2)) (cat 7)) (print (format "%s %f" "Dog: " (eval dog))) (print (format "%s %d" "Cat: " (eval cat)) nil) (print "Fish.")) #+end_src #+results: : : "Dog: 1.414214" : : "Cat: 7" : : "Fish." #+begin_src emacs-lisp (let ((dog (sqrt 2)) (cat 7)) `((dog ,dog) (cat ,cat) (fish))) #+end_src #+results: | dog | 1.4142135623730951 | | cat | 7 | | fish | | ** headers in R tables during export #+TBLNAME: Chuah07 | condition | Mean.offer | |-----------+------------| | 1.MMM | 48.49 | | 2.MMU | 42.59 | | 3.MUM | 44.87 | | 4.UMU | 46.43 | | 5.UUM | 44.15 | | 6.UUU | 43.80 | | MAL | 46.28 | | UK | 44.1 | | All | 45.29 | #+headers: :var data=Chuah07 #+begin_src R :results output :exports both :cache yes str(data) #+end_src #+results[135a7f73839b69d118780ca29a64c3840601f7b9]: : 'data.frame': 9 obs. of 2 variables: : $ condition : chr "1.MMM" "2.MMU" "3.MUM" "4.UMU" ... : $ Mean.offer: num 48.5 42.6 44.9 46.4 44.1 ... eric ** session associated with R block #+begin_src R :session *chris* x <- 1 y <- 2 y-x #+end_src #+results: : 1 ** detangling example :PROPERTIES: :tangle: yes :comments: yes :shebang: #!/bin/sh :ID: 7a22cf71-6be3-4fca-a700-4c8be8237303 :END: #+source: sh-for-tangling #+begin_src sh echo "this is the `sh-for-tangling' code block" num=`expr 1 + 1` echo "the value of num is $num" #+end_src #+begin_src sh echo "this is an unnamed code block" #+end_src ** vc-log A version control log of this file. The =vc-log= code block lives in the library of babel. #+call: vc-log() :exports results ** CL example #+begin_src lisp (defun range (n &optional m) "Return the numbers in range." (loop for num from (if m n 0) to (if m m (- n 1)) collect num)) (mapcar #'list (mapcar #'1+ (range 10))) #+end_src #+results: | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | | 10 | ** tangling out vc information #+headers: :var STATE=(vc-state (or (buffer-file-name) org-current-export-file)) #+headers: :var REV=(vc-working-revision (or (buffer-file-name) org-current-export-file)) #+begin_src sh :tangle yes rm -rf ./R rm -f ./spreadSim.sub REVISION=$REV.$STATE tar -xf nsa.$REVISION.tar.gz $HOME/R/R/R-2.12.0/bin/Rscript --vanilla -e "source('./R/generateLatinHypercubeScenarios.R'); doIt()" for SCENARIO in ./R/scenarios/*.R; do export SCENARIO=${SCENARIO#./R/scenarios/} qsub nsa.sub done #+end_src ** grabbing the current buffer during export Eric #+begin_src emacs-lisp :var buf=(buffer-file-name (current-buffer)) :exports both (message "buffer %S!" buf) #+end_src #+begin_src sh :exports results :results output git log -1 #+end_src ** colnames with call lines #+TBLNAME: data | x | parameter | value | |---+-----------+-------| | 0 | heat | 30 | | 1 | heat | 30 | #+source: func5 #+begin_src R :var name=data :var a="one" :colnames yes names(name) #+end_src #+results: func5 | x | |-----------| | x | | parameter | | value | #+call: func5(name=data, a="two") :colnames yes #+results: func5(name=data, a="two") | x | |-----------| | x | | parameter | | value | ** caching on export #+source: testcache #+begin_src R :cache yes :exports results dat <- matrix(runif(12), 3, 4) print(dat) #+end_src #+results[e7b83e61596da84f85c5a24e61569576c802f9a2]: testcache | 0.590091332094744 | 0.101750465808436 | 0.487125408137217 | 0.92315583024174 | | 0.483292032498866 | 0.427640072302893 | 0.974636133294553 | 0.995571716455743 | | 0.60190233332105 | 0.122638279106468 | 0.437959408387542 | 0.015639441087842 | ** conflicting header arguments code block #+source: conflict-block #+begin_src sh :exports results :results silent echo eric #+end_src call line #+call: conflict-block() :exports results #+results: conflict-block() : eric ** macros during tangling :PROPERTIES: :ID: d2ff9d6f-b413-4072-91a9-3ae8aa32032c :END: First, add macro expansion to the new `org-babel-tangle-body-hook'. #+begin_src emacs-lisp :results silent (add-hook 'org-babel-tangle-body-hook (lambda () (org-export-preprocess-apply-macros))) #+end_src Then define the macro. Note: you may need to export the buffer before tangling so that the macro definition is noticed and processed by Org-mode. #+MACRO: CONFIG_PARAM01 45 Then on both export and tangling the macro in the following code block will be replaced. #+begin_src sh :tangle yes echo org-mode set CONFIG_PARAMETER to: {{{CONFIG_PARAM01}}} #+end_src ** looks like a pipe in a table #+source: clean #+begin_src emacs-lisp :var in="" (flet ((clean (in) (if (listp in) (mapcar #'clean in) (if (stringp in) (replace-regexp-in-string "¦" "|" in) in)))) (clean in)) #+end_src #+results: regexps | first | (a¦b) | | second | (1¦2) | #+begin_src perl :var a=clean(in=regexps)[0,1] :var b=clean(in=regexps)[1,1] $a; $b; #+end_src #+results: : (1|2) ** eval results as a list #+begin_src python :results value return "(mapcar (lambda (el) (+ 1 el)) '(1 2))" #+end_src #+results: | 2 | 3 | #+begin_src python :results value return "[1, 2]" #+end_src #+results: | 1 | 2 | #+begin_src python :results value return [1, 2] #+end_src #+results: | 1 | 2 | #+begin_src python :results value return "%r" % "[1 2]" #+end_src #+results: : [1 2] ** export of inline R code Here I test inline code evaluation in R. #+begin_src R :session *R* x <- 100 #+end_src #+results: : 100 Now I want to export the value of x, which should be src_R[:session *R*]{x} . Did the number 100 show up at the end of the previous sentence on export? ** simple mysql #+begin_src sql :engine mysql show tables; #+end_src ** leading/trailing spaces #+results: spaces-wrapped-string - " pass through with space " #+begin_src emacs-lisp :var res=spaces-wrapped-string[0] res #+end_src #+results: : pass through with space ** results org raw wrap #+begin_src sh :results output org :exports none cat <> #+end_src #+results[e2c9e6c2f84563b590a765502057d92463e50182]: test_sleep | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | | 10 | ** source block names in current buffer #+begin_src emacs-lisp :results list (org-babel-src-block-names) #+end_src ** simple python block #+begin_src python :return foo foo = 8 foo += 1 #+end_src #+results: : 9 ** sh return a list of elements with spaces #+begin_src sh :results list echo "eric schulte" echo "dan davison" echo "seb vauban" #+end_src #+results: - ("eric" "schulte") - ("dan" "davison") - ("seb" "vauban") #+begin_src sh :results scalar echo "eric schulte" echo "dan davison" echo "seb vauban" #+end_src #+results: : eric schulte : dan davison : seb vauban ** calc variables inside of parenthesis #+BEGIN_SRC calc :var testvar=9000 testvar - 200 #+END_SRC #+results: : 8800 #+BEGIN_SRC calc :var testvar=9000 (testvar - 200) 800 #+END_SRC #+results: : 7040000 ** new lists *** results embedded inside of a list 1. this has results #+results: something-in-a-list : foo 2. and this doesn't work #+begin_src emacs-lisp :var data=something-in-a-list data #+end_src #+results: : foo *** reading and writing #+results: simple-list - 1 - two - 3 - four #+source: simple-list #+begin_src emacs-lisp :var lst=simple-list :results list (reverse lst) #+end_src ** catch the file name during export #+begin_src emacs-lisp :var file-name=(buffer-file-name) :exports both file-name #+end_src ** export of inline code blocks which are silent #+begin_src emacs-lisp :results silent (setf org-babel-default-inline-header-args '((:session . "none") (:results . (if (boundp 'org-current-export-file) "replace" "silent")) (:exports . "results"))) #+end_src Here is an inline code block src_sh{echo 8} <- there ** mentions of file names in file contents directory to search #+results: graph-dir : graph-dir list all files in dir #+source: graph-files #+begin_src sh :results vector :var dir=graph-dir find $dir -type f -exec basename {} \; #+end_src #+results: graph-files | other | | dan | | eric | | seb | association of files with mentions #+source: graph-associations #+begin_src sh :var dir=graph-dir :var files=graph-files for i in $files; do for j in `grep -l -r $i $dir`;do echo $i, `basename $j` done done #+end_src #+results: graph-associations | other | eric | | other | seb | | dan | eric | | eric | seb | | seb | dan | graphing with dot #+source: to-dot #+begin_src sh :var associations=graph-associations :results scalar echo "$associations"|awk '{print $1, "->", $2}' #+end_src #+results: to-dot : other -> eric : other -> seb : dan -> eric : eric -> seb : seb -> dan #+begin_src dot :var data=to-dot :file files.png digraph G{ $data } #+end_src #+results: [[file:files.png]] ** inline code block here is an inline block src_R{1+1} ** recutils #+begin_src sh :file book.rec cat < book.rec # -*- mode: rec -*- %rec: Book %mandatory: Title %type: Location enum loaned home unknown %doc: + A book in my personal collection. Title: GNU Emacs Manual Author: Richard M. Stallman Publisher: FSF Location: home Title: The Colour of Magic Author: Terry Pratchett Location: loaned Title: Mio Cid Author: Anonymous Location: home Title: chapters.gnu.org administration guide Author: Nacho Gonzalez Author: Jose E. Marchesi Location: unknown Title: Yeelong User Manual Location: home # End of books.rec EOF #+end_src #+results: [[file:book.rec]] #+begin_src rec :data book.rec :fields Title,Author Location = 'loaned' #+end_src #+results: | Title | Author | | The Colour of Magic | Terry Pratchett | #+begin_src rec :data book.rec :fields Title,Author #+end_src #+results: | Title | Author | Author_2 | | GNU Emacs Manual | Richard M. Stallman | | | The Colour of Magic | Terry Pratchett | | | Mio Cid | Anonymous | | | chapters.gnu.org administration guide | Nacho Gonzalez | Jose E. Marchesi | | Yeelong User Manual | | | ** SQL --- example reading org-mode table into sql #+tblname: example-table-for-sql | a | b | |---+----| | 1 | 10 | | 2 | 11 | | 3 | 12 | | 4 | 13 | | 5 | 14 | | 6 | 15 | #+headers: :var table=example-table-for-sql #+begin_src sql :engine mysql load data infile "$table" into mytable; #+end_src ** passing keywords inside header arguments #+begin_src emacs-lisp :var lst='(:no-expand :other) lst #+end_src #+results: | :no-expand | other | ** two vars in a properties block -- not possible :PROPERTIES: :var: test1=7 :var: test2=8 :END: #+begin_src emacs-lisp (message "test1=%S test2=%S" test1 test2) #+end_src results in Error : let: Symbol's value as variable is void: test2 *** an alternative :PROPERTIES: :var: tests=all-tests :END: #+tblname: all-tests - 7 - 8 #+begin_src emacs-lisp :var eric=89 (message "test1=%S test2=%S" (first tests) (second tests)) #+end_src #+results: : test1=7 test2=8 *** another alternative :PROPERTIES: :var: vars=variables :END: #+tblname: variables | var1 | 1 | | var2 | 2 | #+begin_src python print vars[0][1] print vars[1][1] #+end_src ** how to set no-expand in properties :PROPERTIES: :no-expand: yes :END: #+begin_src emacs-lisp :var something="other thing" :tangle no-expand.el :test #+end_src tangles to ** non-inlined inline code block The Date is src_sh[:results replace]{date} at the time of =this= export. src_sh[:results replace]{ls} ** results replace not always working #+begin_src sh :results output org replace :exports code for i in `seq 4`;do echo "- place $i in the list" done #+end_src #+results: #+BEGIN_ORG - place 1 in the list - place 2 in the list - place 3 in the list - place 4 in the list #+END_ORG inline block src_emacs-lisp[:exports code :results replace]{(+ 1 1 1)} here is was ** simple calc example #+begin_src calc 2*3 #+end_src #+results: : 6 ** inserting wrappers eats following characters *** Test #+begin_src emacs-lisp :results latex "\\begin{equation}\\frac{1}{2}\n\\end{equation}" #+end_src #+results: #+BEGIN_LaTeX \begin{equation}\frac{1}{2} \end{equation} #+END_LaTeX *** Watch me die :-( ** creating a directory when needed for tangling #+begin_src clojure :tangle (prog1 "src/foo.clj" (make-directory "src" ".")) (ns something) #+end_src a helper function for the above #+begin_src emacs-lisp (defun mkdir-p (file &optional dir) "Create any parent directories of FILE if missing and return FILE." (make-directory (file-name-directory file) (or dir ".")) file) #+end_src allows the following #+begin_src clojure :tangle (mkdir-p "src/foo.clj") (ns something) #+end_src There is now a new header argument controlling this behavior #+begin_src emacs-lisp :mkdirp yes :tangle novel/nested/directories/finally.clj (message "contents") #+end_src ** passing arguments to the shell #+results: something : eric : schulte : yes : more #+results: something-list | 1 | | 2 | | 3 | #+begin_src sh :var data=something-list echo "$data"|wc -l #+end_src #+results: : 3 #+begin_src emacs-lisp :results silent (setq org-babel-sh-var-quote-fmt "`cat <<'BABEL_TABLE'\n%s\nBABEL_TABLE\n`") #+end_src ** wrap noweb references in comments #+source: wrappable #+begin_src emacs-lisp (setq x (+ 4 x)) #+end_src #+begin_src emacs-lisp :comments noweb :noweb yes :tangle yes (let ((x 1)) (message "x=%s" x) <> (message "x=%s" x)) #+end_src ** replace inline code block This is src_emacs-lisp{(+ 1 2 3)} an inline block. #+begin_src emacs-lisp (defun replace-inline-block () (interactive) (if (save-excursion (re-search-backward "[ \f\t\n\r\v]" nil t) (looking-at org-babel-inline-src-block-regexp)) (replace-match ((lambda (el) (if (stringp el) el (format "%S" el))) (org-babel-execute-src-block)) nil nil nil 1) (error "not inside of an inline source block."))) #+end_src ** noweb then variables #+source: replaced-first #+begin_src latex \begin{itemize} \item first \item data \item third \end{itemize} #+end_src #+begin_src latex :var data="second" :noweb yes \section{ordinals} \label{sec:ordinals} <> #+end_src ** empty strings as arguments #+begin_src emacs-lisp :results output :var foo="" (concat foo "bar") #+end_src #+results: : bar ** call lines #+source: doubler #+begin_src emacs-lisp :var n=2 (* n 2) #+end_src #+call: doubler(n=3) #+results: doubler(n=3) : 6 #+call: doubler[:var n=3]() #+results: doubler[:var n=3]() : 6 ** language name abbreviations #+begin_src emacs-lisp (add-to-list 'org-src-lang-modes '("clj" . clojure)) #+end_src #+begin_src clj (map (partial + 1) (range 20)) #+end_src ** eval query #+begin_src emacs-lisp (setq org-confirm-babel-evaluate (lambda (lang body) (not (equal "ditaa" lang)))) #+end_src #+results: | lambda | (lang body) | (not (equal ditaa lang)) | #+begin_src emacs-lisp :eval query (message "eval'd") #+end_src #+results: : eval'd #+begin_src ditaa --- #+end_src ** new file handling #+begin_src sh :sep , :file dirlisting ls -l #+end_src #+results: [[file:dirlisting]] #+begin_src ruby :file ruby-out [[1, 2, 3, 4], [2, 4, 6, 8]] #+end_src #+results: [[file:ruby-out]] #+begin_src emacs-lisp :results file :results append (let ((today (replace-regexp-in-string "[ \t]" "-" (current-time-string)))) (with-temp-file today (insert (message "I'm feeling %s" (nth (random 3) (list "good" "bad" "just fine"))))) today) #+end_src #+results: [[file:Mon-Dec-20-17:27:52-2010]] from http://www.graphviz.org/Gallery/directed/fsm.gv.txt #+begin_src dot :file fsa.png digraph finite_state_machine { rankdir=LR; size="8,5" node [shape = doublecircle]; LR_0 LR_3 LR_4 LR_8; node [shape = circle]; LR_0 -> LR_2 [ label = "SS(B)" ]; LR_0 -> LR_1 [ label = "SS(S)" ]; LR_1 -> LR_3 [ label = "S($end)" ]; LR_2 -> LR_6 [ label = "SS(b)" ]; LR_2 -> LR_5 [ label = "SS(a)" ]; LR_2 -> LR_4 [ label = "S(A)" ]; LR_5 -> LR_7 [ label = "S(b)" ]; LR_5 -> LR_5 [ label = "S(a)" ]; LR_6 -> LR_6 [ label = "S(b)" ]; LR_6 -> LR_5 [ label = "S(a)" ]; LR_7 -> LR_8 [ label = "S(b)" ]; LR_7 -> LR_5 [ label = "S(a)" ]; LR_8 -> LR_6 [ label = "S(b)" ]; LR_8 -> LR_5 [ label = "S(a)" ]; } #+end_src #+results: [[file:fsa.png]] ** tangle templates #+source: template-heading #+begin_src emacs-lisp some stuff here #+end_src #+source: template-footing #+begin_src emacs-lisp some other stuff here #+end_src #+source: template #+begin_src sh :results output :noweb yes :var body="body stuff" heading=$(cat<> EOF ) footing=$(cat<> EOF ) echo $heading echo "$body" echo $footing #+end_src #+call: template[:noweb yes](body="something new") #+results: template[:noweb yes](body="something new") : some stuff here : something new : some other stuff here ** missing lines on tangle :PROPERTIES: :ID: 83eb62fd-4147-405b-bdc2-567b2d5cbd70 :END: #+begin_src org :results latex :tangle latex-err.tex ,one ,two ,three #+end_src #+begin_src org :results latex :results replace ,- eric ,- schulte #+end_src #+results: #+BEGIN_LaTeX \begin{itemize} \item eric \item schulte \end{itemize} #+END_LaTeX ** utf8 and latin-1 encodings #+tblname: toto | é | #+begin_src python :var t=toto :preamble # -*- coding: latin1 -*- :return [len(babel), len(local)] babel = unicode (t[0][0],"latin1") local = unicode ("é","latin1") #+end_src #+results: | 2 | 2 | ** Python requires a utf-8 coding prefix #+begin_src python :prefix # -*- coding: utf-8 -*- :return s s = "é" #+end_src #+results: : é #+begin_src python :prefix # -*- coding: utf-8 -*- :results output s = "é" print(s) #+end_src #+results: : é ** empty lines in R session output #+begin_src R :results output :session x <- 1; x x + 1 x + 4 #+end_src R #+results: : [1] 1 : [1] 2 : [1] 5 ** =:eval query= shows the name #+source: i-have-a-name #+begin_src sh :eval query date #+end_src #+results: i-have-a-name : Tue Nov 30 22:03:25 MST 2010 ** sql variables #+results: sql-param | table | valueTable0 | | column | valueColumn0 | | type | valueType0 | | nullability | valueNullability0 | I want to apply the values onto the following chunk of code: #+srcname: add-column-in-table-0 #+begin_src sql :var table=sql-param[0,1] :var column=sql-param[1,1] :var type=sql-param[2,1] :var nullability=sql-param[3,1] -- add column `@column' (if column does not exist yet) IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '@table' AND COLUMN_NAME = '@column') BEGIN ALTER TABLE $table ADD $column $type @nullability END #+end_src ** python with return header argument #+begin_src python :return y x = 8 y = 98 2 #+end_src #+results: : 98 ** safe lists for Haskell #+tblname: mixed-table | 1 | first | | 2 | second | | 3 | third | | 4 | fourth | #+source: rec-string-wrap #+begin_src emacs-lisp :var data=mixed-table (defun rec-string-wrap (in) (if (listp in) (mapcar #'rec-string-wrap in) (format "%S" in))) (rec-string-wrap data) #+end_src #+begin_src haskell :var tbl=rec-string-wrap(data=mixed-table) map head tbl #+end_src #+results: | 1 | 2 | 3 | 4 | ** add column to table with awk :PROPERTIES: :question_author: Sébastien Vauban :END: I want to *add a column* to the following table. #+results: table-message | This is line 1 of the message. | | This is line 2 of the message. | | This is the last line of the message. | Its value should be dependant on a *regexp matching* the *current row* (for example, if 1 is detected in the original column, then write "A" in the new one, "B" if 2 is read, "C" if 3 is read, etc.). Hence, I'm thinking using AWK as an easy solution. #+begin_src note I'm open to other ideas on how I could do this as easily. Just throw me ideas, if you have some. #+end_src the easiest (for me) would be with the elisp =mapcar= function #+begin_src emacs-lisp :var tbl=table-message (mapcar (lambda (row) (cons "New col" row)) tbl) #+end_src #+results: | New col | This is line 1 of the message. | | New col | This is line 2 of the message. | | New col | This is the last line of the message. | *First* trial: add a column whose cell contents will be *fixed* (here, equal to =New col=). #+srcname: add-col #+begin_src sh :var data=table-message :results output raw :exports both echo "$data" | awk '// {print "| New col | " $0 " |";}' #+end_src #+results: add-col | New col | This is line 1 of the message. | | New col | This is line 2 of the message. | | New col | This is the last line of the message. | ** reading from single-quote-delim languages #+BEGIN_SRC python return [['607', 'Show license short, name on the deed'], ['255', "'(message (concat 'hello ' 'world))"]] #+END_SRC #+results: | 607 | Show license short, name on the deed | | 255 | '(message (concat 'hello ' 'world)) | #+begin_src ruby [['607', 'Show license, short name on the deed'], ['255', "))'(message (concat 'hello ' 'world"]] #+end_src #+results: | 607 | Show license, short name on the deed | | 255 | ))'(message (concat 'hello ' 'world | #+begin_src haskell [["'single quotes'", "b"], ["\"double quotes\"", "d"]] #+end_src #+results: | 'single quotes' | b | | "double quotes" | d | ** un-named R code blocks #+begin_src R 8 #+end_src #+results: : 8 #+begin_src emacs-lisp :eric 8 #+end_src #+results: : 8 #+BEGIN_SRC R :session :results output xyz #+END_SRC #+BEGIN_SRC R :session *R-2* :results output | xyz 9 #+END_SRC #+results: : [1] 9 ** introducing =wrap= header argument #+begin_src emacs-lisp :results wrap :exports both (mapcar (lambda (el) (list el (+ 1 (* el el)))) (number-sequence 0 10)) #+end_src #+results: #+BEGIN_RESULT | 0 | 1 | | 1 | 2 | | 2 | 5 | | 3 | 10 | | 4 | 17 | | 5 | 26 | | 6 | 37 | | 7 | 50 | | 8 | 65 | | 9 | 82 | | 10 | 101 | #+END_RESULT now indented - first - second #+begin_src emacs-lisp :results wrap :exports both "something else" #+end_src #+results: #+BEGIN_RESULT : something else #+END_RESULT ** lists as data types #+results: a-list - org-mode - and - babel #+source: a-list #+begin_src emacs-lisp :var lst=a-list :results list (reverse lst) #+end_src also for a block inside of a list 1. First element 2. Second element -- has a block #+begin_src emacs-lisp (+ 1 1 1 1) #+end_src #+results: : 4 3. third element ** sqlite #+begin_src sqlite :db paper/climate.sqlite select count(*) from temps; #+end_src #+results: : 422689 ** lob calls with header argument pass through #+source: lob-header #+begin_src emacs-lisp :var n=20 n #+end_src #+call: lob-header[:results vector](n=15) #+results: lob-header[:results vector](n=15) | 15 | #+call: lob-header(n=10) :results vector #+results: lob-header(n=10) | 10 | need ob-ref.el to pass through the header arguments in "[]"s #+begin_src emacs-lisp :var n=lob-header[:results vector](n=8) n #+end_src #+results: | 8 | #+begin_src emacs-lisp :var n=lob-header[:results vector](n=8)[0,0] n #+end_src #+results: : 8 ** clojure code blocks and the lazies #+begin_src emacs-lisp :results silent (defun org-babel-execute:clojure (body params) (with-temp-buffer (insert body) (read (slime-eval `(swank:interactive-eval-region ,(buffer-substring-no-properties (point-min) (point-max))))))) #+end_src #+begin_src clojure (map (fn [el] (list el (* el el)))(range 10)) #+end_src #+results: | 0 | 0 | | 1 | 1 | | 2 | 4 | | 3 | 9 | | 4 | 16 | | 5 | 25 | | 6 | 36 | | 7 | 49 | | 8 | 64 | | 9 | 81 | ** playing with calc support #+begin_src emacs-lisp (require 'ob-calc) #+end_src #+begin_src calc :var some=8 some some '* 8+8 '+ #+end_src #+results: : 80 #+begin_src calc 2*(8+8) #+end_src #+results: : 32 #+begin_src calc 2*e #+end_src #+results: : 5.43656365692 #+begin_src calc :var something=9 2*something #+end_src #+results: : 18 ** shell blocks returning a file name #+begin_src sh :file quick.txt :results output date #+end_src #+results: [[file:quick.txt]] ** passing arguments through call lines #+source: test #+begin_src R :session :file test.pdf :var myarg="bla" plot(1:10, main=myarg) #+end_src #+results: test [[file:test.pdf]] #+call: test(myarg="hiho") #+results: test(myarg="hiho") : test.pdf ** simple gnuplot tests #+results: some-more-gnuplot | 1 | 1 | | 2 | 4 | | 3 | 9 | | 4 | 16 | | 5 | 25 | | 6 | 36 | | 7 | 49 | | 8 | 64 | #+TBLFM: $2=$1*$1 #+begin_src gnuplot :var data=some-more-gnuplot plot "$data" #+end_src #+results: Plotting data points from a table could look like this: #+tblname: basic-plot | x | y1 | y2 | |-----+------------+------------| | 0.1 | 0.425 | 0.375 | | 0.2 | 0.3125 | 0.3375 | | 0.3 | 0.24999993 | 0.28333338 | | 0.4 | 0.275 | 0.28125 | | 0.5 | 0.26 | 0.27 | | 0.6 | 0.25833338 | 0.24999993 | | 0.7 | 0.24642845 | 0.23928553 | | 0.8 | 0.23125 | 0.2375 | | 0.9 | 0.23333323 | 0.2333332 | | 1 | 0.2225 | 0.22 | #+begin_src gnuplot :var data=basic-plot :exports code :file basic-plot.png set title "Putting it All Together" set xlabel "X" set xrange [0:1] set xtics 0,0.1,1 set ylabel "Y" set yrange [0.2:0.5] set ytics 0.2,0.05,0.5 plot data u 1:2 w p lw 2 title 'x vs. y1', \ data u 1:3 w lp lw 1 title 'x vx. y2' #+end_src #+results: [[file:basic-plot.png]] ** latex headers in latex code blocks #+begin_src latex :headers \usepackage{lmodern} :file name1.pdf Eric Schulte #+end_src #+results: [[file:name1.pdf]] #+begin_src latex :headers '("\\usepackage{mathpazo}" "\\usepackage{fullpage}") :file name2.pdf Eric Schulte #+end_src #+results: [[file:name2.pdf]] ** export-specific header arguments #+headers: :var out=(if (and (boundp 'latexp) latexp) "latex" "not latex") #+begin_src emacs-lisp out #+end_src #+results: : not latex ** security problem with elisp in header arguments #+begin_src emacs-lisp :var data=(setq org-confirm-babel-evaluate nil) :results silent (+ 1 1) #+end_src ** preceding blank lines on tangle #+begin_src emacs-lisp :results silent (setq org-babel-tangle-pad-newline nil) #+end_src #+begin_src sh :tangle something.reg # something echo "else" #+end_src ** very very large numbers #+tblname: numbers | 1 | | 2 | | 12 | | 45 | | 166 | | 12567890 | | 231231282371983279389999999 | #+begin_src emacs-lisp :var numbers=numbers (mapcar (lambda (line) (let ((number (car line))) (list number (type-of number)))) numbers) #+end_src #+results: | 1 | integer | | 2 | integer | | 12 | integer | | 45 | integer | | 166 | integer | | 12567890 | integer | | 2.3123128237198328e+26 | float | ** weaving with noweb links :PROPERTIES: :tangle: yes :END: #+source: name #+begin_src emacs-lisp (message "eric") #+end_src #+begin_src emacs-lisp :noweb tangle ;; name <> #+end_src ** index into a scalar #+tblname: short-list | a | | b | #+begin_src emacs-lisp :var scalar=short-list[0,0] scalar #+end_src #+results: : a ** cycle -- the input is the output and the rhythm is the base and the base is the treble #+results: cycle | one | | two | | three | #+source: cycle #+begin_src emacs-lisp :var table=cycle (append (last table) (butlast table)) #+end_src #+begin_src emacs-lisp :exports results (+ 1 1 1) #+end_src ** Letter #+source: body #+begin_src org :results latex ,My body includes a list: ,- one ,- two ,and a small table: ,| first | second | ,| other | last | ,Not more. #+end_src #+begin_src latex :noweb yes :tangle yes \documentclass[11pt]{isodoc} \usepackage[utf8x]{inputenc} \usepackage[T1]{fontenc} \setupdocument{ to = {Eric}, subject = {Tough to understand what to do...}, opening = {Hi}, closing = {Best} } \begin{document} \letter{% <> } \end{document} #+end_src ** splitting code blocks - with indentation #+begin_src emacs-lisp ;;;###autoload (defun org-babel-previous-src-block (&optional arg) "Jump to the previous source block. With optional prefix argument ARG, jump backward ARG many source blocks." (interactive "P") (condition-case nil (re-search-backward org-babel-src-block-regexp nil nil (or arg 1)) (error (error "No previous code blocks"))) (goto-char (match-beginning 0)) (org-show-context)) #+end_src #+begin_src emacs-lisp ;;;###autoload (defun org-babel-split-block-maybe (&optional arg) "Split the current source code block on the cursor." (interactive "p") ((lambda (info) (if info (let ((lang (nth 0 info)) (indent (nth 6 info)) (stars (make-string (org-current-level) ?*))) (insert (concat (if (looking-at "^") "" "\n") (make-string indent ? ) "#+end_src\n" (if arg stars (make-string indent ? )) "\n" (make-string indent ? ) "#+begin_src " lang (if (looking-at "[\n\r]") "" "\n ")))) (message "Not in src block."))) (org-babel-get-src-block-info))) ;; other stuff #+end_src ** header arguments on call line #+source: eight #+begin_src R 8 #+end_src #+call: eight() :results vector #+results: eight() | 8 | maybe the following with the new proposed header arguments #+call: eight[:session *R*]() :results vector ** empty-string results test me one two 3 #+begin_src emacs-lisp #+end_src More test #+begin_src emacs-lisp (mapcar (lambda (pair) (list (car pair) (cdr pair))) params) #+end_src #+results: | :cache | no | | :colnames | no | | :comments | | | :exports | code | | :hlines | yes | | :noweb | no | | :results | replace | | :session | none | | :shebang | | | :tangle | no | ** tangle org-mode block #+source: org-list #+begin_src org :results latex - one - two - three #+end_src #+begin_src emacs-lisp :tangle example.tangled :noweb yes " <> " #+end_src ** remove results when nil is returned #+begin_src emacs-lisp (progn (+ 1 1) nil) #+end_src #+results: ** comparative speed of python evaluation #+begin_src python :session test 2+2 #+end_src #+results: : 4 #+begin_src python return 2+2 #+end_src #+results: : 4 #+begin_src python :session test def add(a,b): return a+b def sub(a,b): return a-b add(sub(10,1),sub(10,2)) #+end_src #+results: : org_babel_python_eoe ** customizable comment formats :PROPERTIES: :tangle: yes :comments: yes :END: #+begin_src emacs-lisp :results silent (setq org-babel-tangle-comment-format-beg "{-# LINE %start-line \"%file\" #-}" org-babel-tangle-comment-format-end "" org-babel-tangle-pad-newline) #+end_src #+begin_src haskell :tangle Main.hs test = length main = print $ test [1,2,3] #+end_src I would like the following output in the tangled file Main.hs: : {-# LINE 4 "Haskell.org" #-} : test = length : main = print $ test [1,2,3] ** tangling with full comments :PROPERTIES: :comments: org :tangle: full-comments.el :END: The top block #+begin_src emacs-lisp (message "first block") #+end_src here's some text which won't be tangled *** subheading another block | 1 | first | | 2 | second | #+begin_src emacs-lisp (message "second") #+end_src and finally a block with a =:noweb= header argument #+begin_src emacs-lisp :noweb yes (progn <>) #+end_src ** quoting header args (e.g. :cmdline) #+begin_src C :cmdline 1 2 3 4 5 :includes int main(int argc, char **argv){ printf("argv[1] %s\n", argv[1]); return 0; } #+end_src #+results: : argv[1] 1 ** :var (buffer-file-name) during export (buffer-file-name) will return nil because the temporary export buffer is not visiting any file. /file=(vc-working-revision (buffer-file-name))/ #+begin_src sh :var file=(vc-working-revision (or (buffer-file-name) "")) :exports results echo $file Revision #+end_src /file=(vc-working-revision (or (buffer-file-name) org-current-export-file))/ #+begin_src sh :var file=(vc-working-revision (or (buffer-file-name) org-current-export-file)) :exports results echo $file Revision #+end_src ** :session evaluation on export This first block is evaluated but /doesn't/ appear in export. /:session *R* :exports none/ #+begin_src R :session *R* :exports none x <- 8 #+end_src This second block /does/ appear in export. #+begin_src R :session *R* :exports results x #+end_src ** ditaa with tilda in path #+begin_src ditaa :file example.png +--------------+ | | | | | | | | | | +--------------+ #+end_src ** conditional tangling #+begin_src emacs-lisp :results silent (setq tangle-tag "right") #+end_src *** first subheading :left: #+begin_src R :tangle (and (equal (car (org-get-tags-at (point))) tangle-tag) "yes") "first" #+end_src *** second subheading :right: #+begin_src R :tangle (and (equal (car (org-get-tags-at (point))) tangle-tag) "yes") "second" #+end_src ** scheme sessions #+begin_src scheme :var number=9 :session *scheme* :scheme guile (+ number 0) #+end_src #+results: : 9 #+begin_src scheme :var number=9 :session *scheme* :scheme racket (+ number 1) #+end_src #+results: : 10 ** pulling information from tags :blue: #+begin_src R :var color=(car (org-get-tags-at (point))) :tangle example.R color #+end_src #+results: : blue ** initial scheme support #+source: numbers #+begin_src scheme (map (lambda (el) (+ el 1)) '(1 2 3)) #+end_src #+results: | 2 | 3 | 4 | #+begin_src scheme :var numbers=numbers (map (lambda (el) (- el 1)) numbers) #+end_src #+results: | 1 | 2 | 3 | ** initial javascript support using node.js #+begin_src js var n = 0; n = n+1; return n #+end_src #+results: : 1 #+source: cars #+begin_src js var cars = ["Saab","Volvo","BMW"]; return cars; #+end_src #+results: cars | Saab | Volvo | BMW | #+begin_src js :var cars=cars return cars[0][0]; #+end_src #+results: : Saab #+begin_src js :var cars=cars return cars[0].length; #+end_src #+results: : 3 ** duplicate results on execute subtree #+begin_src emacs-lisp :results org :exports results "- first - second - third " #+end_src #+results: #+BEGIN_SRC org - first - second - third #+END_SRC ** eval for side effect on export - one plus one #+source: one-plus-one #+begin_src emacs-lisp :exports none :results silent (+ 1 1) #+end_src - plus one is #+begin_src emacs-lisp :var two=one-plus-one :exports both (+ 1 two) #+end_src ** trying out plantuml setup #+begin_src emacs-lisp :results silent (require 'ob-plantuml) (setq org-plantuml-jar-path "~/src/org/contrib/scripts/plantuml.jar") #+end_src usage -- sequence diagram #+begin_src plantuml :file tryout.png Alice -> Bob: synchronous call Alice ->> Bob: asynchronous call #+end_src #+results: [[file:tryout.png]] ** wrapping up raw/org results #+begin_src emacs-lisp :results org :exports results "- first - second - third " #+end_src #+results: #+BEGIN_SRC org - first - second - third #+END_SRC ** not caching :PROPERTIES: :session: *R* :results: output :exports: both :cache: yes :END: #+begin_src R :noeval cat("random result:", runif(1), "\n") Sys.sleep(2) alarm() #+end_src #+begin_src R :noeval cat("random result:", runif(1), "\n") Sys.sleep(2) alarm() #+end_src *** cache on export do we export cached blocks #+begin_src emacs-lisp :cache yes :exports results (random) #+end_src #+results[46632b4fe2e3a23e847953c95adcba58c270b381]: : 490528137 *** looks like this is a problem with info collection #+begin_src emacs-lisp (format "%S" info) #+end_src #+begin_src emacs-lisp :results scalar :exports results :tangle yes :comments yes (mapcar (lambda (el) (list (car el) (cdr el))) (nth 2 info)) #+end_src #+results[4184710f118ac768ea0d90632508792d695efd7a]: | :cache | yes | | :colnames | no | | :comments | yes | | :exports | results | | :hlines | yes | | :noweb | no | | :results | output replace scalar | | :session | *R* | | :shebang | | | :tangle | yes | #+begin_src emacs-lisp :exports results (message "calculating info") (org-babel-sha1-hash info) #+end_src #+results[0427db66afdc95462d1c8514b662829987d71ff5]: : 0427db66afdc95462d1c8514b662829987d71ff5 ** eval and noeval date, should export both, but won't output results because of presence of the =:noeval= header argument. #+begin_src sh :noeval :exports both date #+end_src should export code, so no need to do anything #+begin_src sh date #+end_src should export nothing, and should not query #+source: this-is-ls #+begin_src sh :eval query :exports code date #+end_src should export results, and should trigger query above #+begin_src emacs-lisp :var ls=this-is-ls :exports results ls #+end_src ** issues with shell evaluation #+begin_src sh :results silent cd ~/src/org/ make #+end_src ** org results and replace #+begin_src emacs-lisp :results org "| 1 | 2 | | 2 | 3 |" #+end_src #+results: | 1 | 2 | | 2 | 3 | #+begin_src R rnorm(1) #+end_src #+begin_src R numbers <- matrix(c(51,43,22,92,28,21,68,22,9),ncol=3,byrow=TRUE) numbers #+end_src #+results: | 51 | 43 | 22 | | 92 | 28 | 21 | | 68 | 22 | 9 | #+begin_src R :colnames yes numbers <- matrix(c(51,43,22,92,28,21,68,22,9),ncol=3,byrow=TRUE) numbers #+end_src #+results: | V1 | V2 | V3 | |----+----+----| | 51 | 43 | 22 | | 92 | 28 | 21 | | 68 | 22 | 9 | ** ledger example output text #+results: ledger-stuff #+begin_example 09-Aug-21 CHEQUE : 9953055 Expenses:Unknown 166.70 EUR 166.70 EUR 09-Sep-17 CHEQUE : 7691785 Expenses:Unknown 100.00 EUR 266.70 EUR 09-Oct-16 REMISE CHEQUE N 8686318 001 105 Expenses:Unknown -525.00 EUR -258.30 EUR #+end_example #+begin_src sh :var stuff=ledger-stuff echo "$stuff" #+end_src ** importing the output of ledger #+results: ledger-output #+begin_example 09-Aug-21 CHEQUE : 9953055 Expenses:Unknown 166.70 EUR 166.70 EUR 09-Sep-17 CHEQUE : 7691785 Expenses:Unknown 100.00 EUR 266.70 EUR 09-Oct-16 REMISE CHEQUE N 8686318 001 105 Expenses:Unknown -525.00 EUR -258.30 EUR #+end_example #+begin_src emacs-lisp :var ledger=ledger-output (with-temp-buffer (insert ledger) (message ledger) (org-table-convert-region (point-min) (point-max) 2) (org-table-to-lisp)) #+end_src #+results: | 09-Aug-21 CHEQUE : 9953055 | Expenses:Unknown | 166.70 EUR | 166.70 EUR | | 09-Sep-17 CHEQUE : 7691785 | Expenses:Unknown | 100.00 EUR | 266.70 EUR | | 09-Oct-16 REMISE CHEQUE N 8686318 001 105 | Expenses:Unknown | -525.00 EUR | -258.30 EUR | ** lob -- writing results out to files #+source: table #+begin_src emacs-lisp (mapcar (lambda (el) (number-sequence el (+ el 3))) (number-sequence 0 4)) #+end_src writes the results out as csv file #+call: write(data=table, file="~/Desktop/example.csv") :results silent writes the results out as tab separated file #+call: write(data=table, file="~/Desktop/example.tsv") :results silent write the results out as a normal org-mode file #+call: write(data=table, file="~/Desktop/example.org") :results silent ** lisp #+begin_src lisp :var n=5 (mapcar (lambda (el) (* el el)) (append '(1 7 3 4) (list n))) #+end_src #+results: | 1 | 49 | 9 | 16 | 25 | #+results: short-list | 1 | | 2 | | 3 | #+begin_src lisp :var lst=short-list :session t (+ 1 (length lst)) #+end_src #+results: : 4 ** comments in R blocks #+begin_src R :session *R* :results output # this is a comment x <- rnorm(1) # this is another comment x #+end_src #+results: : : [1] 1.320853 ** tangle R and load :PROPERTIES: :tangle: to-load.r :END: evaluate this #+begin_src emacs-lisp :results silent :tangle no (setq org-babel-post-tangle-hook nil) (add-hook 'org-babel-post-tangle-hook (lambda () (ess-load-file (buffer-file-name)))) #+end_src then tangle #+begin_src R :comments yes x <- 10 #+end_src #+begin_src R y <- 9 #+end_src #+begin_src R :tangle file2.R y <- 9 #+end_src ** colnames to specific variables #+tblname: spec-colnames | one | two | thee | |-----+-----+------| | 1 | 2 | 3 | #+tblname: nospec-colnames | three | two | one | |-------+-----+-----| | 3 | 2 | 1 | #+begin_src python :var nospec=nospec-colnames :var spec=spec-colnames :colnames '(spec) return nospec #+end_src #+results: | one | two | thee | |-------+-----+------| | three | two | one | | 3 | 2 | 1 | ** caption on code block #+caption: Examples of variable declaration. #+label: sql-block #+begin_src sql SELECT 6*9; #+end_src ** palendromic primes Note that because Haskell is funny about what can be typed into the interpreter, the following should be loaded with =org-babel-load-in-session=. #+begin_src haskell palendromic_primes = [x | x <- [1..], prime x, palendrome x] where factors n = [x | x <- [1..floor(sqrt(fromIntegral(n)))], n `mod` x == 0] prime n = factors n == [1] primes = [x | x <- [2..], prime x] palendrome n = show(n) == reverse(show(n)) palendromic_prime_distances = map (\(x,y)-> y-x) neighbors where neighbors = (zip palendromic_primes (tail palendromic_primes)) #+end_src #+source: palendromic_prime_distances #+begin_src haskell take 180 (zip [1..] palendromic_prime_distances) #+end_src For high-quality png output from gnuplot, the following sequence of graphing to a =.eps= file, and then converting to a =.png= can be useful. #+source: dist-graph #+begin_src gnuplot :var data=palendromic_prime_distances :file pps.eps set term postscript landscape color enhanced set log y set title "distance between consecutive palendromic primes" plot "$data" with fs notitle #+end_src The =convert= command is part of the [[http://www.imagemagick.org/script/index.php][imagemagick]] suite. #+begin_src sh :var input=dist-graph :results file convert -depth 300 -rotate 90 $input pps.png echo "pps.png" #+end_src ** input from an example block #+results: lorem #+begin_example Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enimad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. #+end_example #+begin_src emacs-lisp :var lorem=lorem (message "%d words in Lorem" (length (split-string lorem))) #+end_src #+results: : 68 words in Lorem #+results: 1D | 1 | | 2 | | 3 | | 4 | #+begin_src emacs-lisp :var lst=1D[:,0] lst #+end_src #+results: : 1 ** fixing result insertion needs to replace the results when there is a new hash *** normal results #+begin_src sh date #+end_src #+results: : Mon Jul 12 22:18:16 PDT 2010 *** unnamed source block results #+begin_src emacs-lisp :cache yes (+ 1 2 3 4) #+end_src #+results[16a776d6d139e1d39e99d736536a546df115c2dc]: : 10 #+begin_src emacs-lisp :cache yes (list '(1 2 3) '(4 5 6)) #+end_src #+results[53f489ed6977857b9945d79d06e575b2cbbebf11]: | 1 | 2 | 3 | | 4 | 5 | 6 | *** named source block results #+srcname: something-w-table #+begin_src emacs-lisp (sleep-for 2) (list '(1 2 3) '(4 5 8)) #+end_src #+source: something #+begin_src emacs-lisp :cache yes (+ 1 2 3 4 8) #+end_src Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enimad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. # something else #+results[d053f6643d9dc52a0e804c15f2a762da73a00a07]: something : 18 #+attr_latex: width=0.4\textwidth #+results[5fac69648ab749ef9ee88ea65b3d49d93f3f6cc8]: something-w-table | 1 | 2 | 3 | | 4 | 5 | 8 | ** example w/o source name delete emacs-lisp below for errors #+begin_src emacs-lisp (* (+ 1 1 1) (+ 1 1 1) (+ 1 1 1) (+ 1 1 1) (+ 1 1 1) (+ 1 1 1) (+ 1 1 1)) #+end_src ** limited precision #+results: anova-example | Effect | DFn | DFd | SSn | SSd | F | p | p<.05 | pes | |--------+-----+-----+-----------------+------------------+------------------+----------------------+-------+------------------| | Days | 9 | 153 | 166235.12250176 | 151101.038615303 | 18.7026979326383 | 8.99534541600196e-21 | * | 0.52384550792003 | #+begin_src emacs-lisp :var tab=anova-example :colnames yes :cache yes (mapcar (lambda (row) (mapcar (lambda (cell) (if (numberp cell) (format "%.4f" cell) cell)) row)) tab) #+end_src #+results[16ac354f1e7a65594bb59e252ab221e6a4b10f80]: | Effect | DFn | DFd | SSn | SSd | F | p | p<.05 | pes | |--------+--------+----------+-------------+-------------+---------+----------------------+-------+--------| | Days | 9.0000 | 153.0000 | 166235.1225 | 151101.0386 | 18.7027 | 8.99534541600196e-21 | * | 0.5238 | ** export blocks w/o languages should raise an error source #+begin_src emacs-lisp ;; this is a comment (+ 1 1 1) #+end_src broken source #+begin_src (+ 2 2 2) #+end_src example #+begin_example this is exampled #+end_example #+begin_src ruby # this is the first [1, 2, 3, 4, 5].map{|r| r+1} #+end_src ** scratch #+begin_src emacs-lisp :exports results (+ 1 1 1 1) (setq org-export-babel-evaluate t) #+end_src and now for src_emacs-lisp{87} an inline block looking at paths #+begin_src emacs-lisp (buffer-file-name) #+end_src ** dot #+begin_src dot :file models.png :cmdline -Tpng digraph data_relationships { "data_requirement" [shape=Mrecord, label="{DataRequirement|description\lformat\l}"] "data_product" [shape=Mrecord, label="{DataProduct|name\lversion\lpoc\lformat\l}"] "data_requirement" -> "data_product" } #+end_src #+results: [[file:models.png]] ** Tom found a bug #+begin_src emacs-lisp :tangle something.el (list 1 (+ 2 3)) #+end_src #+results: | 1 | 5 | ** python errors #+begin_src python :session :results value [1, [2], 3, 4] #+end_src #+results: | 1 | (2) | 3 | 4 | #+begin_src ruby :results output :session [1, 2, 3, 4, 6].map{|n| puts n} #+end_src #+results: : 1 : 2 : 3 : 4 : 6 #+begin_src python :session :results output print 9 #+end_src #+results: R-with-colnames | one | |-----| | 1 | #+begin_src R :results output "something" #+end_src #+results: : [1] "something" #+begin_src R :session *R* :results output 1 2 3 4 #+end_src #+results: : [1] 1 : [1] 2 : [1] 3 : [1] 4 #+begin_src perl :results output print "8\n"; print "9\n"; #+end_src #+results: : 8 : 9 #+begin_src clojure (+ 8 7) #+end_src #+results: : 15 #+begin_src clojure :session *clj* (println "eric") #+end_src #+results: : nil #+begin_src perl :results value 8 #+end_src #+results: : 8 #+begin_src c++ :includes '( ) printf("eric schulte\n"); #+end_src #+results: : eric schulte #+begin_src sh echo 78 #+end_src #+results: : 78 ** tangle R and load :PROPERTIES: :tangle: with-comments.r :comments: yes :END: #+begin_src R :tangle no z <- 0 #+end_src #+begin_src R x <- 8 #+end_src #+begin_src R y <- 9 #+end_src #+srcname: i-have-a-name #+begin_src R x+y+z #+end_src ** table comment issue #+BEGIN_changemargin {-4.2cm}{0cm} #+TBLNAME: AutresFPNVE #+ATTR_LaTeX: align=lrrrrr | | | Montant total (\EUR) | Taux amort (\%) | Part pro. (\%) | Déduc (\%) | NVE (\EUR) | |---+------------------------------------------+----------------------+-----------------+----------------+------------+------------| | | Documentation et formation | 51.05 | | | | 0.00 | | | Communications GSM | 831.16 | 100 | 25 | 100 | 207.79 | | | Internet (Dommel) | 167.88 | 100 | 33 | 100 | 55.40 | | | Fournitures à amortir (ordinateur + GSM) | 762.51 | 33 | 80 | 100 | 201.30 | | | Restaurant | 304.70 | 100 | 100 | 69 | 210.24 | |---+------------------------------------------+----------------------+-----------------+----------------+------------+------------| | | Total | | | | | 1062.02 | | ^ | | | | | | Total | #+tblfm: $7=$3*$4*$5*$6/1000000;%.2f::@2$3=51.05::@3$3=9.00+184.88+51.22+201.82+45.67+69.03+62.93+54.16+38.87+39.77+36.35+37.46::@4$3=12*13.99::@6$3=146.50+158.20;%.2f::@7$7=vsum(@-I..@-II);%.2f #+END_changemargin save me! #+begin_src org , #+TBLNAME: AutresFPNVE , #+ATTR_LaTeX: align=lrrrrr , | | | Montant total (\EUR) | Taux amort (\%) | Part pro. (\%) | Déduc (\%) | NVE (\EUR) | , |---+------------------------------------------+----------------------+-----------------+----------------+------------+------------| , | | Documentation et formation | 51.05 | | | | 0.00 | , | | Communications GSM | 831.16 | 100 | 25 | 100 | 207.79 | , | | Internet (Dommel) | 167.88 | 100 | 33 | 100 | 55.40 | , | | Fournitures à amortir (ordinateur + GSM) | 762.51 | 33 | 80 | 100 | 201.30 | , | | Restaurant | 304.70 | 100 | 100 | 69 | 210.24 | , |---+------------------------------------------+----------------------+-----------------+----------------+------------+------------| , | | Total | | | | | 1062.02 | , | ^ | | | | | | Total | , #+TBLFM: $7=$3*$4*$5*$6/1000000;%.2f::@2$3=51.05::@3$3=9.00+184.88+51.22+201.82+45.67+69.03+62.93+54.16+38.87+39.77+36.35+37.46::@4$3=12*13.99::@6$3=146.50+158.20;%.2f::@7$7=vsum(@-I..@-II);%.2f #+end_src #+begin_example #+tblname: example | 1 | 2 | #+end_example ** latex literals in export #+ATTR_LaTeX: width=\textwidth [[./composite-pattern.png]] ** captions #+caption: I'm not removed from export #+label: also-not-removed | A | B | | 1 | 2 | ** booktabs #+tblname: months | num | Abbrev. | |-----+---------| | 1 | Jan. | | 2 | Feb. | | 3 | Mar. | #+call: booktabs(table=months, align="r|l") :results latex :exports results ** complex #+source: raw-data #+begin_src sh :results scalar wget --quiet -qO- "http://ogdi.cloudapp.net/v1/dc/RecreationParks?format=json" #+end_src #+source: dc-parks #+begin_src emacs-lisp :var keys='(ward area) :var data=raw-data (mapcar (lambda (lis) (mapcar (lambda (key) (cdr (assoc key lis))) keys)) (cdr (car (with-temp-buffer (insert data) (goto-char (point-min)) (json-read))))) #+end_src #+source: dc-parks-metric #+begin_src ruby :var data=dc-parks data.map{|f| [f[0], 2.59 * f[1]]} #+end_src #+begin_src R :var parkData=dc-parks-metric :file parks.png :session *R* plot(parkData) title(main="Park size by Ward") #+end_src #+results: [[file:parks.png]] ** table-label #+label: bam | 1 | | 2 | | 3 | ** haskell issues #+begin_src haskell length [1, 2] #+end_src #+results: : 2 #+tblname: example-4-haskell | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | #+begin_src haskell :var this=example-4-haskell length this #+end_src #+results: : 7 ** possible prefixes | prefix | remaining characters | |----------+----------------------| | ob- | 5 | | org-b- | 2 | | orgb- | 3 | | org-bbl- | 0 | | bbl- | 4 | | babel- | 2 | #+TBLFM: $2='(sbe leftover (prefix $$1)) #+source: leftover #+begin_src emacs-lisp :var prefix="" (- ;; length w/o .el (- 13 (length ".el")) ;; length of prefix (length prefix)) #+end_src ** hlines in python #+tblname: many-cols | a | b | c | |---+---+---| | d | e | f | |---+---+---| | g | h | i | #+source: echo-table #+begin_src python :var tab=many-cols :hlines yes :exports both :session return tab #+end_src #+begin_src emacs-lisp :var table=echo-table :exports none (butlast (apply #'append (mapcar (lambda (el) (list el 'hline)) table))) #+end_src #+call: echo-table(tab=many-cols) #+begin_src python :exports results return [['foo', 'bar', 'baz'], ["a", "b", "None of the above"], ['1', 2, 3]] #+end_src #+begin_src emacs-lisp :exports results (message "Exist") #+end_src ** protecting block bodies neither of these work as expected #+begin_src org ,#+TITLE: stuff ,#+begin_src emacs-lisp , (message "something") ,#+end_src ,more stuffs ,#+resname: something ,: value ,# and a comment #+end_src #+begin_src org ,* example org ,# this is a comment ,this is not a comment #+end_src #+begin_src org ,* escaped org-mode markup ,this should be exported as is ,#+results: escaping-example ,: 24 #+end_src #+begin_html
#comment
#+end_src
#+end_html final ** multiple evals for refs #+begin_src emacs-lisp (setq counter 0) #+end_src #+results: : 0 #+source: counter #+begin_src emacs-lisp (setq counter (+ 1 counter)) counter #+end_src #+begin_src emacs-lisp :var counter_val=counter counter_val #+end_src #+results: : 3 ** tangling #+begin_src sh :shebang #!/bin/sh :tangle yes date #+end_src #+begin_src sh :shebang #!/bin/bash :tangle whoisme :exports both echo $USER #+end_src #+begin_src emacs-lisp :tangle yes :comments yes (message "BAM") #+end_src #+begin_src fortran :exports both 1+8 #+end_src ** cache on export do we export cached blocks #+begin_src sh :cache yes :exports results date #+end_src #+results[06ed73c6d8d022cf9c323d92af885952865add17]: : Thu Jun 17 07:35:19 PDT 2010 ** foo org :PROPERTIES: :session: *R* :END: Figure \ref{fig:one} (p. \pageref{fig:one}) is produced by the following code #+BEGIN_SRC R plot(x, y) abline(out1) #+END_SRC Note that =x=, =y=, and =out1= are remembered from the preceding code chunk. We don't have to regenerate them. All code chunks are part of one R "session". and more stuff here and then the results #+attr_latex: width=0.8\textwidth,placement=[p] #+label: fig:one #+caption: Scatter Plot with Regression Line [[file:fig1.pdf]] ** comments not commented # $some stuff # some more stuff$ -- I should be a comment line 1) a source block inside of an =enumerate= #+source: plotxy #+begin_src emacs-lisp :exports results (message "I think so") #+end_src #+begin_src emacs-lisp (message "don't eat me") #+end_src can cause problems 2) how about this one... ** don't eat me! 1) a source block inside of an =enumerate= #+begin_src emacs-lisp :exports results (list (list "I'm hungry" "I'm hungry") (list "I'm hungry" "I'm hungry") (list "I'm hungry" "I'm hungry")) #+end_src #+results: | I'm hungry | I'm hungry | | I'm hungry | I'm hungry | | I'm hungry | I'm hungry | #+begin_src emacs-lisp (message "don't eat me") #+end_src can cause problems 2) source blocks should be able to be on adjacent lines ** simple reference #+tblname: table-the-first | 1 | 2 | 3 | | 4 | 5 | 6 | | 7 | 8 | 9 | #+begin_src emacs-lisp :var data=table-the-first[1,1] data #+end_src #+results: : 5 ** exporting with call lines #+source: rpn-to-alg(alg) #+begin_src clojure :results output :var alg="00+" (def binary-operators '(\+ \- \* \/)) (def unary-operators '(\s)) (defn rpn-to-alg [chars stack] (if (> (.size chars) 0) (let [el (first chars)] (if (some #{el} binary-operators) (rpn-to-alg (rest chars) (cons (apply str "(" (or (second stack) 1) " " el " " (or (first stack) 1) ")") (rest (rest stack)))) (if (some #{el} unary-operators) (rpn-to-alg (rest chars) (cons (apply str "(" el " " (or (first stack) 1) ")") (rest (rest stack)))) (rpn-to-alg (rest chars) (cons el stack))))) (first stack))) (println (apply str (rpn-to-alg (seq alg) '()))) #+end_src #+results: distributed-best : 73*x11/+4/++51xxx13*y/++6y5*6/6-+xx+* #+call: rpn-to-alg(alg=distributed-best) ** can't open indented results #+begin_src latex :packages '(("" "tikz") ("active,tightpage" "preview")) :file recursion.pdf \begin{preview} \ovalbox{ \begin{tikzpicture} \node{$n$} child { node{$\left(\frac{n}{2}\right)^2$} child{ node{$\left(\frac{n}{4}\right)^2$} node{$\left(\frac{n}{4}\right)^2$} } } child{ node{$\left(\frac{n}{2}\right)^2$} child{ node{$\left(\frac{n}{4}\right)^2$} node{$\left(\frac{n}{4}\right)^2$} } }; \end{tikzpicture} } \end{preview} #+end_src #+results: [[file:recursion.pdf]] ** indented source-code blocks and indented results #+source: time #+begin_src emacs-lisp :results append ;; (list (list (current-time-string))) (current-time-string) #+end_src #+results: time | 1 | 2 | 3 | #+call: time() :results prepend #+results: time() | 1 | 2 | 3 | : Thu Jun 10 14:13:21 2010 : Thu Jun 10 14:13:21 2010 : : Thu Jun 10 14:13:21 2010 : : Thu Jun 10 14:13:21 2010 : : Thu Jun 10 14:13:21 2010 : nil : nil : Thu Jun 10 14:11:22 2010 : Thu Jun 10 14:11:20 2010 : nil : nil : Thu Jun 10 14:06:04 2010 : Thu Jun 10 14:06:03 2010 : Thu Jun 10 14:05:51 2010 : Thu Jun 10 14:05:57 2010 : Thu Jun 10 14:06:00 2010 ** not expand inlines in examples : src_emacs-lisp{(+ 1 2 3)} #+begin_example src_emacs-lisp{(+ 1 2 3)} #+end_example src_emacs-lisp{(+ 1 2 3)} #+begin_example src_emacs-lisp{(+ 1 2 3)} #+end_example ** indented source names #+srcname: i-am-indented #+begin_src emacs-lisp (message "i am indented") #+end_src #+results: i-am-indented : i am indented #+begin_src emacs-lisp :var output=i-am-indented (length output) #+end_src #+results: : 13 #+results: : eric ** updating results "in-situ" #+results: in-situ : update me in place please -- Mon Jun 7 16:44:44 2010 : update me in place please -- Mon Jun 7 16:44:43 2010 : update me in place please -- Mon Jun 7 16:44:42 2010 : update me in place please -- Mon Jun 7 16:44:37 2010 : update me in place please -- Mon Jun 7 16:42:14 2010 : update me in place please (at the bottom) -- Mon Jun 7 16:44:59 2010 : update me in place please (at the bottom) -- Mon Jun 7 16:45:00 2010 : update me in place please (at the bottom) -- Mon Jun 7 16:45:02 2010 the results should be *above* the block #+srcname: in-situ #+begin_src emacs-lisp :results prepend (format "update me in place please -- %s" (current-time-string)) #+end_src #+srcname: in-situ #+begin_src emacs-lisp :results append (format "update me in place please (at the bottom) -- %s" (current-time-string)) #+end_src ** inhibiting evaluation on export :PROPERTIES: :noeval: don't do it :END: #+begin_src clojure :session eric :exports none (+ 1 1 1 1) (error) #+end_src ** executing emacs-lisp on export #+begin_src emacs-lisp (error "eric") #+end_src ** stripping existing results #+results: trickily-located-somehwere-else : I shouldn't be exported Neither of the result strings for the following two code blocks should be included in the export. And only one of the bodies should be included... #+begin_src emacs-lisp :exports code (+ 1 1 1 1) #+end_src #+results: : don't include me in the export!!!!!!! #+srcname: trickily-located-somehwere-else #+begin_src emacs-lisp :exports none (message "I shouldn't be exported") #+end_src ** export with existing results #+begin_src emacs-lisp :exports none :results silent '((1 2) (3 4)) #+end_src #+results: | 1 | 2 | | 3 | 4 | #+begin_src ditaa :file /tmp/eric.png :exports none :results silent +---------------+ | | | | | | +-----------------+ | Eric | | | | | | Schulte | | | | | | | +-----------------+ +---------------+ #+end_src #+results: [[file:/tmp/eric.png]] ** non-empty comint prompt #+begin_src ruby :session eric 8 + 9 #+end_src ** unwind-protect with narrowing I'm not in the subtree *** I'm in the subtree #+begin_src emacs-lisp (+ 6 "I'm not a number!!") #+end_src ** commas on tangling test test comma protection on tangling #+begin_src emacs-lisp :results silent (org-babel-add-interpreter "org") (add-to-list 'org-babel-tangle-langs '("org" "org")) #+end_src #+begin_src org :tangle commas.org ,* org-mode , :PROPERTIES: , :CUSTOM_ID: comma-protect , :END: ,#+begin_src emacs-lisp , protected? ,#+end_src #+end_src #+begin_example ,* this should be # commented out and maybe not this... #+end_example ** simple table #+begin_src emacs-lisp '((1 2 3) (4 5 6) (7 8 900)) #+end_src #+results: | 1 | 2 | 3 | | 4 | 5 | 6 | | 7 | 8 | 900 | ** inline expressions :PROPERTIES: :session: 'default :END: #+begin_src R :exports code :results silent x<-4 #+end_src the sum of 1 and x is equal to src_R{x+1}, now I'll sneakily reset this value in a hidden inline block src_R[:exports none]{x<-2}, so it's value is now src_R{x}. ** adding file names to literal values on export #+results: three : 9 #+begin_src R :var num=three :exports results runif(n=num, min=0, max=1) #+end_src #+begin_src R :var num=3 :exports results runif(n=num, min=0, max=1) #+end_src ** appending tangle :PROPERTIES: :tangle: appended.el :END: append all these block #+begin_src emacs-lisp (message "block %d" 1) #+end_src #+begin_src emacs-lisp (message "block %d" 2) #+end_src #+begin_src emacs-lisp (message "block %d" 3) #+end_src ** visibility affecting execution *** folding lets test folding **** folded #+begin_src emacs-lisp (message "folded1") #+end_src #+results: : folded1 #+begin_src emacs-lisp (message "folded2") #+end_src #+results: : folded2 **** unfolded #+begin_src emacs-lisp (message "unfolded1") #+end_src #+results: : unfolded1 #+begin_src emacs-lisp (message "unfolded2") #+end_src #+results: : unfolded2 ** empty code blocks -- and latex vs. LaTeX eric #+begin_src latex #+end_src michael #+begin_src LaTeX #+end_src schulte #+begin_src emacs-lisp (message "error") #+end_src ** colnames #+tblname: A | a | b | c | |---+---+---| | d | e | f | | g | h | i | #+begin_src python :var tab=A :colnames yes return [[val + '*' for val in row] for row in tab] #+end_src #+results: | a | b | c | |----+----+----| | d* | e* | f* | | g* | h* | i* | #+tblname: A | a | b | c | | d | e | f | | g | h | i | #+begin_src ruby :var tab=A :colnames yes tab.map{|r| r.map{|e| e+"*"} } #+end_src #+results: | a | b | c | |----+----+----| | d* | e* | f* | | g* | h* | i* | ** lisps not fully eval'd #+begin_src emacs-lisp (message "one") (message "two") #+end_src #+results: : two #+begin_src clojure :session :default (println "one") (println "two") (+ 1 2) #+end_src #+results: : 3 ** tangling org #+begin_src org :tangle ~/Desktop/test.org ,* first ,| eric | me | ,| patton | my dog | ,* second ,some more stuff... ,#+HTML: I bet this is quoted #+end_src #+begin_src ruby :tangle ~/Desktop/test.rb # this is a comment eric.map{|l| puts l} #+end_src ** colnames mismatched sizes #+tblname: mismatch-colnames | a | b | |---+----| | 1 | 8 | | 2 | 9 | | 3 | 10 | | 4 | 11 | #+begin_src python :var tab=mismatch-colnames return [[1, 2, 3]] #+end_src #+results: | 1 | 2 | 3 | #+begin_src python :var tab=mismatch-colnames return [[1, 2]] #+end_src #+results: | a | b | |---+---| | 1 | 2 | #+begin_src python :var tab=mismatch-colnames :colnames yes return [1,2] #+end_src #+results: | 1 | 2 | ** variable indexing #+TBLNAME: MyTable | X | Y | |-----+----| | 0 | 0 | | 1 | 1 | | 2 | 4 | | 3 | 9 | | 4 | 16 | | 5 | 25 | |-----+----| | Sum | 55 | #+TBLFM: $2=$1*$1::@8$2=vsum(@2..@-1) #+begin_src python :var sum=MyTable[2:7,1] :exports none return sum #+end_src #+results: | 0 | 1 | 4 | 9 | 16 | 25 | #+begin_src python :var sum=MyTable[9,1] :exports none return sum #+end_src #+results: : 55 #+begin_src gnuplot :var data=MyTable[1:-2] :var sum=MyTable[7,1] :results silent :exports none reset set label "Sum: %.0f",sum at graph 0.03, graph 0.93 plot data with linespoints #+end_src ** hline processing #+tblname: many-cols | a | b | c | |---+---+---| | d | e | f | |---+---+---| | g | h | i | #+tblname: less-cols | 1 | |---| | 2 | | 3 | #+tblname: less-cols2 | 1 | 2 | 3 | #+begin_src emacs-lisp :var tab=many-cols (message "%S" tab) ;; (remove 'hline tab) ;; (flet ((rem-hline (el) ;; (if (listp el) ;; (remove nil (mapcar #'rem-hline el)) ;; (if (equal 'hline el) nil el)))) ;; (rem-hline tab)) #+end_src #+begin_src ruby :var tab=less-cols tab #+end_src #+results: | 1 | |---| | 2 | | 3 | #+begin_src ruby :var one=2 1 + 2 #+end_src #+results: : 3 #+begin_src python :var tab=less-cols return tab #+end_src #+results: | 1 | |---| | 2 | | 3 | #+begin_src ruby :var tab=less-cols :colnames no tab #+end_src #+results: | 1 | | 2 | | 3 | #+begin_src emacs-lisp :var tab=row-and-col-names (message "%S" tab) #+end_src #+results: : (("" "c1" "c2" "c3") hline ("r1" 1 4 7) ("r2" 2 5 8) ("r3" 3 6 9)) #+tblname: row-and-col-names | | c1 | c2 | c3 | |----+----+----+----| | r1 | 1 | 4 | 7 | | r2 | 2 | 5 | 8 | | r3 | 3 | 6 | 9 | functions #+begin_src emacs-lisp (defun org-babel-del-hlines (table) "Remove all 'hlines from TABLE." (remove 'hline table)) (defun org-babel-get-colnames (table) "Return a cons cell, the `car' of which contains the TABLE less colnames, and the `cdr' of which contains a list of the column names" (if (equal 'hline (second table)) (cons (cddr table) (car table)) table)) (defun org-babel-get-rownames (table) "Return a cons cell, the `car' of which contains the TABLE less colnames, and the `cdr' of which contains a list of the column names. Note: this function removes any hlines in TABLE" (flet ((trans (table) (apply #'mapcar* #'list table))) (let ((table (trans (remove 'hline table)))) (cons (cdr table) (car table))))) (defun org-babel-put-colnames (table colnames) "Add COLNAMES to TABLE if they exist." (if colnames (apply 'list colnames 'hline table) table)) (defun org-babel-put-rownames (table rownames) "Add ROWNAMES to TABLE if they exist." (if rownames (mapcar (lambda (row) (if (listp row) (cons (or (pop rownames) "") row) row)) table) table)) #+end_src ** test gnuplot #+begin_src gnuplot plot sin(x), x+5 #+end_src ** evaluate references #+begin_src emacs-lisp :var var=`(+ 9 ,(- 19 7)) :tangle yes (message "var is %S" var) #+end_src #+begin_src emacs-lisp (+ 1 2) #+end_src #+results: : 3 #+begin_src ruby + 1 2 #+end_src #+results: : nil ** tangling and variable resolution :PROPERTIES: :ID: 18b4f1be-bb1d-49bc-a651-c97406a35bdd :tangle: yes :END: #+source: A #+begin_src emacs-lisp :eval no :expand yes :var id=(org-entry-get nil "ID" t) :var two=2 (concat "This is the entry ID: " id) #+end_src #+results: A : This is the entry ID: 18b4f1be-bb1d-49bc-a651-c97406a35bdd ** latex attributes #+ATTR_LaTeX: width=0.38\textwidth wrap placement={r}{0.4\textwidth} #+begin_src ditaa :file=scrap.png +---------------------------+ | | | latex | | | | +------------+ | | | | | | | | | | | cBLU | | | +------------+ | | cPNK | +---------------------------+ #+end_src ** access to variables set in property drawers :PROPERTIES: :special: 89 :text: schulte :END: : "(org-entry-get nil "special" t)" #+begin_src emacs-lisp :var special=(string-to-number (org-entry-get nil "special" t)) (+ special 1) #+end_src #+results: : 90 #+begin_src emacs-lisp :var special=(org-entry-get nil "text" t) special #+end_src #+results: : schulte ** variables into shell scripts #+results: into-shell-scripts | username | guest | | password | nothing | #+begin_src sh :var username=into-shell-scripts[0,0] :var password=into-shell-scripts[1,1] :results output echo "$username -p $password" #+end_src #+results: : username -p nothing #+results: number-into-shell : 9 #+begin_src sh :var num=number-into-shell for i in `seq $num`; do echo $i done #+end_src #+results: | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | ** results lines for function calls #+call: fibonacci(input=5) :resname eric #+results: : 8 #+begin_src emacs-lisp :var fib=fibonacci(input=5) (message "fib(5)=%d" fib) #+end_src #+results: : fib(5)=8 ** haskell variables playing with Haskell #+results: haskell-stuff : 9 #+begin_src haskell :var num=haskell-stuff num + 1 #+end_src #+begin_src ruby :var num=haskell-stuff num + 1 #+end_src #+results: : 10 ** list index w/function style name #+results: function-style-index | 0 | | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | #+srcname: function-style-indexing(data=function-style-index[1:4,0]) #+begin_src emacs-lisp (message "%S" data) #+end_src #+results: function-style-indexing : ((1) (2) (3) (4)) ** looking at source name exports #+source: fibonacci #+begin_src emacs-lisp :var input=0 (defun fib (n) (if (> n 1) (+ (fib (- n 1)) (fib (- n 2))) 1)) (fib input) #+end_src #+results: fibonacci : 1 now applying our Fibonacci function #+call: fibonacci(input=5) ** short shell test #+begin_src sh date #+end_src #+results: : Sun Feb 7 10:17:44 MST 2010 #+tblname: fibs | 1 | 1 | | 2 | 1 | | 3 | 2 | | 4 | 3 | | 5 | 5 | | 6 | 8 | #+begin_src sh :var table=fibs echo "$table" |wc #+end_src #+results: : 6 12 24 #+begin_src sh :var table=fibs echo "$table" #+end_src #+results: | 1 | 1 | | 2 | 1 | | 3 | 2 | | 4 | 3 | | 5 | 5 | | 6 | 8 | #+begin_src sh :var table=fibs :separator -- echo "$table" | head -1 #+end_src #+results: : 1--1 ** tables to shell scripts ideas #+tblname: sec | Hello | World | 1) allowing the user to specify a separator with a header argument as follows #+begin_src sh :var table=sec :separator , cat <> if not then we would probably need to wrap source-code blocks in figures to make them referable. how about a link back to [[keystone]] The above appears to work in LaTeX, but not in HTML. ** fancier export #+source: square #+begin_src emacs-lisp :var input=1 (* input input) #+end_src ** exporting org-source #+begin_src org ,lets see how this org-mode code exports to html ,is this [[link]] blue? ,#+begin_src emacs-lisp , (+ 1 2) ,#+end_src #+end_src ** exporting and caching #+begin_src ditaa :file data/example.png :exports none +------------------+ | ditaa example | | | | | +------------------+ #+end_src #+results: [[file:data/example.png]] ** no noweb by default #+srcname: sample #+begin_src emacs-lisp (message "sample") #+end_src #+begin_src emacs-lisp :noweb no <> #+end_src #+results: : sample ** looking at double quotes #+tblname: double-quote-test-input | test | this | 8 | 9 | #+srcname: double-quote-test-output #+begin_src python :var data=double-quote-test-input return data #+end_src #+results: double-quote-test-output | test | this | 8 | 9 | ** quoted session name #+begin_src sh :session "eric" echo 'name-me' #+end_src #+results: : name-me ** eval-buffer #+begin_src emacs-lisp (+ 1 2) #+end_src #+results: : 3 #+begin_src emacs-lisp (+ 3 4) #+end_src #+results: : 7 ** gnuplot variable expansion #+source: simple-function #+begin_src emacs-lisp "sin(x)" #+end_src #+begin_src gnuplot :var fun=simple-function plot $fun #+end_src ** debug hints from mailing list - edebug-defun: (in emacs-lisp mode, C-u C-M-x) will mark the function so that when it is called, the interpreter stops and you can then single-step through it with . At each point, you can press "e" and evaluate variables (actually arbitrary expressions). - Insert a strategically placed (debug) call and then call the function. If/when the debug call is executed, you are dropped into the debugger and you can then evaluate arbitrary expressions. ** sql exports to latex example from email list *** ECM - faire un script Bash (et =isql=) envoyant un /listing/ de stagiaires; #+srcname: envoi-stg #+begin_src sql DECLARE @dateFmtStyleIn int; SET @dateFmtStyleIn = 120 -- ODBC canonical DECLARE @dateFmtStyleOut int; SET @dateFmtStyleOut = 103 -- French dd/mm/yyyy DECLARE @firstDayOfThisMonth smalldatetime SET @firstDayOfThisMonth = CONVERT(smalldatetime, CAST(YEAR(GETDATE()) AS char(4)) + '-' + CAST(MONTH(GETDATE()) AS char(2)) + '-' + '01' + ' 00:00:00', @dateFmtStyleIn) DECLARE @now smalldatetime SET @now = CONVERT(smalldatetime, CAST(YEAR(GETDATE()) AS char(4)) + '-' + CAST(MONTH(GETDATE()) AS char(2)) + '-' + CAST(DAY(GETDATE()) AS char(2)) + ' ' + CAST(DATEPART(hh, GETDATE()) AS char(2)) + ':' + CAST(DATEPART(mi, GETDATE()) AS char(2)) + ':' + '00', @dateFmtStyleIn) SELECT pfiID FROM dossier #+end_src ** whitespace/newline results issues #+begin_src sh echo output #+end_src This #+begin_src sh echo output #+end_src text here results in t#+results: : output ext here #+begin_src emacs-lisp (+ 1 1) #+end_src #+results: : 2 ** sh with sessions #+begin_src sh :session eric cd ~/Desktop #+end_src #+begin_src sh :session eric cd ~/Desktop/clj/ ls *.clj #+end_src #+results: | "ants.clj" | "" | "concurrent.clj" | "" | "hello.clj" | "" | "spell-checker.clj" | ** testing srcname aliases #+source: two #+begin_src emacs-lisp 2 #+end_src #+begin_src emacs-lisp :var input=two (+ input 1) #+end_src #+results[1ec6c8d3de6aaeac7b2720f1d801402e762875ea]: : 3 ** hiding results #+begin_src emacs-lisp (mapcar (lambda (el) (list el)) (number-sequence 0 20)) #+end_src #+results: | 0 | | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | | 10 | | 11 | | 12 | | 13 | | 14 | | 15 | | 16 | | 17 | | 18 | | 19 | | 20 | ** elisp references #+begin_src emacs-lisp :results silent (setq first 10) #+end_src #+srcname: resolve(name=nil) #+begin_src emacs-lisp :results silent (eval (intern name)) #+end_src #+begin_src python :var a=resolve(name="first") return a + 10 #+end_src ** elisp variables #+begin_src ditaa :file (format "%d.png" 45) +-----------+ | | | | | | | | +-----------+ #+end_src #+results: [[file:45.png]] ** haskell and tables #+begin_src haskell sumListCond :: Int -> Int -> [Int] -> Int sumListCond l n xs | foldl (+) 0 (take l xs) <= n = sumListCond (l + 1) n xs | otherwise = foldl (+) 0 (take (l - 1) xs) #+end_src #+begin_src oz #+end_src ** latex pngs $x \mapsto y$ *** Theorem $|consts(t)| \leq sizes(t)$ - by induction on the structure of t - base cases are $t \in [true, false, 0]$: - $|consts(t)| = |[t]| = 1 = size(t)$ - inductive size - $t \in [succ(t_1), pred(t_1), iszero(t_1)]$: - $|consts(t)| = |consts(t_1)| = |[t]| \leq size(t_1) < size(t)$ - $t = if\, t_1 \, then \, t_2 \, else t_3$ - $|consts(t)| = |consts(t_1) \cup consts(t_1) \cup consts(t_1)|$ - $\leq |consts(t_1)| + |consts(t_1)| + |consts(t_1)|$ - $\leq size(t_1) + size(t_1) + size(t_1)$ - $< size(t)$ ** indexing into gnuplot #+tblname: squares | 1 | 1 | | 2 | 4 | | 3 | 9 | | 4 | 16 | | 5 | 25 | | 6 | 36 | #+begin_src gnuplot :var data=squares :results silent plot data using 1:2 with lines #+end_src #+tblname: squares-with-sum | 1 | 1 | | 2 | 4 | | 3 | 9 | | 4 | 16 | | 5 | 25 | | 6 | 36 | |----+----| | 21 | 91 | #+begin_src gnuplot :var data=squares-with-sum[0:-3] :results silent plot data using 1:2 with lines #+end_src ** multiple arguments #+begin_src emacs-lisp :var first=9 :var second=10 (+ first second) #+end_src #+resname: : 19 ** indexing into results #+data: indexable-table | eric | | michael | | schulte | | is | | my | | name | #+begin_src emacs-lisp :var data=indexable-table[2:4] data #+end_src #+results: | schulte | | is | | my | #+tblname: multidimensional-indexing | 1 | 2 | | 3 | 4 | | 5 | 6 | | 7 | 8 | | 9 | 10 | #+begin_src emacs-lisp :var data=multidimensional-indexing[0:-2] data #+end_src #+resname: | 1 | 2 | | 3 | 4 | | 5 | 6 | | 7 | 8 | ** cached results #+begin_src emacs-lisp :cache yes (setq org-babel-default-header-args '((:session . "none") (:results . "replace") (:exports . "code")(:cache))) #+end_src #+results[937269632ae5b5eee5c93f9eb50e0bc55e34520d]: | (:session . none) | (:results . replace) | (:exports . code) | (:cache) | #+srcname: eric-schulte #+begin_src emacs-lisp :cache yes (+ 5 7 1) #+end_src #+results[005b04829608b3d22b61686e90309af3a9a6fe7c]: eric-schulte : 13 #+begin_src ditaa :file caching-example.png +--------------------+ | | +-----------+ | | | | | | | | | +----+ | | | | | | | +-----------+ | +----+ | | | +--------------------+ #+end_src #+results[fd11ddbfd00f6038e6e37db71ddaf43d65b0e200]: [[file:caching-example.png]] ** switches and references #+begin_src ruby -n -r -l "(ref:%s)" :results output class Schulte def self.eric puts :imp # (ref:imp) end end Schulte.eric #+end_src #+resname[bb4cebabe38a5d3d43835acebdbe17aa3314cef6]: : imp Line no. [[(imp)]] is important! #+begin_src ruby -n -r -l "(ref:%s)" :results output class Schulte def self.eric puts :imp # (ref:imp) end end Schulte.eric # (ref:output) #+end_src #+resname: eric : imp ** unresolved noweb references #+begin_src emacs-lisp :results silent (setq org-babel-noweb-error-langs '("ruby")) #+end_src #+srcname: i-have-a-name #+begin_src ruby 1 + 2 #+end_src #+begin_src ruby :noweb <> + 3 #+end_src #+resname: : 6 ** clojure #+begin_src clojure :results silent (list 8 9) #+end_src ** reference parts of tables #+TBLNAME: squares | 1 | 1 | | 2 | 4 | | 3 | 9 | | 4 | 16 | | 5 | 25 | | 6 | 36 | | 7 | 49 | | 8 | 64 | | 9 | 81 | | 10 | 100 | | 11 | 121 | | 12 | 144 | | 13 | 169 | | 14 | 196 | | 15 | 225 | | 16 | 256 | | 17 | 289 | | 18 | 324 | #+TBLFM: $2=$1*$1 #+begin_src gnuplot :var data=squares set title "Implementing Gnuplot" plot data using 1:2 with lines #+end_src ** results switches #+begin_src ruby :results output :results_switches -n 10.times do |n| puts "-"*n end #+end_src #+resname: #+begin_example -n - -- --- ---- ----- ------ ------- -------- --------- #+end_example #+begin_src ruby :results output 10.times do |n| puts "-"*n end #+end_src #+resname: #+begin_example - -- --- ---- ----- ------ ------- -------- --------- #+end_example ** xml and n3 introduce org-babel to =xml= and =n3= #+begin_src emacs-lisp :results silent (add-to-list 'org-babel-interpreters "xml") (add-to-list 'org-babel-interpreters "n3") #+end_src inform org-babel-tangle of their existence and file extensions #+begin_src emacs-lisp :results silent (add-to-list 'org-babel-tangle-langs '("xml" "xml")) (add-to-list 'org-babel-tangle-langs '("n3" "n3")) #+end_src #+begin_src xml :tangle example #+end_src #+begin_src n3 :tangle example n3 stuff #+end_src ** noweb referernces #+srcname: noweb-example #+begin_src ruby a = 28 #+end_src #+begin_src ruby :noweb # <> a + 4 #+end_src #+resname: : 32 ** =pp= results *** python #+begin_src python :results pp :session ['one', 'two', 'three', 'one', 'two', 'three', 'one', 'two', 'three'] #+end_src #+resname: : ['one', 'two', 'three', 'one', 'two', 'three', 'one', 'two', 'three'] *** ruby #+begin_src ruby :results pp class Schulte attr_accessor :name, :age end eric = Schulte.new eric.name = "eric" eric.age = 27 eric #+end_src #+resname: #+begin_src ruby :results pp a = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3] a #+end_src #+resname: #+begin_example [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3] #+end_example ** empty =output= results for emacs-lisp #+begin_src emacs-lisp :results output 8 #+end_src #+resname: ** =:table= results param #+begin_src emacs-lisp :results table 8 #+end_src #+resname: | 8 | ** code results *** emacs lisp #+begin_src emacs-lisp :results code (mapcar (lambda (el) (lambda (item) (+ item el))) '(1 2 3 4 5)) #+end_src #+resname: #+BEGIN_SRC emacs-lisp ((lambda (item) (+ item el)) (lambda (item) (+ item el)) (lambda (item) (+ item el)) (lambda (item) (+ item el)) (lambda (item) (+ item el))) #+END_SRC #+begin_src emacs-lisp :results code (mapcar (lambda (el) (* el el)) '(1 2 3 89)) #+end_src #+resname: #+BEGIN_SRC emacs-lisp (1 4 9 7921) #+END_SRC *** ruby #+begin_src ruby :results code [1, 2, 33, 4].map{|n| "the number #{n}"} #+end_src #+resname: #+BEGIN_SRC ruby ["the number 1", "the number 2", "the number 33", "the number 4"] #+END_SRC #+begin_src ruby :session :results code [1, 2, 33, 4].map{|n| n + 10 } #+end_src #+resname: #+BEGIN_SRC ruby [11, 12, 43, 14] #+END_SRC *** python #+begin_src python :results code ['one', 'two', 'three'] #+end_src #+resname: #+BEGIN_SRC python ['one', 'two', 'three'] #+END_SRC #+begin_src python :results code [1, 2, 33, 4] #+end_src #+resname: #+BEGIN_SRC python [1, 2, 33, 4] #+END_SRC #+begin_src python :session :results code [1, 2, 33, 4] #+end_src #+resname: #+BEGIN_SRC python [1, 2, 33, 4] #+END_SRC ** indentation #+begin_src python 9 #+end_src ** persistent python #+begin_src python :session :results silent import types #+end_src #+begin_src python :session types.FunctionType #+end_src #+resname: : function *** more persistent python :PROPERTIES: :session: default :END: #+begin_src python :results silent import types #+end_src #+begin_src python types.FunctionType #+end_src #+resname: : function ** quoted latex The following latex isn't exported correctly #+begin_latex \begin{code} data BTree = Leaf a | Node Tree Tree \end{code} #+end_latex #+begin_src haskell data BTree = Leaf a | Node Tree Tree #+end_src ** pretty print #+begin_src emacs-lisp :results scalar '(1 2 3 4) #+end_src ** simple scalar #+begin_src emacs-lisp (+ 1 3) #+end_src #+resname: : 4 ** lua export #+srcname: determine the neighbors of the segments that the bisector hits #+begin_src lua :tangle no :exports code local s1, s2 = intersecting_segs[1], intersecting_segs[2] local n1 = table_find_segment(cell.neighbors, s1) local n2 = table_find_segment(cell.neighbors, s2) #+end_src I got: #+begin_example \lstset{language=lua} \begin{lstlisting} local s1, s2 = intersecting_segs[1], intersecting_segs[2] local n1 = table_find_segment(cell.neighbors, s1) local n2 = table_find_segment(cell.neighbors, s2) \end{lstlisting} #+end_example Emacs -Q got: #+begin_example \begin{verbatim} local s1, s2 = intersecting_segs[1], intersecting_segs[2] local n1 = table_find_segment(cell.neighbors, s1) local n2 = table_find_segment(cell.neighbors, s2) \end{verbatim} #+end_example Emacs -Q + Org-babel got: #+begin_example \begin{verbatim} local s1, s2 = intersecting_segs[1], intersecting_segs[2] local n1 = table_find_segment(cell.neighbors, s1) local n2 = table_find_segment(cell.neighbors, s2) \end{verbatim} #+end_example ** simple R #+begin_src R :session R 8 #+end_src #+resname: : 8 ** changing source name #+srcname: emacs-nine #+begin_src emacs-lisp 8 #+end_src #+resname: emacs-nine : 8 #+resname: emacs-eight : 8 ** advanced table | DATA | WHAT | WHERE | HOW MUCH | |------------------+------------+-------------+----------| | [2009-09-25 Fri] | | | 28.95 | |------------------+------------+-------------+----------| | | food | supermarket | 7.85 | | | ticket bus | | 2.3 | | | tea + ice | ice uno | 4.4 | | | ticket | | 14.4 | |------------------+------------+-------------+----------| | [2009-09-26 Sat] | | | 41 | #+begin_src emacs-lisp (let ((total 0) (responding t) purchases) (while responding (setq purchases (cons (list "" (read-from-minibuffer "What: ") (read-from-minibuffer "Where: ") (read-minibuffer "How Much: ")) purchases)) (setq responding (y-or-n-p "more? "))) (append purchases (list (list (format-time-string "%Y-%m-%d" (current-time)) "" "" (progn (mapc (lambda (purchase) (setq total (+ total (fourth purchase)))) purchases) total))))) #+end_src #+resname: | "" | "fish and chips" | "diner" | 9.78 | | "" | "food" | "subway" | 5.45 | | "2009-09-29" | "" | "" | 15.23 | ** haskell #+begin_src haskell powerSet :: [a] -> [[a]] powerSet = foldr (\ x ps -> map (\ y -> x : y) ps ++ ps ) [[]] #+end_src #+begin_src haskell powerSet [1, 2, 3] #+end_src ** yasnippet ** indented #+begin_src emacs-lisp (message "I ran!!") #+end_src #+resname: : I ran!! ** dynamic table #+TBLNAME: todays-clock #+BEGIN: clocktable :maxlevel 2 :block today :scope tree1 :link t Clock summary at [2009-09-15 Tue 08:51], for Tuesday, September 15, 2009. | L | Headline | Time | | |---+--------------+--------+------| | | *Total time* | *1:10* | | |---+--------------+--------+------| | 1 | [[file:/Users/eschulte/Desktop/test.org::top][top]] | 1:10 | 1 | | 2 | [[file:/Users/eschulte/Desktop/test.org::show%20all][show all]] | | 1:00 | | 2 | [[file:/Users/eschulte/Desktop/test.org::later][later]] | | 0:10 | #+END: clocktable #+begin_src emacs-lisp :var data=todays-clock(1,1) (message "table is %S" data) #+end_src #+resname: : table is (("L" "Headline" "Time" "") hline ("" "*Total time*" "*1:10*" "") hline (1 "[[file:/Users/eschulte/Desktop/test.org::top][top]]" "1:10" 1) (2 "[[file:/Users/eschulte/Desktop/test.org::show%20all][show all]]" "" "1:00") (2 "[[file:/Users/eschulte/Desktop/test.org::later][later]]" "" "0:10")) #+begin_src R :session R-pie-example :var times=todays-clock :results silent pie(times[2:length(times),4], labels = times[2:length(times),2]) #+end_src ** show all CLOCK: [2009-09-15 Tue 07:51]--[2009-09-15 Tue 08:51] => 1:00 :PROPERTIES: :exports: both :END: #+begin_src ditaa :file blue.png +----------------------+ | | | | | +-----------+ | | | | | | | | | | +-----------+ | | +----------------------+ #+end_src ** later CLOCK: [2009-09-15 Tue 09:41]--[2009-09-15 Tue 09:51] => 0:10 stuff here ** and then more and more stuffs here ** asymptote #+begin_src asymptote :file asymptote-test.png :exports code import graph; size(0,4cm); real f(real t) {return 1+cos(t);} path g=polargraph(f,0,2pi,operator ..)--cycle; filldraw(g,pink); xaxis("$x$",above=true); yaxis("$y$",above=true); dot("$(a,0)$",(1,0),N); dot("$(2a,0)$",(2,0),N+E); #+end_src #+resname: [[file:asymptote-test.png]] ** asymptote cosine #+begin_src asymptote :exports code import graph; size(0,4cm); real f(real t) {return cos(t);} path g=polargraph(f,0,2pi,operator ..)--cycle; filldraw(g,pink); for(int i=0; i < 8; ++i) { real j = 0.125 + 0.125*i; real h(real t) {return j;}; path k=polargraph(h, -(acos(j)), acos(j), operator ..); draw(k,blue); } xaxis("$x$",above=true); yaxis("$y$",above=true); dot("$(pi,0)$",(1,0),N); #+end_src ** gnuplot #+begin_src gnuplot plot cosx #+end_src #+end_src