0

Consider an org-mode file like so:

#+NAME: foobar
|     | Alice | Bob | Charlie |
| Foo |    11 |  12 |      13 |
| Bar |    21 |  22 |      23 |
| Baz |    31 |  32 |      33 |

* Alice
- Works at Airbus

** Stats
- Magic here!

* Bob
- Works at Boeing

** Stats
- Magic here!

* Charlie
- Works at Cirrus

** Stats
- Magic here!

For each Magic here! placeholder, I would like to get the relevant information out of the table. I don't mind passing in the column number as a variable, but the output should be refreshed by a nice function. For instance, for Bob I would like the information to look as so:

* Bob
- Works at Boeing

** Stats
- Foo :: 12
- Bar :: 22
- Baz :: 32

I don't even know where to start. What docs should I be reading? Is org-mode even capable of this?

1 Answer 1

1

You can use a source block in the language of your choice to get the data from the table and format it appropriately. You can then use an inline call at the the three "magic" places that when executed will insert the results in the file.

I wrote the source block in python and found it convenient to transpose the original table. Here's a sample org file with the inline calls in place and after having run the first one with C-c C-c:

#+NAME: foobar
|         | Foo | Bar | Baz |
| Alice   |  11 |  21 |  31 |
| Bob     |  12 |  22 |  32 |
| Charlie |  13 |  23 |  33 |

#+name: stats
#+begin_src python :results output raw :var row=1 :var table=foobar 
  for els in list(zip(table[0], table[row]))[1:]:
      print("- %s :: %s" % (els[0], els[1]))
#+end_src

* Alice
- Works at Airbus

** Stats
#+call: stats(row=1, table=foobar)

#+RESULTS:
- Foo :: 11
- Bar :: 21
- Baz :: 31

* Bob
- Works at Boeing

** Stats
 #+call: stats(row=2, table=foobar)

* Charlie
- Works at Cirrus

** Stats
 #+call: stats(row=3, table=foobar)

Evaluate the other two calls with C-c C-c on each one to get the results you want.

You can use a language other than python of course, and, if you want, you can keep the original table at the price of a more complicated program (which is left as an exercise for the interested reader...)

2
  • Perfect, thank you! I'm proficient enough in Python to work around the table axes. Note to the next guy who needs this: I needed to add (org-babel-do-load-languages 'org-babel-load-languages '( (python . t) )) to the Emacs config file, and must invoke org-reload before Ctrl-C Ctrl-C will work. Oh, and I had to add a symlink from python to python3 as current *buntus do not come with a python command.
    – dotancohen
    Sep 22, 2023 at 20:56
  • 1
    Better solution than the python symlink. Just add :python python3 to the begin line to specify the Python interpreter. Documented here.
    – dotancohen
    Sep 28, 2023 at 0:47

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.