1 #+title: The Library of Babel
2 #+author: Org-mode People
3 #+STARTUP: odd hideblocks
6 The Library of Babel is an extensible collection of ready-made and
7 easily-shortcut-callable source-code blocks for handling common
8 tasks. Org-babel comes pre-populated with the source-code blocks
9 located in this file. It is possible to add source-code blocks from
10 any org-mode file to the library by calling =(org-babel-lob-ingest
13 This file is included in worg mainly less for viewing through the
14 web interface, and more for contribution through the worg git
15 repository. If you have code snippets that you think others may
16 find useful please add them to this file and [[file:~/src/worg/worg-git.org::contribute-to-worg][contribute them]] to
19 The raw Org-mode text of this file can be downloaded at
20 [[repofile:contrib/babel/library-of-babel.org][library-of-babel.org]]
23 A collection of simple utility functions
26 #+begin_src emacs-lisp :var input="echo'd"
31 ** reading and writing files
32 Read the contents of the file at =path= into a string.
34 #+begin_src emacs-lisp :var path=""
35 (with-temp-filebuffer path
36 (buffer-substring (point-min) (point-max)))
39 Read the lines of the file at =path= into a list.
41 #+begin_src emacs-lisp :var path=""
43 (with-temp-filebuffer path
44 (buffer-substring (point-min) (point-max))))
47 Write =data= to a file at =path=. If =data= is a list, then write it
48 as a table in traditional Org-mode table syntax.
50 #+begin_src emacs-lisp :var data="" :var path=""
52 (org-babel-insert-result data))
58 Read local or remote file in [[http://www.json.org/][json]] format into emacs-lisp objects.
60 #+begin_src emacs-lisp :var file='() :var url='()
64 (with-temp-filebuffer file
65 (goto-char (point-min))
71 (goto-char (point-min))
78 Plot column 2 (y axis) against column 1 (x axis). Columns 3 and beyond, if present, are ignored.
80 #+srcname: R-plot(data=R-plot-example-data)
81 #+begin_src R :session *R*
85 #+tblname: R-plot-example-data
92 #+lob: R-plot(data=R-plot-example-data)
94 #+resname: R-plot(data=R-plot-example-data)
100 ** LaTeX Table export
102 This block can be used to wrap a table in the latex =booktabs=
103 environment, it takes the following arguments -- all but the first two
105 | arg | description |
106 |-------+--------------------------------------------|
107 | table | a reference to the table |
108 | align | optional alignment string |
109 | env | optional environment, default to "tabular" |
110 | width | optional width specification string |
113 #+begin_src emacs-lisp :var table='((:head) hline (:body)) :var align='() :var env="tabular" :var width='() :noweb yes :results latex
116 (mapcar (lambda (lis)
121 (format "%S" el))) lis)
123 (list :lend " \\\\" :sep " & " :hline "\\hline"))))
126 \\begin{%env}%width%align
132 (cons "env" (or env "table"))
133 (cons "width" (if width (format "{%s}" width) ""))
134 (cons "align" (if align (format "{%s}" align) ""))
136 ;; only use \midrule if it looks like there are column headers
137 (if (equal 'hline (second table))
138 (concat (to-tab (list (first table)))
140 (to-tab (cddr table)))
145 This block can be used to wrap a table in the latex =longtable=
146 environment, it takes the following arguments -- all but the first two
148 | arg | description |
149 |-----------+-------------------------------------------------------------|
150 | table | a reference to the table |
151 | align | optional alignment string |
152 | width | optional width specification string |
153 | hline | the string to use as hline separator, defaults to "\\hline" |
154 | head | optional "head" string |
155 | firsthead | optional "firsthead" string |
156 | foot | optional "foot" string |
157 | lastfoot | optional "lastfoot" string |
160 #+begin_src emacs-lisp :var table='((:table)) :var align='() :var width='() :var hline="\\hline" :var firsthead='() :var head='() :var foot='() :var lastfoot='() :noweb yes :results latex
163 \\begin{longtable}%width%align
172 (cons "width" (if width (format "{%s}" width) ""))
173 (cons "align" (if align (format "{%s}" align) ""))
174 (cons "firsthead" (if firsthead (concat firsthead "\n\\endfirsthead\n") ""))
175 (cons "head" (if head (concat head "\n\\endhead\n") ""))
176 (cons "foot" (if foot (concat foot "\n\\endfoot\n") ""))
177 (cons "lastfoot" (if lastfoot (concat lastfoot "\n\\endlastfoot\n") ""))
178 (cons "table" (orgtbl-to-generic
179 (mapcar (lambda (lis)
184 (format "%S" el))) lis)
186 (list :lend " \\\\" :sep " & " :hline hline)))))
189 ** Elegant lisp for transposing a matrix.
191 #+tblname: transpose-example
196 #+begin_src emacs-lisp :var table=transpose-example
197 (apply #'mapcar* #'list table)
206 #+srcname: python-identity(a=1)
211 #+srcname: python-add(a=1, b=2)