title: Automatically adjust LaTeX table width to fit pdf using knitr and Rstudio
body: <p>Using Rstudio and knitr to produce latex-tables in pdf, how do I make wide tables fit the page? I'm basically looking for at way to shrink the tables.</p>
  
  <p>With figures, it is really easy in Knitr using out.width=, but with tables I can't seem find a way to do it. </p>
  
  <p>Any suggestions?</p>
  
  <pre><code>\documentclass{article}
  
  \begin{document}
  </code></pre>
  
  <p>The following tables are too wide to fit the pdf. I hope there is a simple way to shrink them to fit. In this example I've used tables generated from the xtable(), stargazer() and latex() functions.</p>
  
  <pre><code>&lt;&lt;message=FALSE&gt;&gt;=
  library(xtable)
  library(stargazer)
  library(Hmisc)
  library(tables)
  wide.df &lt;- cbind(iris[1:10,],iris[1:10,],iris[1:10,])
  
  @
  
  
  
  &lt;&lt;results='asis'&gt;&gt;=
  xtable(wide.df)
  @
  
  
  &lt;&lt;results='asis'&gt;&gt;=
  stargazer(wide.df,summary=FALSE)
  @
  
  
  &lt;&lt;results='asis'&gt;&gt;=
  latex( tabular( Species ~  (Sepal.Length +Sepal.Length +  Sepal.Width +   Petal.Length  +  Petal.Width  )*(mean + sd + mean + mean )          , data=iris)            )
  
  @
  
  
  
  
  \end{document}
  </code></pre>
  
  <p>Following Stat-R's suggestions I've tried to use resizebox but can't get it to work:</p>
  
  <pre><code>\documentclass{article}
  \usepackage{graphicx}
  \begin{document}
  </code></pre>
  
  <p>I've tried to use reshapebox but I am really clueless on how to get it to work in Rstudio/knitr:</p>
  
  <pre><code>&lt;&lt;message=FALSE&gt;&gt;=
  library(xtable)
  wide.df &lt;- cbind(iris[1:10,],iris[1:10,],iris[1:10,])
  @
  
  \resizebox{0.75\textwidth}{!}{%
  &lt;&lt;results='asis'&gt;&gt;=
  xtable(wide.df)
  @
  %}
  
  \end{document}
  </code></pre>
  
  <p>I get this error: </p>
  
  <pre><code>! File ended while scanning use of \Gscale@box@dd.
  
  
  sessioninfo()
  
  R version 3.0.0 (2013-04-03)
  Platform: i386-w64-mingw32/i386 (32-bit)
  
  locale:
  [1] LC_COLLATE=Danish_Denmark.1252  LC_CTYPE=Danish_Denmark.1252    LC_MONETARY=Danish_Denmark.1252 LC_NUMERIC=C                   
  [5] LC_TIME=Danish_Denmark.1252    
  
  attached base packages:
  [1] splines   grid      stats     graphics  grDevices utils     datasets  methods   base     
  
  other attached packages:
   [1] tables_0.7      Hmisc_3.10-1    survival_2.37-4 stargazer_3.0.1 pgirmess_1.5.7  splancs_2.01-32 spdep_0.5-56    coda_0.16-1     deldir_0.0-22  
  [10] maptools_0.8-23 foreign_0.8-53  MASS_7.3-26     Matrix_1.0-12   lattice_0.20-15 rgdal_0.8-9     sp_1.0-9        nlme_3.1-109    boot_1.3-9     
  [19] xtable_1.7-1    scales_0.2.3    plyr_1.8        reshape2_1.2.2  ggplot2_0.9.3.1
  
  loaded via a namespace (and not attached):
   [1] cluster_1.14.4     colorspace_1.2-2   dichromat_2.0-0    digest_0.6.3       evaluate_0.4.3     formatR_0.7        gtable_0.1.2       knitr_1.2         
   [9] labeling_0.1       LearnBayes_2.12    munsell_0.4        proto_0.3-10       RColorBrewer_1.0-5 stringr_0.6.2      tools_3.0.0 
  </code></pre>