{"id":6699,"date":"2017-10-25T15:18:00","date_gmt":"2017-10-25T19:18:00","guid":{"rendered":"http:\/\/irreal.org\/blog\/?p=6699"},"modified":"2017-10-25T15:18:24","modified_gmt":"2017-10-25T19:18:24","slug":"tables-and-org-mode-code-blocks","status":"publish","type":"post","link":"https:\/\/irreal.org\/blog\/?p=6699","title":{"rendered":"Tables and Org Mode Code Blocks"},"content":{"rendered":"<p> I have lots of Org files that collect data and have an embedded code block to generate a summary table. Here are a couple of examples that others may find useful. The first thing to know is that tables are stored as a list of rows with each row being a list of the row&rsquo;s elements. If we think of a table as an \\(m \\times n\\) matrix, the table is stored as <\/p>\n<p>\\begin{matrix}<br \/>\n((T_{1,1} T_{1,2} \\cdots T_{1,n})<br \/>\n (T_{2,1} T_{2,2} \\cdots T_{2,n})<br \/>\n\\cdots<br \/>\n (T_{m,1} T_{m,2} \\cdots T_{m,n}))<br \/>\n\\end{matrix}<\/p>\n<p> It&rsquo;s pretty easy to build a table from scratch and make it the output of a code block. As a simple example, suppose you have a long table of 3 columns that tracks data over time. You don&rsquo;t want to export the whole table when you output the document because a lot of the data is old and not that interesting anymore. Let&rsquo;s suppose that the table is named (via <code>#+NAME:<\/code>) &ldquo;data-table&rdquo;. Here is a code block to output the last 3 entries. <\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-org\"><span style=\"color: #b22222;\">#+BEGIN_SRC emacs-lisp :var tab=data-table :exports results :results table<\/span>\n<span style=\"color: #7f7f7f;\">  (cons '(<\/span><span style=\"color: #8b2252;\">\"Col. 1\"<\/span><span style=\"color: #7f7f7f;\"> <\/span><span style=\"color: #8b2252;\">\"Col. 2\"<\/span><span style=\"color: #7f7f7f;\"> <\/span><span style=\"color: #8b2252;\">\"Col. 3\"<\/span><span style=\"color: #7f7f7f;\">) (cons 'hline (last tab 3)))<\/span>\n<span style=\"color: #b22222;\">#+END_SRC<\/span>\n<\/pre>\n<\/div>\n<p> The <code>(last tab 3)<\/code> generates a list of the last 3 rows. The two <code>cons<\/code>&rsquo;s put the header and horizontal line on the front of the table. The result is <\/p>\n<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">\n<colgroup>\n<col  class=\"org-right\" \/>\n<col  class=\"org-right\" \/>\n<col  class=\"org-right\" \/>\n<\/colgroup>\n<thead>\n<tr>\n<th scope=\"col\" class=\"org-right\">Col. 1<\/th>\n<th scope=\"col\" class=\"org-right\">Col. 2<\/th>\n<th scope=\"col\" class=\"org-right\">Col. 3<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"org-right\">1<\/td>\n<td class=\"org-right\">2<\/td>\n<td class=\"org-right\">3<\/td>\n<\/tr>\n<tr>\n<td class=\"org-right\">4<\/td>\n<td class=\"org-right\">5<\/td>\n<td class=\"org-right\">6<\/td>\n<\/tr>\n<tr>\n<td class=\"org-right\">7<\/td>\n<td class=\"org-right\">8<\/td>\n<td class=\"org-right\">9<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p> As a second example, suppose we want to use the entire input table for our calculations. Org mode is especially intelligent about this. By default, if you have a header (indicated by the horizontal line), Org will remove the header before sending the table to your code block. It&rsquo;s Emacs so of course that&rsquo;s configurable. You can control what happens to the header with the <a href=\"http:\/\/orgmode.org\/manual\/colnames.html#index-g_t_0040code_007b_003acolnames_007d_002c-src-header-argument-2147\">:colnames header argument<\/a> but most of the time you&rsquo;ll want the default action. <\/p>\n<p> Now suppose we want to build a summary table containing the low and high values of columns 2 and 3. We can do something like <\/p>\n<div class=\"org-src-container\">\n<pre class=\"src src-org\"><span style=\"color: #b22222;\">#+BEGIN_SRC emacs-lisp :var tab=data-table :exports results :results table<\/span>\n<span style=\"color: #7f7f7f;\">  (<\/span><span style=\"color: #a020f0;\">let<\/span><span style=\"color: #7f7f7f;\"> (col2-low col2-high col3-low col3-high)<\/span>\n<span style=\"color: #7f7f7f;\">    <\/span><span style=\"color: #b22222;\">;; <\/span><span style=\"color: #b22222;\">routine calculations to set col1-low, etc.<\/span>\n<span style=\"color: #7f7f7f;\">    `((<\/span><span style=\"color: #8b2252;\">\" \"<\/span><span style=\"color: #7f7f7f;\"> <\/span><span style=\"color: #8b2252;\">\"Low\"<\/span><span style=\"color: #7f7f7f;\"> <\/span><span style=\"color: #8b2252;\">\"High\"<\/span><span style=\"color: #7f7f7f;\">) hline<\/span>\n<span style=\"color: #7f7f7f;\">      (<\/span><span style=\"color: #8b2252;\">\"Col. 2\"<\/span><span style=\"color: #7f7f7f;\"> ,col2-low ,col2-high)<\/span>\n<span style=\"color: #7f7f7f;\">      (<\/span><span style=\"color: #8b2252;\">\"Col. 3\"<\/span><span style=\"color: #7f7f7f;\"> ,col3-low ,col3-hi)))<\/span>\n<span style=\"color: #b22222;\">#+END_SRC<\/span>\n<\/pre>\n<\/div>\n<p> Assuming the Low and High values are as shown, the last quasiquoted expression is output as the table <\/p>\n<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">\n<colgroup>\n<col  class=\"org-left\" \/>\n<col  class=\"org-right\" \/>\n<col  class=\"org-right\" \/>\n<\/colgroup>\n<thead>\n<tr>\n<th scope=\"col\" class=\"org-left\">&#xa0;<\/th>\n<th scope=\"col\" class=\"org-right\">Low<\/th>\n<th scope=\"col\" class=\"org-right\">High<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"org-left\">Col. 2<\/td>\n<td class=\"org-right\">2<\/td>\n<td class=\"org-right\">8<\/td>\n<\/tr>\n<tr>\n<td class=\"org-left\">Col. 3<\/td>\n<td class=\"org-right\">3<\/td>\n<td class=\"org-right\">9<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p> The nice thing is that the &ldquo;routine calculations&rdquo; don&rsquo;t have to worry about the header when accessing the data; it&rsquo;s as if it didn&rsquo;t exists. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have lots of Org files that collect data and have an embedded code block to generate a summary table. Here are a couple of examples that others may find useful. The first thing to know is that tables are &hellip; <a href=\"https:\/\/irreal.org\/blog\/?p=6699\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[18,4],"class_list":["post-6699","post","type-post","status-publish","format-standard","hentry","category-general","tag-emacs","tag-org-mode"],"_links":{"self":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6699"}],"version-history":[{"count":1,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6699\/revisions"}],"predecessor-version":[{"id":6700,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6699\/revisions\/6700"}],"wp:attachment":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}