1

Let suppose our program output many formatted tables.

For instance :

#+Name: Scores
#+BEGIN_SRC python :results output raw

from random import randint
li ='''Max Charlie Cooper Buddy Jack Rocky Oliver Bear Duke Tucker'''.split()
print(  "|{0:8s}| {1:3s} | {2:3s} | {3:3s} |".format(
      "Name","str","agi","int"
))

for f in li:
  print("|{0:8s}| {1:3d} | {2:3d} | {3:3d} |".format(
     *( [f]+[randint(0,20) for x in range(3)])
  ))

#+END_SRC   

#+Call: Scores()

The evaluation of the code block will produce an output similar to this

#+RESULTS:
| Name    | str | agi | int |
| Max     |  17 |  15 |  16 |
| Charlie |   2 |   5 |  19 |
| Cooper  |   3 |   5 |   5 |
| Buddy   |   9 |   6 |   9 |
| Jack    |  11 |  14 |  19 |
| Rocky   |  19 |   1 |   2 |
| Oliver  |   5 |  17 |   5 |
| Bear    |   0 |  17 |  17 |
| Duke    |  16 |   8 |  19 |
| Tucker  |  14 |  13 |   9 |

When exporting, the #+CALL is evaluated, and its results gets exported.

Question: how to automate the insertion of something like

#+ATTR_LaTeX: :center nil :align |p{5cm}|l|l|l|

on top of the table in the results section ?

Any proposal is welcome, be it by outputting by value, hooking the export, creating a latex header... or indicating any appropriate org function.

2

A :prologue header is appended to the body of the block before execution:

#+Name: Scores
#+header: :prologue print('#+ATTR_LaTeX: :center nil :align |p{5cm}|l|l|l|')
#+BEGIN_SRC python :results output raw :exports results :eval yes

from random import randint
li ='''Max Charlie Cooper Buddy Jack Rocky Oliver Bear Duke Tucker'''.split()
print(  "|{0:8s}| {1:3s} | {2:3s} | {3:3s} |".format(
      "Name","str","agi","int"
))

for f in li:
  print("|{0:8s}| {1:3d} | {2:3d} | {3:3d} |".format(
     *( [f]+[randint(0,20) for x in range(3)])
  ))

#+END_SRC

#+results: Scores
#+ATTR_LaTeX: :center nil :align |p{5cm}|l|l|l|
|Name    | str | agi | int |
|Max     |   9 |  13 |  17 |
|Charlie |  12 |   7 |  10 |
|Cooper  |  16 |  19 |   7 |
|Buddy   |  16 |   2 |  13 |
|Jack    |  17 |  10 |  20 |
|Rocky   |   0 |  19 |  13 |
|Oliver  |  17 |   2 |   6 |
|Bear    |  19 |  20 |  17 |
|Duke    |   8 |  19 |   9 |
|Tucker  |   1 |  10 |   7 |
  • This is a little cleaner than the :post solution. – John Kitchin Aug 4 '18 at 18:38
2

You can do this with the :post header and a named block that adds that. https://orgmode.org/manual/post.html#post

Here is an example modified from that page.

#+name: attr_wrap
#+begin_src sh :var data=""  :results output
  echo "#+ATTR_LATEX: :center nil :align |p{5cm}|l|l|l|"
  echo "$data"
#+end_src


#+Name: Scores
#+BEGIN_SRC python :results output raw :post attr_wrap(data=*this*) 

from random import randint
li ='''Max Charlie Cooper Buddy Jack Rocky Oliver Bear Duke Tucker'''.split()
print(  "|{0:8s}| {1:3s} | {2:3s} | {3:3s} |".format(
      "Name","str","agi","int"
))

for f in li:
  print("|{0:8s}| {1:3d} | {2:3d} | {3:3d} |".format(
     *( [f]+[randint(0,20) for x in range(3)])
  ))

#+END_SRC

#+RESULTS: Scores
#+ATTR_LATEX: :center nil :align |p{5cm}|l|l|l|
| Name    | str | agi | int |
| Max     |  12 |   4 |  10 |
| Charlie |  15 |  10 |  10 |
| Cooper  |  17 |  13 |  20 |
| Buddy   |   6 |  15 |  11 |
| Jack    |   8 |  15 |  16 |
| Rocky   |   1 |  16 |   6 |
| Oliver  |   9 |  11 |  14 |
| Bear    |  10 |   6 |   3 |
| Duke    |   2 |  19 |   1 |
| Tucker  |   8 |   0 |   2 |
  • This opens a new range of possibilities – Flint Aug 4 '18 at 19:56

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.