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. – luckButtered Jul 1 '15 at 14:04
  • Seems i overlooked it, sorry. – Johannes_B Jul 1 '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 tex.stackexchange.com/questions/45980/… – David Carlisle Jul 1 '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. – luckButtered Jul 1 '15 at 15:37
3

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

  • But your example doesn't use a 2-column (e.g. multicols) environment. I thought that it was combining that with the knitr stuff which was the problem? However, perhaps I've misunderstood - the question is much longer now than when I first read it and I have not reread it at all carefully! – cfr Jul 3 '15 at 2:57
  • @cfr The OP original problem appears to be that two tables were created in R and passed to LaTeX via knitr. And longtable will not easily allow two separate longtables to be side by side on the same page, nor will it properly page break it. So I suggested to the OP before that the two table be first combined in R and then use the ability of knitr to envoke the longtable environment in LaTeX. – R. Schumacher Jul 3 '15 at 3:13
  • @R.Schumacher Thank you for your help. I am able to get something close to what I desire. My only area for improvement is to make the output appear like two columns. However, I have opened that up on a new discussion (stackoverflow.com/questions/31247757/…). – luckButtered Jul 6 '15 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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