5

I have the same question as this question except that I don't see how to do so other than in elisp. I am using Ruby, but I suppose the question applies to other languages as well. If I return an array of arrays as the value result of some code, I want to have it formatted as a table headed with the column names.

Here is the solution to what I want in elisp:

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

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

But I've tried variants of this in Ruby with no luck. For example:

#+BEGIN_SRC ruby :results value table
    [ [ 'First', 'Second'],
      :hline,
      [ :a, :b ],
      [ :c, :d ]
    ]
#+END_SRC

#+RESULTS:
| (First Second) | :hline | (:a :b) | (:c :d) |

Which is not what I want. So, what is the incantation to use here?

2
  • 1
    You could try the colnames header argument, for example :colnames '("First" "Second").
    – mutbuerger
    Jan 6, 2017 at 15:59
  • 1
    Thanks, but I'm looking for a way to do it from within the code. Jan 6, 2017 at 21:08

1 Answer 1

5

For others who may want to know the answer, I finally figured it out. In the case of ruby, you should insert a nil where you want the hline to appear. Org represents a table as an array of arrays.

Here is what I used to discover the right incantation for ruby:

#+NAME: test
|  Ref | Value |
|------+-------|
| 0001 | blue  |
| 0002 | red   |

#+HEADER: :colnames no
#+HEADER: :hlines yes
#+HEADER: :results value raw
#+BEGIN_SRC ruby :var tab=test
 tab
#+END_SRC

#+RESULTS:
[["Ref", "Value"], nil, [1, "blue"], [2, "red"]]

So to generate a table with column headers and an hline, just produce an array of arrays with the first sub-array an array of headings and the second element a nil instead of a sub-array.

Just use a SRC block for your favorite language to discover how it represents headings.

1
  • The same idea works for Python: supplying a None as a row will produce a hline.
    – mike3996
    Mar 14, 2021 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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