Search code examples
emacsorg-modeorg-babel

How to include an emacs table in tangled output?


I'd like to insert a table into tangled output as a comment. Using the table name yields a blank result:

#+name: test-data
| type   | amount |
|--------+--------|
| sale   |  31.41 |
| return |   5.92 |

#+BEGIN_SRC python :var data=test-data :tangle test.py :colnames no :noweb yes
  ## Table
  ## <<test-data>>

  [zip(data[0], row) for row in data[1:]]
#+END_SRC

output:

data=[["type", "amount"], ["sale", 31.41], ["return", 5.92]]
## Table
## 

[zip(data[0], row) for row in data[1:]]

Calling the reference yields a lisp list:

#+BEGIN_SRC python :var data=test-data :tangle test.py :colnames no :noweb yes
  ## Table
  ## <<test-data()>>
#+END_SRC

...

## Table
## (("type" "amount") hline ("sale" 31.41) ("return" 5.92))

Solution

  • You can just about do what you want by wrapping up your table in its own code block. The slight disadvantage is the extra boilerplate and the extra line in the comment:

    #+name: test-data-block
    #+BEGIN_SRC org
    #+name: test-data-table
    | type   | amount |
    |--------+--------|
    | sale   |  31.41 |
    | return |   5.92 |
    #+END_SRC
    
    #+BEGIN_SRC python :var data=test-data-table :tangle test.py :colnames no :noweb yes
      ## Table
      ## <<test-data-block>>
    
      [zip(data[0], row) for row in data[1:]]
    #+END_SRC
    

    And the tangled output is:

    data=[["type", "amount"], ["sale", 31.41], ["return", 5.92]]
    ## Table
    ## #+name: test-data-table
    ## | type   | amount |
    ## |--------+--------|
    ## | sale   |  31.41 |
    ## | return |   5.92 |
    
    [zip(data[0], row) for row in data[1:]]