[Tex/LaTex] Two tables (not hard-coded) side-by-side in LaTeX that will page break

knitrpage-breakingtables

I wrote my original question here: (Two tables side-by-side in LaTeX that will page break). Then, I realized I needed to revise my question, by also including an additional problem that comes with it. I cannot separate them (as I did in the original query), otherwise the solutions offered to me will not really solve my specific set of problems.

For the most detail, see the original post listed above. However, I will state the basic and full problem here again (as well as the additional problem that needs to be stated together):

I created a code.Rnw file in Rstudio. I then knit it using:

knit("code.Rnw","code.tex")

I am trying to get two tables side-by-side. In case they are long, I would like them to automatically page break. I have tentative syntax that seems to work (when using the \quad command) as shown below:

\documentclass[12pt,english,nohyper]{tufte-handout}
\usepackage{longtable}

\begin{document}

<<echo=FALSE,results='asis'>>=
library(xtable)
@

\begin{tabular}{cc}
  \hline
 & vals \\ 
  \hline
a & 3.39 \\ 
  b & 4.35 \\ 
  c & 6.16 \\ 
  d & 9.17 \\ 
  e & 2.82 \\ 
   \hline
\end{tabular}
\quad % separates first and second table
\begin{tabular}{cc}
  \hline
 & vals \\ 
  \hline
a & 3.39 \\ 
  b & 4.35 \\ 
  c & 6.16 \\ 
  d & 9.17 \\ 
  e & 2.82 \\  
   \hline
\end{tabular}

\end{document}

This creates the desired output as follows:

Desired output

However, I am running across a problem when the tables become long. In the example above, there were only 5 rows per table. However, if I increase the number of rows so that each table can no longer fit on a single page, then rather than automatically page-break, it will simply run off the page. It looks something like this (there are more rows, y2, z2, etc, but these simply run off the page):

Output runs off page

I tried to remedy this problem by using the "longtable" option (instead of the "tabular" option), as this is supposed to help with page-breaks:

\documentclass[12pt,english,nohyper]{tufte-handout}
\usepackage{longtable}

\begin{document}

<<echo=FALSE,results='asis'>>=
library(xtable)
@

\begin{longtable}{cc} % CHANGED FROM "TABULAR" TO "LONGTABLE"
  \hline
 & vals \\ 
  \hline
a & 3.39 \\ 
  b & 4.35 \\ 
  c & 6.16 \\ 
  d & 9.17 \\ 
  e & 2.82 \\ 
   \hline
\end{longtable} % CHANGED FROM "TABULAR" TO "LONGTABLE"
\quad % separates first and second table
\begin{longtable}{cc} % CHANGED FROM "TABULAR" TO "LONGTABLE"
  \hline
 & vals \\ 
  \hline
a & 3.39 \\ 
  b & 4.35 \\ 
  c & 6.16 \\ 
  d & 9.17 \\ 
  e & 2.82 \\  
   \hline
\end{longtable} % CHANGED FROM "TABULAR" TO "LONGTABLE"

\end{document}

Even though it did solve the page-break issue (prevented text from falling off the page) in long table, it also made the tables no longer side by side (instead, placing them top to bottom). This was the undesired output:

Now tables are undesirably top-to-bottom

The other problem (that I did not mention in my original post) is that I cannot hardcode this table (as I did in my tentative solution posted at the very beginning of this post), because these two tables will need to be automatically generated from a given data frame. My general idea would be to cut the data frame in half and then feed each into some table function that is separated by something like \quad. It would look something like this (Here I am not cutting the data frame in half, but simply repeating the same data frame twice):

\documentclass[12pt,english,nohyper]{tufte-handout}
\usepackage{longtable}

\begin{document}

<<echo=FALSE,results='asis'>>=
library(xtable)
set.seed(1)
myDF = data.frame(vals=runif(5, 1, 10))
row.names(myDF) = letters[1:5]
@

\print(xtable(myDF),floating=FALSE,tabular.environment = "tabular")
\quad
\print(xtable(myDF),floating=FALSE,tabular.environment = "tabular")

\end{document}

Ideally, when I knit this code.Rnw file, then I will successfully get a code.tex file that can be converted to a code.pdf file that contains the two tables side-by-side and page-breakable. However, my current syntax above just does not work.

Even though (print(xtable(myDF),floating=FALSE,tabular.environment = "tabular")) will output the correct tabular structure, I cannot insert \quad between that \print(xtable) syntax and have it knit correctly. Any advice is greatly appreciated!

EDIT:

I appreciate the help! I took into account the resource offered by @DavidCarlisle (Balancing long table inside multicol in LaTeX). Then @touhami asked me to clarify what I still cannot solve. What I cannot solve is how to put the dataframe into this new syntax, so that the .Rnw file can be knitted. Here is an example of what I tried (by tailoring syntax from the resource offered by @DavidCarlisle):

\documentclass[11pt, a4paper]{article}
\usepackage[margin=3cm]{geometry}
\usepackage{longtable}
\usepackage{multicol}

\newsavebox\ltmcbox

\def\tabline{Student & Correct Percent\\}
\def\tablines{\tabline\tabline\tabline}

\begin{document}

<<echo=FALSE,results='asis'>>=
library(xtable)
set.seed(1)
myDF = data.frame(vals=runif(10, 1, 10))
row.names(myDF) = letters[1:10]
@

\begin{multicols}{2}

\setbox\ltmcbox\vbox{
\makeatletter\col@number\@ne
\begin{longtable}{|l|l|}
myDF
\end{longtable}
\unskip
\unpenalty
\unpenalty}
\unvbox\ltmcbox
\end{multicols}
\end{document}

When I knit this new .Rnw code, this is my ouptput:

Undesirable output for new tailored script

I also tried other unsuccessful approaches as the following:

\documentclass[11pt, a4paper]{article}
\usepackage[margin=3cm]{geometry}
\usepackage{longtable}
\usepackage{multicol}

\newsavebox\ltmcbox

\def\tabline{Student & Correct Percent\\}
\def\tablines{\tabline\tabline\tabline}

\begin{document}

<<echo=FALSE,results='asis'>>=
library(xtable)
set.seed(1)
myDF = data.frame(vals=runif(10, 1, 10))
row.names(myDF) = letters[1:10]

cat(sprintf('\\begin{multicols}{2}
\\setbox\ltmcbox\vbox{
\\makeatletter\col@number\@ne
\\begin{longtable}{|l|l|}'))
myDF
cat(sprintf('\\end{longtable}
\\unskip
\\unpenalty
\\unpenalty}
\\unvbox\ltmcbox
\\end{multicols}'))
@

\end{document}

Which gave me errors:

Error: '\l' is an unrecognized escape in character string starting "'\\begin{multicols}{2}

If you have any advice on how to use the helpful resource from @DavidCarlisle for my specific problem, please let me know. Thank you!

Best Answer

Here is a quick demo of linking R and LaTeX using the 'knitr' package. This has been run on Windows 8.1, with MikTeX 2.9, and TeXmaker 4.4.1 as the IDE. The following code is saved as knit02.Rnw (and this is case sensitive). With the package 'knitr' installed in R 3.1.3 you run the command: knit("knit02.Rnw"). This will generate the file 'knit02.tex' which you now compile with pdflatex and view as a pdf.

The example has three parts. First, a simple longtable; second, a simple tablular; and third, a multi data.frame combined into one longtable. The output is still not optimal, especially in the headings. However at this point, you will have to migrate over to https://stackoverflow.com/questions/tagged/r and ask specific questions on how to use xtable to pass the customizations of longtable.

\documentclass[10pt,letterpaper]{article}
\usepackage{longtable}
\usepackage[tmargin=2in,bmargin=2in]{geometry} % Done to force early page changes for demonstration. 
\begin{document}
\textbf{Note: In all three of these examples the \LaTeX{} environment is determined in the R code block. }


Demonstration of a very large R table exported to \LaTeX{] using the package \textit{longtable}.
<<echo=FALSE>>=
library(xtable)
set.seed(1)
@

<<echo=FALSE,results='asis'>>=
## Demonstration of longtable support.
## Remember to insert \usepackage{longtable} on your LaTeX preamble
x <- matrix(rnorm(500), ncol = 10)
x.big <- xtable(x, label ='tabbig',caption ='Example of longtable spanning several pages')
print(x.big, tabular.environment ='longtable', floating = FALSE)
@
\clearpage
Now here is a regular (as in fit on one page) table using \textit{tabular}.

<<echo=FALSE,results='asis'>>=
x <- x[1:20, ]
x.small <- xtable(x, label ='tabsmall', caption ='regular table env')
print(x.small)  # default, no longtable
@
\clearpage
Demonstration of a  large R table composed of multiple data.frames exported to \LaTeX{] using the package \textit{longtable}.


<<echo=FALSE,results='asis'>>=
## Demonstration of longtable support with combining two data.frames.
## Remember to insert \usepackage{longtable} on your LaTeX preamble
x <- rnorm(100)
y <- rnorm(100)
id<- rep(letters[1:25],4)
spacecolumn<- rep(" ",100)
dtableone<-data.frame(id,x,y)
dtabletwo<-data.frame(id,y,x)
fulltable<-data.frame(dtableone,spacecolumn,dtabletwo)
names(fulltable)<-c("id","x","y"," ","id","y","x")

x.big <- xtable(fulltable, label ='tabtwo',caption ='Example of longtable spanning several pages build from 2 data.frames')
print(x.big, tabular.environment ='longtable', floating = FALSE)
@

\end{document}

Page 1

enter image description here

Page 2

enter image description here

Page 3

enter image description here

Page 4

enter image description here

Page 5

enter image description here

Page 6

enter image description here