[Tex/LaTex] Wide table problem when generating PDF

conversionrtables

I have a LaTeX wide table generated from a data frame in R using xtable, but when the PDF is created not all the data is shown, just a part of it, and the other part is hidden on the right of the PDF. How this can be solved?

cat("\\documentclass{article}\n\begin{document}\n", file=txt)
 print(xtable(table), include.rownames=TRUE, floating=FALSE, file=txt, append=TRUE)   
  cat("\\end{document}\n", file=txt, append=TRUE)

where should i place the scalebox thing ??

Best Answer

I would suggest either to rotate the Table, using the lscape package, or to resize your table (easier, IMHO) with \scalebox from the graphics package. I often use the latter for "not too large" tables (say, 10 rows by 5 to 7 columns with custom headings), that won't fit in a rotated page. It has to be done from within your tex file.

Here is a toy example:

  • In R:

    x <- replicate(2, sample(LETTERS[1:10], 100, rep=T))
    tab <- table(x[,1], x[,2])
    library(xtable)
    print(xtable(tab, digits=0), file="tab.tex", floating=FALSE)
    

Here we ask to write the resulting table in a file (you may have to change the full path to reflect your working tex directory), tab.tex. This way, you don't have to edit your Latex file too much.

  • In Latex:

    \documentclass{article}
    \usepackage{graphics}
    
    \begin{document}
    \scalebox{0.9}{\input{tab}}
    \end{document}
    

The scaling factor used here, 0.9, means 90% of text width. See the on-line help for further details. You can then compile the resulting file with pdflatex.

There's a subtlety here: I didn't ask to get a floating table (see floating=FALSE when calling xtable()), bacause we can't use \scalebox around a float. Should you want to add caption, label, etc., you just need to replace the above command with

\begin{table}[ht]
\begin{center}
\input{tab}
\end{center}
\end{table}

Finally, the landscape solution is easily obtained as

\documentclass{article}
\usepackage{lscape}

\begin{document}
\begin{landscape}
\input{tab}
\end{landscape}
\end{document}

Here, it will work with or without asking for a floating object when calling xtable() in R.


Edit:

With the syntax you provided in your updated question, here's is how it should read:

cat("\\documentclass{article}\n\\usepackage{graphics}\n\\begin{document}\n", file="1.tex")
cat("\\scalebox{0.7}{", file="1.tex", append=TRUE)
print(xtable(tab), include.rownames=TRUE, floating=FALSE, file="1.tex", append=TRUE) 
cat("}", file="1.tex", append=TRUE)  
cat("\\end{document}\n", file="1.tex", append=TRUE)

Here, tab refers to the two-by-two table from my example, replace it by table if you named it like this (which is not a good idea because this is the name of an R function). The table is already aligned on the left margin. So, if you want to change that, a quick and dirty fix is to ask for right to left shift (I'm pretty sure TeXnicians know of a better way to do that):

cat("\\scalebox{0.7}{\\hskip-50pt", file="1.tex", append=TRUE)
Related Question