问题描述
使用 Rstudio 和 knitr 制作 pdf 格式的乳胶表格,如何使宽表格适合页面?我基本上是在寻找缩小表格的方法.
对于数字,在 Knitr 中使用 out.width= 非常容易,但对于表格,我似乎找不到方法.
有什么建议吗?
documentclass{文章}开始{文档}
以下表格太宽,无法容纳 pdf.我希望有一种简单的方法可以缩小它们以适应.在此示例中,我使用了从 xtable()、stargazer() 和 latex() 函数生成的表.
<<message=FALSE>>=库(xtable)图书馆(观星者)图书馆(Hmisc)图书馆(表)wide.df <- cbind(iris[1:10,],iris[1:10,],iris[1:10,])@<<results='asis'>>=xtable(wide.df)@<<results='asis'>>=观星者(wide.df,summary=FALSE)@<<results='asis'>>=乳胶(表格(物种〜(萼片长度+萼片长度+萼片宽度+花瓣长度+花瓣宽度)*(平均值+标准差+平均值+平均值),数据=虹膜)@结束{文档}
按照 Stat-R 的建议,我尝试使用 resizebox 但无法正常工作:
documentclass{文章}使用包{graphicx}开始{文档}
我尝试使用 reshapebox,但我真的不知道如何让它在 Rstudio/knitr 中工作:
<<message=FALSE>>=库(xtable)wide.df <- cbind(iris[1:10,],iris[1:10,],iris[1:10,])@
esizebox{0.75 extwidth}{!}{%<<results='asis'>>=xtable(wide.df)@%}结束{文档}
我得到这个错误:
!扫描使用 Gscale@box@dd 时文件结束.会话信息()R 版本 3.0.0 (2013-04-03)平台:i386-w64-mingw32/i386(32位)语言环境:[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附加的基础包:[1] 样条网格统计图形 grDevices utils 数据集方法库其他附加包:[1] tables_0.7 Hmisc_3.10-1survival_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通过命名空间加载(未附加):[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
更新以反映过去几年代码的变化,以及人们通常使用 .RMarkdown 而不是 Rnw 文件格式工作的偏好.
R 中的 kableExtra
包是调整表格大小的最简单方法.您可以使用函数 kable_styling(latex_options = "scale_down")
来缩放表格的宽度.这将强制表格与页面宽度一致.
kable(iris[1:5,],格式 = 乳胶",booktabs = TRUE) %>%kable_styling(latex_options = "scale_down")
有关 kableExtra 包的更多示例,请在此处查看该包:
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.
With figures, it is really easy in Knitr using out.width=, but with tables I can't seem find a way to do it.
Any suggestions?
documentclass{article}
egin{document}
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}
Following Stat-R's suggestions I've tried to use resizebox but can't get it to work:
documentclass{article}
usepackage{graphicx}
egin{document}
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,])
@
esizebox{0.75 extwidth}{!}{%
<<results='asis'>>=
xtable(wide.df)
@
%}
end{document}
I get this error:
! 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
Updating to reflect the changes in code past few years, and the preference for people to typically work in .RMarkdown instead of Rnw file format.
The kableExtra
package in R is the easiest way for adjusting the size of tables. You can scale the width of the table using the function kable_styling(latex_options = "scale_down")
. This will force the table to the width of the page.
kable(iris[1:5,],
format = "latex", booktabs = TRUE) %>%
kable_styling(latex_options = "scale_down")
Here is an example MWE:
---
title: "MWE"
author: "Mikey Harper"
date: "7 November 2017"
output: pdf_document
---
```{r setup, include=FALSE}
library(kableExtra)
library(magrittr)
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
# Build the dataframe
wide.df <- cbind(iris[1:10,],iris[1:10,],iris[1:10,])
```
```{r}
# Basic table
knitr::kable(wide.df)
```
```{r}
# Scaled Table
knitr::kable(wide.df, format = "latex", booktabs = TRUE) %>%
kable_styling(latex_options = "scale_down")
```
这篇关于使用 knitr 和 Rstudio 自动调整 LaTeX 表格宽度以适合 pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!