0

I have the following code:

#+BEGIN_SRC emacs-lisp :results value table
  (append '(("a" "b" "c" "d")
        hline
        (1 2 3 4)
        (5 6 7 8)))
#+END_SRC

When I run this source block, that gives me the expected result

#+RESULTS:
| a | b | c | d |
|---+---+---+---|
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |

What I want is to add a #+NAME: to the table so that I can reference it later, like this:

#+RESULTS:
#+NAME: my-table
| a | b | c | d |
|---+---+---+---|
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |

I have seen the example in the org mode manual in Section 15.6 that uses postprocessing, but that example uses sh for the processor and for various reasons I want to stick to emacs-lisp. I have tried various things but I can't get the postprocessing to work. The main problem seems to be that the postprocessing block has to be :results output, not :results value, but it is passed the table as a Lisp object, which I would have to format again as a string to prepend the #+NAME: string.

How can I get a #+NAME: added to my table using only emacs lisp?

1 Answer 1

0

The usual trick is to add the #+NAME: before the results, but for that to work well, you should also name the code block (which in turn will have the effect of labeling the results block, so babel can find it and replace it, rather than producing more results blocks):

#+NAME: my-table-producer
#+BEGIN_SRC emacs-lisp :results value table
  (append '(("a" "b" "c" "d")
        hline
        (1 2 3 4)
        (5 6 7 8)))
#+END_SRC

#+NAME: my-table
#+RESULTS: my-table-producer
| a | b | c | d |
|---+---+---+---|
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |


#+begin_src python :var tbl=my-table :results output
print(tbl)
#+end_src

#+RESULTS:
: [[1, 2, 3, 4], [5, 6, 7, 8]]

As you can see, the #+NAME: before the #+RESULTS: (with nothing in between) is effective: the python code block was able to find the table and assign it to tbl.

6
  • OMG, I never thought I could add a #+NAME: BEFORE the result block. Thanks, that solves my problem completely. Feb 23 at 18:57
  • However, if you don't name the code block and put the table name before the named #+RESULTS: block, you will be cursing me for ever :-) Or at least every time you evaluate the code block. So make sure to do that as well.
    – NickD
    Feb 23 at 19:00
  • Yeah, I heeded your advice and it works like a charm in my production code. Also, I think I've already exceeded my curse quota for today. Feb 23 at 19:02
  • I'm just going through the manual again and I can't find the option to name result blocks anywhere. Is that documented and I've just missed it? Feb 23 at 19:12
  • See the first code block in my answer. It is done the same way as with a table: add #+NAME: foo above the code block. You can find the description for that particular use in the manual, but #+NAME is one of the so-called affiliated keywords, i.e. keywords that you can attach to other things. You can find a description in the Affiliated Keywords subsection of the Keywords section in the Syntax document on Worg.
    – NickD
    Feb 23 at 22:15

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.