org-mode/contrib/babel/library-of-babel.org

6.8 KiB
Raw Blame History

The Library of Babel

Introduction

The Library of Babel is an extensible collection of ready-made and easily-shortcut-callable source-code blocks for handling common tasks. Org-babel comes pre-populated with the source-code blocks located in this file. It is possible to add source-code blocks from any org-mode file to the library by calling (org-babel-lob-ingest "path/to/file.org").

This file is included in worg mainly less for viewing through the web interface, and more for contribution through the worg git repository. If you have code snippets that you think others may find useful please add them to this file and contribute them to worg.

The raw Org-mode text of this file can be downloaded at library-of-babel.org

Simple

A collection of simple utility functions

  input

File I/O

reading and writing files

Read the contents of the file at path into a string.

  (with-temp-filebuffer path
    (buffer-substring (point-min) (point-max)))

Read the lines of the file at path into a list.

  (split-string
   (with-temp-filebuffer path
     (buffer-substring (point-min) (point-max))))

Write data to a file at path. If data is a list, then write it as a table in traditional Org-mode table syntax.

  (with-temp-file path
    (org-babel-insert-result data))
  nil

remote files

Read local or remote file in json format into emacs-lisp objects.

  (require 'json)
  (cond
   (file
    (with-temp-filebuffer file
      (goto-char (point-min))
      (json-read)))
   (url
    (require 'w3m)
    (with-temp-buffer
      (w3m-retrieve url)
      (goto-char (point-min))
      (json-read))))

Plotting code

R

Plot column 2 (y axis) against column 1 (x axis). Columns 3 and beyond, if present, are ignored.

plot(data)
1 2
2 4
3 9
4 16
5 25
nil

Gnuplot

Tables

LaTeX Table export

booktabs

This block can be used to wrap a table in the latex booktabs environment, it takes the following arguments all but the first two are optional.

arg description
table a reference to the table
align optional alignment string
env optional environment, default to "tabular"
width optional width specification string
  (flet ((to-tab (tab)
                 (orgtbl-to-generic
                  (mapcar (lambda (lis)
                            (if (listp lis)
                                (mapcar (lambda (el)
                                          (if (stringp el)
                                              el
                                            (format "%S" el))) lis)
                              lis)) tab)
                  (list :lend " \\\\" :sep " & " :hline "\\hline"))))
    (org-fill-template
     "
  \\begin{%env}%width%align
  \\toprule
  %table
  \\bottomrule
  \\end{%env}\n"
     (list
      (cons "env"       (or env "table"))
      (cons "width"     (if width (format "{%s}" width) ""))
      (cons "align"     (if align (format "{%s}" align) ""))
      (cons "table"
            ;; only use \midrule if it looks like there are column headers
            (if (equal 'hline (second table))
                (concat (to-tab (list (first table)))
                        "\n\\midrule\n"
                        (to-tab (cddr table)))
              (to-tab table))))))

longtable

This block can be used to wrap a table in the latex longtable environment, it takes the following arguments all but the first two are optional.

arg description
table a reference to the table
align optional alignment string
width optional width specification string
hline the string to use as hline separator, defaults to "\\hline"
head optional "head" string
firsthead optional "firsthead" string
foot optional "foot" string
lastfoot optional "lastfoot" string
  (org-fill-template
   "
  \\begin{longtable}%width%align
  %firsthead
  %head
  %foot
  %lastfoot
  
  %table
  \\end{longtable}\n"
   (list
    (cons "width"     (if width (format "{%s}" width) ""))
    (cons "align"     (if align (format "{%s}" align) ""))
    (cons "firsthead" (if firsthead (concat firsthead "\n\\endfirsthead\n") ""))
    (cons "head"      (if head (concat head "\n\\endhead\n") ""))
    (cons "foot"      (if foot (concat foot "\n\\endfoot\n") ""))
    (cons "lastfoot"  (if lastfoot (concat lastfoot "\n\\endlastfoot\n") ""))
    (cons "table" (orgtbl-to-generic
                   (mapcar (lambda (lis)
                             (if (listp lis)
                                 (mapcar (lambda (el)
                                           (if (stringp el)
                                               el
                                             (format "%S" el))) lis)
                               lis)) table)
                   (list :lend " \\\\" :sep " & " :hline hline)))))

Elegant lisp for transposing a matrix.

1 2 3
4 5 6
  (apply #'mapcar* #'list table)
1 4
2 5
3 6

Misc

a
a + b