12

I would like to generate tables that have headers from code blocks. Something like this:

#+BEGIN_SRC emacs-lisp :results value table :table-header | First | Second |
  (identity '((a b) (c d)))
#+END_SRC

#+RESULTS:
| First | Second |
|-------+--------|
| a     | b      |
| c     | d      |

Unfortunately something like :table-header | First | Second | does not exist. Or rather I am hoping that I am overlooking something. (Here I am using (identity ...) as a placeholder for code which actually calculates a value.)

This workaround doesn't quite do it for me:

#+BEGIN_SRC emacs-lisp :results value table
  (cons (list "First" "Second")
        (identity '((a b) (c d))))
#+END_SRC

#+RESULTS:
| First | Second |
| a     | b      |
| c     | d      |

I have to modify the content of the code block and the resulting header is not actually a header row, its a row like any other.

Related to this (but not 100% the same, above I would like to avoid having to put table setup instructions inside the code block, below that is required), is there a special element that causes the insertion of a separator? Something like:

#+BEGIN_SRC emacs-lisp :results value table
  (nconc (identity '((a b) (c d)))  ; calculated value A
         (list 'linebreak)          ; explicitly inserted linebreak
         (identity '((A B) (C D)))) ; calculated value B
#+END_SRC

#+RESULTS:
| a | b |
| c | d |
|---+---|
| A | B |
| B | D |

2 Answers 2

11

Just use hline instead of linebreak. The following works:

* test
#+BEGIN_SRC emacs-lisp :results value table
  '(("First" "Second")
    hline
    (a b) (c d))
#+END_SRC

#+RESULTS:
| First | Second |
|-------+--------|
| a     | b      |
| c     | d      |
2
  • After having played with :colnames as suggested in the other answer, it looks like I'll have to open feature request for #+BEGIN_SRC ... :colnames yes as well as :colnames "| First | Second |". Until then I will go with this approach.
    – tarsius
    Commented Mar 8, 2016 at 13:42
  • Just a note in case anyone finds this a helpful addition: If you're not using quoted values for everything (e.g. maybe you're using a :var header and a, b, c, etc. are actually derived variables which you use list on), just quote the 'hline symbol itself.
    – lindes
    Commented Feb 24, 2018 at 5:26
3

After adding the header row:

  1. Assign #+NAME: to code block.

    #+NAME: needs-a-table-header
    #+BEGIN_SRC emacs-lisp :results value table 
      (cons (list "First" "Second")
            (identity '((a b) (c d))))
    #+END_SRC
    
  2. Add #+CALL: statement with :colnames yes header

    #+CALL: needs-a-table-header() :colnames yes 
    
  3. Run #+CALL: statement using C-c C-c.

    #+RESULTS:
    | First | Second |
    |-------+--------|
    | a     | b      |
    | c     | d      |
    

Hope that helped!


Code Tested using

GNU Emacs 24.5.1 (x86_64-unknown-cygwin, GTK+ Version 3.14.13)
org-version: 8.3.2

2
  • Thanks. To avoid having two tables I prefixed both the code block and the call with #+NAME: same-name. Since I still have to cons the header row when using this approach, I'll go with the solution in the other answer.
    – tarsius
    Commented Mar 8, 2016 at 13:38
  • @tarsius Thanks for update! BTW thanks for asking this question! I know of several different ways to add table headers which you might find helpful. I'll look through my notes an post another answer.
    – Melioratus
    Commented Mar 8, 2016 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.