【问题标题】:wrap long lines in kable() for use in Rnw/LaTeX [duplicate]在 kable() 中换行以用于 Rnw/LaTeX [重复]
【发布时间】:2015-10-21 16:10:49
【问题描述】:

不久前我向question 询问了如何在 RMarkdown 文档中包装长表格单元格内容。一个不错的答案指向pander() 包。

我又遇到了类似的问题,只是我在使用Rnw 文件,我的理解是pander() 不适用于LaTeX。所以我又回来尝试弄清楚如何在kable() 中换行。

\documentclass{article}
\begin{document}

This is my test

<<test, echo=FALSE>>=
library(knitr)
test <- data.frame(v1=c("This is a long string. This is a long string. This is a long string. This is a long string. This is a long string.",
                        "This is a another long string. This is a another long string. This is a another long string. This is a another long string. This is a another long string."),
                   v2=c(1, 2))
kable(test)
@

\end{document}

【问题讨论】:

  • 嗨,@eric-green 看起来这个问题真的很困扰你。大声笑我看到你的question about it earlier this year 为什么不在knitr 的github 页面上提交问题?
  • 它不会让我彻夜难眠,但正如我在问题中指出的那样,我之前确实以不同的形式遇到过它。我想过提交问题,但我不确定这是kable需要解决的问题。正如我接受的答案所指出的那样,xtable 可能会填补这个空白。
  • 我今天确实遇到了类似的情况,我找到了你的帖子。 xtable 绝对不是我最喜欢的软件包,我也很想看到 kable 将来有这个功能。 :)
  • 我已经开始使用 xtable,但我也喜欢 kable 的简单性。我想同时想要大量的定制和简单是很困难的
  • 上面链接的问题有一个使用kable()的答案。

标签: r knitr


【解决方案1】:

如果您使用乳胶编译 pdf,我推荐使用包 xtable,您几乎可以复制表格环境中的所有内容。 align 选项为您提供您想要的:l - 左,c - 中心,r - 右,或列的大小。我添加了其他选项供您玩耍。在块选项中,您必须添加 results = 'asis'。

library(xtable)
print(xtable(test, caption="A caption", align="lp{2cm}p{2cm}"), 
      comment=F, include.rownames=F, table.placement="ht", 
      size=getOption("xtable.size", "tiny"))

【讨论】:

  • 如果kable() 没有解决方法,这可能是一个很好的建议。除非发布kable() 解决方案,否则我可能会推荐此答案(因为这是我最初的问题)。谢谢!
猜你喜欢
  • 2012-10-03
  • 1970-01-01
  • 1970-01-01
  • 2021-05-23
  • 1970-01-01
  • 2018-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多