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

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

问题描述

我想在Rnw文档的xtable上添加标题标题.这是代码.不幸的是,我无法在表格下添加标题.我已经尝试过\ caption {}函数,但是不会打印PDF.

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.

我看过 R:xtable标题(或评论),但这是对于从R中的lm()函数创建的表不起作用.您有任何线索吗?

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

推荐答案

xtable中,我看不到将文本添加到表格底部的快速选项(这并不意味着没有一个)因此我在此处和您问题的链接中使用了一个想法.这是一个粗略的修正,但有一个很大的缺点,即您需要指定要添加的文本的宽度(等于表的宽度)-如果将其设置得太长,则会拉伸最后一列(以查看8.5到10的变化) ).

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}
\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}

我认为乳胶中有许多替代方法可以完成此操作,但可能会让您入门.

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

从评论中:我尝试将其输出到html,但无法正常工作.有什么想法吗?

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

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

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天全站免登陆