5

I've this table:

|H1    |H2    |H3    |
|------+------+------|
|text11|text12|text13|
|text21|text22|text23|
|...   |...   |...   |
|textN1|textN2|textN3|
|--------------------|

and i want this list:

* text11
- H1: text11
- H2: text12
- H3: text13

* text21
- H1: text21
- H2: text22
- H3: text23

...

* textN1
- H1: textN1
- H2: textN2
- H3: textN3

How do I do that?

1
  • Oh this is super cool to find because I am a huge believer in having the plaintext representation of tabular data be a list of lists instead of ASCII art that looks like a table, since the latter is very painful to edit by hand and is less possible (and more complex when possible) to reflow visually to the screen size. So I would actually love an Org-mode addon where my manual editing and my saved .org files have tables written almost how you want the output in this question to look. And this makes me realize it might be possible for me to just write a bit of code to do that.
    – mtraceur
    Jun 1, 2023 at 17:00

1 Answer 1

7

EDIT: Updated code because I had misunderstood what the headings should be, and to switch to s-join because apparently @lap's version of Emacs doesn't have string-join from subr-x (s-join and string-join take their arguments in opposite order!).

EDIT: Last fix: OP is using an older version of Org Mode and the parsed table includes the symbol hline to indicate the position of horizontal lines, so we remq them away.

You can name the table and then use a :var declaration in a source block and Org Mode will parse the table for you!

#+NAME: laps-table
|H1    |H2    |H3    |
|------+------+------|
|text11|text12|text13|
|text21|text22|text23|
|...   |...   |...   |
|textN1|textN2|textN3|
|--------------------|

#+BEGIN_SRC emacs-lisp :var tbl=laps-table
  (require 's)
  (let ((headings (car tbl)))
    (s-join
     "\n"
     (mapcar
      (lambda (row)
        (concat
         (format "* %s\n- " (car row))
         (s-join
          "\n- "
          (cl-map 'list 
                  (lambda (hd x) (concat hd ": " x))
                  headings row))))
      (remq 'hline (cdr tbl)))))
#+END_SRC

Running the source block results in the following:

#+RESULTS:
#+begin_example
* text11
- H1: text11
- H2: text12
- H3: text13
* text21
- H1: text21
- H2: text22
- H3: text23
* ...
- H1: ...
- H2: ...
- H3: ...
* textN1
- H1: textN1
- H2: textN2
- H3: textN3
#+end_example
8
  • I've this error with your code: Symbol's function definition is void: string-join
    – lap
    Sep 26, 2016 at 12:10
  • ...not... text1, text2, etc... i want to insert text11, text21, etc. IN MY table... thanks!
    – lap
    Sep 26, 2016 at 12:12
  • string-join is relatively new in Emacs, if you are using an old version of Emacs you can install a library called s and replace string-join with s-join.
    – Omar
    Sep 26, 2016 at 16:30
  • I loaded the library s.el and replaced string-join with s-join but running the code still get errors (wrong type argument: sequencep, hline)
    – lap
    Sep 26, 2016 at 17:11
  • What version of Emacs and what version of Org Mode are you using, @lap?
    – Omar
    Sep 26, 2016 at 17:56

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.