使用Knitr和Rstudio自动调整LaTeX表格宽度以适合pdf [英] Automatically adjust LaTeX table width to fit pdf using knitr and Rstudio

查看:294
本文介绍了使用Knitr和Rstudio自动调整LaTeX表格宽度以适合pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Rstudio和knitr以pdf格式生成乳胶表,如何使宽表适合页面?我基本上是在寻找缩小表的方法.

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.

有了数字,在Knitr中使用out.width =确实很容易,但是对于表格我似乎找不到解决方法.

With figures, it is really easy in Knitr using out.width=, but with tables I can't seem find a way to do it.

有什么建议吗?

\documentclass{article}

\begin{document}

下表太宽,无法容纳pdf.我希望有一种简单的方法来缩小它们以使其适合.在此示例中,我使用了从xtable(),stargazer()和latex()函数生成的表.

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.

<<message=FALSE>>=
library(xtable)
library(stargazer)
library(Hmisc)
library(tables)
wide.df <- cbind(iris[1:10,],iris[1:10,],iris[1:10,])

@



<<results='asis'>>=
xtable(wide.df)
@


<<results='asis'>>=
stargazer(wide.df,summary=FALSE)
@


<<results='asis'>>=
latex( tabular( Species ~  (Sepal.Length +Sepal.Length +  Sepal.Width +   Petal.Length  +  Petal.Width  )*(mean + sd + mean + mean )          , data=iris)            )

@




\end{document}

按照Stat-R的建议,我尝试使用resizebox,但无法使其正常工作:

Following Stat-R's suggestions I've tried to use resizebox but can't get it to work:

\documentclass{article}
\usepackage{graphicx}
\begin{document}

我尝试过使用reshapebox,但是对于如何使其在Rstudio/knitr中工作我一无所知:

I've tried to use reshapebox but I am really clueless on how to get it to work in Rstudio/knitr:

<<message=FALSE>>=
library(xtable)
wide.df <- cbind(iris[1:10,],iris[1:10,],iris[1:10,])
@

\resizebox{0.75\textwidth}{!}{%
<<results='asis'>>=
xtable(wide.df)
@
%}

\end{document}

我收到此错误:

! 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 

推荐答案

您可以像这样将scalebox参数传递给print.xtable

You can pass a scalebox argument to print.xtable like so

<<results='asis'>>=
print(xtable(wide.df), scalebox='0.75')
@

这不会自动调整表格的大小以适合页面(不幸的是xtable不支持resizebox参数),但是对于许多应用程序来说,以上内容可能就足够了.

That doesn't automatically resize the table to fit the page (unfortunately xtable doesn't support a resizebox argument) but for many applications the above might be good enough.

您的代码的问题是xtable返回包装在table环境中的表,而不仅仅是表格.但是,您必须在resizebox中包装的是tabular.我可以看到的使它按您希望的方式工作的唯一方法是让xtable仅返回tabular,如下所示:

The problem with your code is that xtable returns the table wrapped in a table environment and not just a tabular. What you have to wrap in the resizebox, however, is the tabular. The only way I can see to get this to work as you want it is to let xtable return only the tabular, like so:

\begin{table}
\resizebox{\textwidth}{!} {
<<results='asis'>>=
print(xtable(wide.df), floating=FALSE)
@
}
\end{table}

,然后手动在其周围编写LaTeX代码.

and then to write the LaTeX code around it manually.

这篇关于使用Knitr和Rstudio自动调整LaTeX表格宽度以适合pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆