xtable 在顶部添加标题并在表格下方添加标题 [英] xtable adding a title on top and a caption under the table

查看:18
本文介绍了xtable 在顶部添加标题并在表格下方添加标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want to put a caption title on the xtable in a Rnw document. Here's the code. Unfortunately, I'm not able to add a caption under the table. I've tried the caption{} function, but it won't print the PDF.

I've seen R: xtable caption (or comment), but it's not working for a table that was created from the lm() function in R. Do you have any clue?

<<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")
@

解决方案

I couldn't see an quick option in xtable to add text to the bottom of the table (that doesn't mean there isn't one) so i have used an idea from here and from the link in your question. It is a rather crude fix with the large drawback that you need to specify the width of the text to add (equal to the width of the table) - if you make it too long it stretches the final column (to see change 8.5 to 10).

documentclass{article}

usepackage{array}

ewcolumntype{L}[1]{>{
aggedrightarraybackslash}m{#1}}

egin{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}

I assume there are many alternatives in latex to accomplish this but might get you started.


From comments: I tried outputting it to html and didn't work. Any thoughts?

You can change the latex command multicolumn in the add.to.row argument of print.table to use html table functions instead. (using html output of Rmarkdown)

```{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>'))

```

这篇关于xtable 在顶部添加标题并在表格下方添加标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆