2014-11-06 96 views
6

我想在Rnw文档的xtable上添加标题标题。这是代码。不幸的是,我无法在表格下添加标题。我试过\ caption {}函数,但它不会打印PDF。xtable在顶部添加一个标题并在表格下添加标题

我见过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") 
@ 
+0

好吧,标题打印,但如果我想在表下添加说明,它不起作用。我不知道该怎么做。例如,我想补充一句:“在这张表中,我使用了线性模型,blablabla ...”。简而言之,将会有与表格相关的标题,表格和描述。顺便说一下,感谢您的快速回复! – 2014-11-07 00:59:10

回答

8

我看不出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} 

enter image description here

我假设有乳胶许多替代来实现这一点,但可能让你开始。


从评论:我试图将其输出到HTML和没有工作。有什么想法吗?

您可以改变的add.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>')) 

``` 
+0

非常感谢!它正在工作。如果你知道,为什么它在最后创造了一条新线。是否有可能将其删除?这是一个设计目的! – 2014-11-07 04:16:21

+0

不客气。你的意思是文字下方的水平线(\ hline)?如果是这样,删除指定你想要的行的位置;使用'hline.after'指定你想要的\ hlines ['...,blah}“),hline.after = c(-1,0))'] – user20650 2014-11-07 04:24:04

+0

哇!这正是我想要的。你真棒! – 2014-11-07 05:46:20