3

I have some org files that I generated from a program I wrote. My program is not intelligent enough to parse the tables in these files "correctly". For example, one of my org files may look like

#+Title: hello world * section 1 here is an unaligned table |this |is | my | | first| unaligned| table | * section 2 here are two more unaligned tables | this | is |my | |second| unaligned | table| |yet|another| unaligned| |table| to |alter| 

I'd like to be able to automatically align all the tables in these files from the command line. Thus running such a command on my above file would yield

#+Title: hello world * section 1 here is an unaligned table | this | is | my | | first | unaligned | table | * section 2 here are two more unaligned tables | this | is | my | | second | unaligned | table | | yet | another | unaligned | | table | to | alter | 

Does such a command exist?

I'm aware that setting org-startup-align-all-tables to t will automatically align every table upon visiting the file but I'd like to align the tables without actually opening the file.

0

3 Answers 3

2

It seems that there is no command to do what you want, but in emacs we can have our way

I have wrote a small shell script based on the tangle example in the emacs manual

#!/bin/sh # -*- mode: shell-script -*- # # tangle files with org-mode # DIR=`pwd` FILES="" # wrap each argument in the code required to call tangle on it for i in $@; do FILES="$FILES \"$i\"" done emacs -Q --batch \ --eval "(progn (require 'org)(require 'org-table) (mapc (lambda (file) (find-file (expand-file-name file \"$DIR\")) (org-table-map-tables 'org-table-align) (write-file file nil) (kill-buffer)) '($FILES)))" 

Be aware that the script overwrites the original file

6
  • How does that align tables as op wants? Commented Sep 14, 2015 at 14:43
  • To my understanding OP has the files with unaligned tables, and wants to align them as Org would do when opening a file. I tested the script and it did that PLease pay attention to the "org-table-map" line Commented Sep 14, 2015 at 14:46
  • @Joafigue I wonder if this script could be altered to also update all the formulas in the tables... Would that be an easy alteration or should I ask a separate question? Commented Sep 15, 2015 at 6:14
  • Ahh.. figured it out. Replacing 'org-table-align with 'org-table-iterate does the trick. Commented Sep 15, 2015 at 6:54
  • @Joafigue How would I apply this script to all the org files in a directory? Commented Sep 17, 2015 at 6:53
3

Does such a command exist?

No, but you can easily mimic in a variety of ways. The easiest approach seems an adaptation of org-mode's built-in column alignment overrides <10> at the top of each column. For example:

| <8> | <11> | <12> | | this | is | my | | second | unaligned | table | 

your second table

| <8> | <11> | <12> | | yet | another | unaligned | | table | to | alter | 

where <8>, <11> and <12> are arbitrary column widths that you specify for each column of each table, overriding the automatic alignment. This will have the effect of aligning all the columns and the tables.

I can confirm I've used this feature extensively while dumping to html.

Addendum:

To print in orgmode, you must invoke the export to text feature:

C-c C-e t a 

There are other options to save to file, save to a new buffer, etc. If you want to export from the command line, use one like this:

emacs foo.org --batch -f org-ascii-export-to-ascii --kill 

The manual has more details about batch execution.

2
  • This doesn't seem to to accomplish what I'd like. Issuing cat foo.org on my file still yields an unaligned table. Commented Sep 14, 2015 at 2:25
  • cat won't work, see the updated addendum in the answer. Commented Sep 14, 2015 at 3:44
1

In an org file I have several named code blocks, some of them generate (#+RESULTS) named tables. Whenever I want to regenerate the tables in the file, I run org-babel-execute-buffer, and then I end up in a situation pretty much similar to yours, that is with unaligned tabled.

Well, I should say I "would" end up in such situation, but in fact the latest code block in the file does the alignment for me:

#+BEGIN_SRC emacs-lisp :results output silent (org-table-map-tables 'org-table-align) #+END_SRC 

This block applies in fact what is used in the selected answer. By including this block at the end, after generating all the tables they are automatically re-aligned.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.