emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Christian Moe <mail@christianmoe.com>
To: Matt Price <moptop99@gmail.com>
Cc: tbanelwebmin <tbanelwebmin@free.fr>, Org Mode <emacs-orgmode@gnu.org>
Subject: Re: convert subtree or nested list to table
Date: Thu, 08 Jul 2021 14:22:25 +0200	[thread overview]
Message-ID: <87v95l2dxa.fsf@christianmoe.com> (raw)
In-Reply-To: <CAN_Dec_OhmtoV+Ej3Ld7UTx+R5EiDfeEgYJ4Z7ExT1H5=0V=ZQ@mail.gmail.com>


Hi, Matt,

Here's a version of this with a bit more processing.

Define this somewhere in your document

#+NAME: list2table
#+BEGIN_SRC elisp :var order="columns"
  (let (longest)
    (setq data (map 'list 'flatten data))
    (setq data (map 'list (lambda (x) (seq-difference x '(unordered ordered))) data))
    ;; Pad out lists to equal length
    (setq longest (seq-max (map 'list 'length data)))
    (setq data
	  (map 'list
	       (lambda (l)
		 (append l (make-list (- longest (length l)) "")))
		 data))
    ;; Order by columns or rows
    (if (string= order "columns")
	(apply #'mapcar* #'list data) ; transpose
      data))
#+END_SRC

Here is an example list to try it out with:

#+NAME: testlist
- Letters
  1. a
  2. b
  3. c
- Roman numerals
  1. i
  2. ii
  3. iii
- Greek letters
  1. alpha
  2. beta
  3. gamma

Now you can call the src block, passing the name of the list to the
"data" variable.

#+CALL: list2table(data=testlist)

#+RESULTS:
| Letters | Roman numerals | Greek letters |
| a       | i              | alpha         |
| b       | ii             | beta          |
| c       | iii            | gamma         |

The default here is that each top item and its sublist forms a column.

To get rows instead, pass order="rows" (or anything other than
order="columns" really):

#+CALL: list2table(data=testlist, order="rows")

#+RESULTS:
| Letters        | a     | b    | c     |
| Roman numerals | i     | ii   | iii   |
| Greek letters  | alpha | beta | gamma |

You can use numbered or unnumbered lists. Sublists don't strictly have
to be the same length - the code pads them out to equal length with the
empty string before transposing. However, I would strongly recommend
using numbered sublists of the same length (with blank items as needed),
so you can make sure that items line up correctly.

If you want column headers or rownames, you will need to take care of
that manually before exporting. Using ":colnames yes" will lead to
errors when the source is a list. Might be away to hack org-babel to get
around this but I don't know how. (The only automatic solution I can
think of would be by naming the calls in an unexported section and
referencing them with another layer of calls in the exported section,
using a src block that only passes the data on with :colnames yes. But
that's fiddly.)

Will this work for you?

Yours,
Christian


Matt Price writes:

> I think this is exactly what I want (with just a little moreprocessing).
> Thank you so much for the idea!
>
> I'm having a little bit of trouble getting the same output as you though,
> and I'm wondering if there might be a setting that I need to change.
>
> Here is what I tried, and the result. Do you have an idea of what is going
> wrong here?
>
> Thank you!
>
>
> ------------
> #+NAME:essay-rubric
> - Category
>   - A
>   - B
>   - C
>   - D
>   - F
> - Writing
>   - great
>   - good
>   - ok
>   - lousy
>   - awful
>
> #+begin_src emacs-lisp :var contents=essay-rubric :results table
> contents
> #+end_src
>
> #+RESULTS:
> #+begin_src emacs-lisp
> | (("Category" |
> #+end_src
> -------------
> On Wed, Jul 7, 2021 at 6:29 AM tbanelwebmin <tbanelwebmin@free.fr> wrote:
>
>> Hi Matt
>>
>> Le 05/07/2021 à 21:44, Matt Price a écrit :
>> > I have to write a number of text-heavy documents which need to be
>> > delivered as tables with wrapped paragraphs in most cells. Working
>> > directly in table format is pretty arduous and uncomfortable.  Has
>> > anyone ever written a function to accept a list or subtree as input
>> > and process it into a table?
>> >
>> > If anyone has done something similar, I'd love some tips!
>>
>> Maybe you could use builtin Babel
>> Hereafter you have a starting point
>> - Give a name to your input Org list
>> - Process it with Emacs-Lisp (or whatever language you are comfortable
>> with) to output it as a table
>>
>>
>> ____ self contained Org Mode example _____
>>
>> Example of a named list
>> #+NAME: BBB
>> - abc
>>   + 123
>>   + 456
>> - def
>>   + red
>>   + blue
>> - ghi
>>   + big
>>   + small
>>
>> Example of converting the named list into a table with Emacs-Lisp
>> #+begin_src elisp :var bbb=BBB :results table
>> bbb
>> #+end_src
>>
>> #+RESULTS:
>> | abc | (unordered (123) (456))   |
>> | def | (unordered (red) (blue))  |
>> | ghi | (unordered (big) (small)) |
>> ___________________________________________
>>
>>
>>


  parent reply	other threads:[~2021-07-08 12:28 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-05 19:44 convert subtree or nested list to table Matt Price
2021-07-05 20:01 ` Juan Manuel Macías
     [not found]   ` <CAN_Dec8iqKS+3qvjkYvQxovegnEzqR_rra0Q-ZA9baPz1mXDAA@mail.gmail.com>
2021-07-06 12:56     ` Juan Manuel Macías
2021-07-07  6:18       ` Uwe Brauer
2021-07-07 18:27         ` Juan Manuel Macías
2021-07-07 18:44           ` Uwe Brauer
2021-07-07 21:16       ` Matt Price
2021-07-05 22:10 ` Tim Cross
2021-07-06  3:15   ` Matt Price
2021-07-06  5:10     ` Tim Cross
2021-07-06 11:00       ` Eric S Fraga
2021-07-06 11:49         ` Tim Cross
2021-07-06 11:50 ` Uwe Brauer
2021-07-07 21:17   ` Matt Price
2021-07-07 10:27 ` tbanelwebmin
2021-07-07 21:13   ` Matt Price
2021-07-08  9:27     ` tbanelwebmin
2021-07-08 12:22     ` Christian Moe [this message]
2021-07-20 15:11       ` Matt Price
2021-07-20 16:03         ` Nick Dokos
2021-07-20 17:03           ` Matt Price
2021-07-10 12:03 ` [a very different solution] (was: convert subtree or nested list to table) Uwe Brauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87v95l2dxa.fsf@christianmoe.com \
    --to=mail@christianmoe.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=moptop99@gmail.com \
    --cc=tbanelwebmin@free.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).