Reputation: 125
I'm creating various tables in org mode using R and want to include them in an export to PDF. Some of these tables are too wide for a standard page, and some may be too long. I've figured out how to rotate an org table, but I really need to rotate a table that's the output of a code block.
I've looked through the Org manual, https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-R.html, the questions SO suggested, https://raw.githubusercontent.com/dfeich/org-babel-examples/, and other places. Either these didn't offer a solution or I didn't understand it.
Borrowing from https://github.com/dfeich/org-babel-examples/tree/master/latex, i appended these lines to the front end of my org file:
#+latex_compiler: pdflatex
#+LATEX_HEADER_EXTRA: \usepackage{rotfloat}
#+LATEX_HEADER_EXTRA: \usepackage{lmodern}
#+LATEX_HEADER: \hypersetup{colorlinks=true, linkcolor=blue}
#+LATEX_HEADER_EXTRA: \usepackage{tabularx}
#+LATEX_HEADER_EXTRA: \usepackage{tabu,longtable}
#+LATEX_HEADER_EXTRA: \usepackage{booktabs}
and created a table like this:
#+NAME: widetbltest
#+CAPTION: Sideways Table Test
#+ATTR_LATEX: :float sideways :placement [H]
#+HEADER: :exports results :results table latex :colnames yes
#+begin_src R
y <- data.frame(
a = seq(1,20,by = 1),
b = seq(1,20,by = 1),
c = seq(1,20,by = 1),
d = seq(1,20,by = 1),
e = seq(1,20,by = 1),
f = seq(1,20,by = 1),
g = seq(1,20,by = 1),
h = seq(1,20,by = 1),
i = seq(1,20,by = 1),
j = seq(1,20,by = 1),
k = seq(1,20,by = 1),
l = seq(1,20,by = 1),
m = seq(1,20,by = 1),
n = seq(1,20,by = 1),
o = seq(1,20,by = 1),
p = seq(1,20,by = 1),
q = seq(1,20,by = 1),
r = seq(1,20,by = 1),
s = seq(1,20,by = 1)
)
y
#+end_src
I expected a formatted table that was rotated 90 degrees. I got a slightly ragged unrotated table seemingly made with misaligned pipe symbols separating columns.
I'm running Emacs 26.1 and org 9.2.4.
Ideas? If it would help and is allowed, I could post the 85-line full document.
If the solution extends naturally to rendering long tables across pages nicely, that would be great.
UPDATE: @lars
Here is what I get in the PDF:
Upvotes: 6
Views: 3106
Reputation: 6402
There is one basic problem with what you wrote: the #+CAPTION:
and #+ATTR_LATEX:
directives have to apply to the table that is produced by the code block, not to the code block itself. The best way I have found to do this is to give a name to the code block, so that the results are produced under that name, and then attach the directives to the results:
#+NAME: widetbltest
#+HEADER: :exports results :results table :colnames yes
#+begin_src R
....
#+end_src
#+CAPTION: Sideways Table Test
#+ATTR_LATEX: :float sideways
#+RESULTS: widetbltest
Now when you do C-c C-c
on the code block (or when the exporter executes the code block), the results are going to be placed, reliably and reproducibly, under the #+RESULTS: widetbltest
directive, which is what the #+CAPTION:
and #+ATTR_LATEX:
directives are attached to.
Another problem is the header of your code block. You want the results as a table, but you don't want to specify latex
there: you'd only use that if your code block produced a latex table, not an Org mode table.
I also got rid of the :placement
directive, because in my experiments I got an error saying that the sidewaystable
environment does not support it. Maybe that changes with rotfloat
or tabu
but
I do not have either of those packages installed. I also got rid of longtable
because it is already included in the defaults (at least my defaults).
Here's a final version that works properly for me:
#+LATEX_HEADER_EXTRA: \usepackage{lmodern}
#+LATEX_HEADER_EXTRA: \usepackage{tabularx}
#+LATEX_HEADER_EXTRA: \usepackage{booktabs}
#+LATEX_HEADER: \hypersetup{colorlinks=true, linkcolor=blue}
* Wide table
#+NAME: widetbltest
#+HEADER: :exports results :results table :colnames yes
#+begin_src R
y <- data.frame(
a = seq(1,20,by = 1),
b = seq(1,20,by = 1),
c = seq(1,20,by = 1),
d = seq(1,20,by = 1),
e = seq(1,20,by = 1),
f = seq(1,20,by = 1),
g = seq(1,20,by = 1),
h = seq(1,20,by = 1),
i = seq(1,20,by = 1),
j = seq(1,20,by = 1),
k = seq(1,20,by = 1),
l = seq(1,20,by = 1),
m = seq(1,20,by = 1),
n = seq(1,20,by = 1),
o = seq(1,20,by = 1),
p = seq(1,20,by = 1),
q = seq(1,20,by = 1),
r = seq(1,20,by = 1),
s = seq(1,20,by = 1),
t = seq(1,20,by = 1),
u = seq(1,20,by = 1),
v = seq(1,20,by = 1),
w = seq(1,20,by = 1),
x = seq(1,20,by = 1),
y = seq(1,20,by = 1),
z = seq(1,20,by = 1)
)
y
#+end_src
#+CAPTION: Sideways Table Test
#+ATTR_LATEX: :float sideways
#+RESULTS: widetbltest
Doing C-c C-e l o
produces a PDF with a sideways table on p.2.
BTW, one indispensable debugging technique in these cases is to export to a latex file with C-c C-e l l
and then examine the latex file, compile it with your compiler of choice (pdflatex
seems to be your choice), and see where and how it fails. That often provides insight on what you have to do to your Org mode file to fix whatever problems you are running into. One corollary of that is that you have to know some LaTeX: some people expect to write Org mode files and never have to look at LaTeX, but that only works in the simplest cases: as soon as you go off the beaten path even a little bit, having some LaTeX knowledge is enormously helpful.
Upvotes: 3