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:
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):
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:
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:
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!