0

I have a snippet of Python code that outputs an Org mode formatted table. After I'm satisfied with the table, I would like to generate the LaTeX markup via orgtbl-to-latex function.

For example, I have a source block similar to the following:

#+begin_src python :output table :results output drawer
print('\n'.join(('| factor | mean diff | t-value | p-value |',
                  '|--------+-----------+---------+---------|',
                  ....))
#+end_src

Which generates the following, elided, output:

#+RESULTS:
:results:
| factor            | mean diff |  t-value | p-value |
|-------------------+-----------+----------+---------|
| A                 |    -21.08 | -2.14404 | 0.03867 |
:end:

Now, I would prefer to name the output table such that I can pass it to an elisp block as such:

#+begin_src elisp :var tab=ttest :results raw drawer replace :colnames no
(orgtbl-to-latex tab nil)
#+end_src

I have not had luck using #+NAME at the top of the generating source block, and I'm not sure what else to try. For now, I copy the table and name it so the orgtbl-to-latex function operates on the copy. Similarly, I can change the python code to generate the LaTeX markup, but this is painful since it is easier to read the org table first to ensure I have the data formatted how I want before translating to LaTeX. More, moreover, I cannot author directly in Org mode since collaboration would be near impossible to deal with, so that's out as well.

This looks like the solution, but it's not using drawers. Is that my problem? Or is there a way to achieve this with drawers.

1 Answer 1

1

The link you point to is only tangentially related to your problem: since you use :results output in your python block, you don't have to worry about the inconcistency of ob-python with :results value unless you change your code block to use that.

Note also that you have a typo in your elisp code block that I correct below: the syntax is :var tab=ttest with an = sign, not a colon.

Drawers are certainly implicated in your problem: if you write a table like this in your Org mode file:

#+name: ttest
:results:
| factor | mean diff |   t-value | p-value |
|--------+-----------+-----------+---------|
| A      |    -21.08 | -2.144404 | 0.03867 |
:end:

and you try to pass the table to your elisp code block, you will get an error, saying the reference is not found: Org mode cannot resolve the ttest name because you cannot attach a #+name to a drawer.

Here is one way to do what you want:

#+name: bar
#+begin_src python :results output raw
print('\n'.join(('| factor | mean diff | t-value | p-value |',
                  '|--------+-----------+---------+---------|',
                  '|A | -21.08 | -2.144404 | 0.03867|')))
#+end_src

#+name: ttest3
#+RESULTS: bar
| factor | mean diff |   t-value | p-value |
|--------+-----------+-----------+---------|
| A      |    -21.08 | -2.144404 | 0.03867 |

#+name: to-latex
#+begin_src elisp :var tab=ttest3 :results raw :wrap export latex :colnames no
(orgtbl-to-latex tab nil)
#+end_src

#+RESULTS: to-latex
#+begin_export latex
\begin{tabular}{lrrr}
factor & mean diff & t-value & p-value\\
A & -21.08 & -2.144404 & 0.03867\\
\end{tabular}
#+end_export

2
  • Typo existed in the question, not the source. But thank you, I fixed the question to remove it. Your solution works, I've removed the drawers, and I'm able to generate the \LaTeX table without copying or too much fiddling. I was hoping I could get the result named automatically, but that's a minor issue.
    – kballou
    Commented Jan 3, 2022 at 22:02
  • 1
    Nothing stopping you: add another print to the code block: print("#+name: ttest3") and get rid of the one I added manually- Org mode does not care as long as the name is "adjacent" to the table i.e. with nothing other than "affiliated keywords" separating it from the table (no empty lines, drawer markers, miscellaneous text): e.g. you can add things like #+ATTR_HTML: .... between the #+NAME: and the table
    – NickD
    Commented Jan 3, 2022 at 22:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.