gpt4 book ai didi

r - 使用 knit 和 Rstudio 自动调整 LaTeX 表格宽度以适合 pdf

转载 作者:行者123 更新时间:2023-12-01 21:57:10 25 4
gpt4 key购买 nike

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

对于数字,在 Knitr 中使用 out.width= 确实很容易,但是对于表格,我似乎找不到方法来做到这一点。

有什么建议吗?

\documentclass{article}

\begin{document}

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

<<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,但无法让它工作:

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

我尝试过使用 reshapebox,但我真的不知道如何让它在 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

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

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

您的代码的问题是 xtable 返回包装在 table 环境中的表格,而不仅仅是表格。然而,您必须在 resizebox 中包装的是 tabular。我认为要让它按照您想要的方式工作的唯一方法是让 xtable 仅返回 tabular,如下所示:

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

然后手动编写 LaTeX 代码。

关于r - 使用 knit 和 Rstudio 自动调整 LaTeX 表格宽度以适合 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16507191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com