xtable在顶部添加标题,在表格下添加标题

时间:2014-11-06 22:23:04

标签: r latex sweave caption xtable

我想在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")
@

1 个答案:

答案 0 :(得分:9)

我无法在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并且无法正常工作。有什么想法吗?

您可以更改multicolumn add.to.row参数中的latex命令print.table来改为使用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>'))

```
相关问题