【问题标题】:xtable adding a title on top and a caption under the tablextable 在顶部添加标题并在表格下方添加标题
【发布时间】:2014-11-06 22:23:03
【问题描述】:

我想在 Rnw 文档中的 xtable 上添加标题。这是代码。不幸的是,我无法在表格下方添加标题。我试过 \caption{} 功能,但它不会打印 PDF。

我见过R: xtable caption (or comment),但它不适用于从 R 中的 lm() 函数创建的表。你有什么线索吗?

<<yoman,echo=FALSE,results=tex>>=
library(xtable)

pop5lm <- lm(mpg ~ wt, data=mtcars) #my linear model

print(xtable(pop5lm,
             caption = c("Estimates of linear model for father Muro CB"), 
             label = "tab:one", digits = c(0,2, 2, 2,3)), 
             table.placement = "tbp", 
             caption.placement = "top")
@

【问题讨论】:

  • 好吧,标题打印出来了,但是如果我想在表格下添加描述,它就不起作用了。我不知道该怎么做。例如,我希望添加:“在此表中,我使用了线性模型,blablabla ...”。简而言之,会有标题、表格和链接到表格的描述。顺便说一句,感谢您的快速回复!

标签: r latex sweave caption xtable


【解决方案1】:

我在xtable 中看不到将文本添加到表格底部的快速选项(这并不意味着没有),所以我使用了来自here 和链接的想法在你的问题中。这是一个相当粗略的修复,有一个很大的缺点是您需要指定要添加的文本的宽度(等于表格的宽度) - 如果您将其设置得太长,它会拉伸最后一列(查看将 8.5 更改为 10 )。

\documentclass{article}

\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}m{#1}}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<yoman,echo=FALSE,results=tex>>=
library(xtable)

mod <- lm(mpg ~ wt, data=mtcars) #my linear model

print(xtable(mod,
             caption = "Estimates of linear model for father Muro CB ", 
             #label = "tab:one", 
             digits = c(0,2, 2, 2,3)), 
             table.placement = "h!", 
             caption.placement = "top",
             add.to.row = list(list(2),  
             "\\hline  \\multicolumn{5}{L{8.5cm}}{\\textbf{Note: }
             This is a description, blah, blah, blah, blah,  blah, blah, 
             blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, 
             blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, 
             blah, blah, blah, blah, blah, blah} \\\\"))

@

\end{document}

我认为 Latex 中有很多替代品可以实现这一点,但可能会让你开始。


来自 cmets: 我尝试将其输出为 html,但没有成功。有什么想法吗?

您可以将 print.tableadd.to.row 参数中的乳胶命令 multicolumn 更改为使用 html 表函数。 (使用 Rmarkdown 的 html 输出)

```{r,echo=FALSE, results='asis'}
library(xtable)

mod <- lm(mpg ~ wt, data=mtcars) #my linear model

print(xtable(mod,
             caption = "Estimates of linear model for father Muro CB ", 
             digits = c(0,2, 2, 2,3)), 
             type="html",
             caption.placement = "top",
             add.to.row = list(list(2),  
             '<tr><td colspan="5"><b>Note: </b>
             This is a description, blah, blah, blah, blah,  blah, blah, 
             blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, 
             blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, 
             blah, blah, blah, blah, blah, blah</td></tr>'))

```

【讨论】:

  • 非常感谢!它正在工作。如果您知道,为什么它在最后创建一个新行。是否可以删除它?这是设计目的!
  • 不客气。您是指文本下方的水平线(\hline)吗?如果是这样,请删除指定您想要的线条;使用 hline.after 指定您想要的 \hlines [..., blah}"), hline.after = c(-1, 0))]
  • 哇!!这正是我想要的。你太棒了!
  • 我尝试将其输出到 html 并没有工作。有什么想法吗?
  • @user20650 太棒了!谢谢!
猜你喜欢
  • 2016-10-14
  • 2016-05-21
  • 2021-11-21
  • 1970-01-01
  • 2016-11-13
  • 2011-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多