1

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!

  • @Johannes_B I wrote about that reference in the first line of my current post. – user12211991 Jul 01 '15 at 14:04
  • Seems i overlooked it, sorry. – Johannes_B Jul 01 '15 at 14:12
  • 1
    you can't have two longtables side by side just as you can not have two paragraphs side by side if they are both part of the main page flow. Just have a single longtable (or supertab) and allow column and line breaking in two column mode. see http://tex.stackexchange.com/questions/45980/balancing-long-table-inside-multicol-in-latex/46001#46001 – David Carlisle Jul 01 '15 at 14:15
  • @DavidCarlisle: Thank you. However, I am not sure how I could produce that syntax from a data frame automatically (instead of hard-coding the tabular format, like the examples in your reference). I also need to be able to knit it, as well. That was the issue that made me reopen this question from my original post. Thanks again. – user12211991 Jul 01 '15 at 15:37
  • sorry but I can't help with the knit/R issues, (and they are probably off topic for this site) – David Carlisle Jul 01 '15 at 16:11
  • @DavidCarlisle Thank you, if you happen to have a recommendation, where would you recommend me to post knit/R issues? – user12211991 Jul 01 '15 at 16:26
  • if i understand, the link in @DavidCarlisle 's comment is your answer, just don't (cut the data frame in half) you have one longtable. What is the problem? – touhami Jul 02 '15 at 05:53
  • @touhami Thank you, I appreciate it. I added an EDIT to better explain the (frustrating) last bit I cannot successfully apply to my problem from the helpful resource supplied by DavidCarlisle – user12211991 Jul 02 '15 at 18:13
  • Get the xtable (R package) documentation and read thru the examples, especially the example of generating from R a "longtable environment" (pg15). 2) Use R to build a single data frame with the information you are now putting into two data frames. 3) Now just output the single combined table using knitr.
  • – R. Schumacher Jul 02 '15 at 18:45
  • @R.Schumacher Thank you. I will look into those resources. Could you clarify, are you referring to my updated Edit? Because that is where I feel I am the closest to solving this problem. I don't think I am using two data frames any more. If so, at what point are you referring me to doing this? – user12211991 Jul 02 '15 at 18:59
  • Last Edit. Since you already have everything into a single table, then you can have xtable create for you the correct longtable environment. This is very simple. Run the xtable manual example. First inside R console or Rstudio to see what it does. And then embed in an R block. – R. Schumacher Jul 02 '15 at 20:31
  • I have no idea about what knitr is, but if i can ask, did you try to just replace \begin{longtable}{|l|l|l|} \tablines\tablines\tablines\tablines\tablines\tablines \end{longtable} in David Carlisle's code by \print(xtable(myDF),floating=FALSE,tabular.environment = "longtable") ? – touhami Jul 02 '15 at 21:05