Xtable adding a title at the top and an inscription under the table

I want to put the title of the header in xtable in an Rnw document. Here is the code. Unfortunately, I cannot add a caption under the table. I tried the function \ caption {}, but does not print PDF.

I saw R: the header (or comment) , but it does not work for the table that was created from the lm () function in R. Do you have any hint?

 <<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") @ 
+6
source share
1 answer

I could not see the quick option in xtable to add text to the bottom of the table (this does not mean that it is not), so I used the idea from here and the link in your question. This is a rather crude correction with a big drawback that you need to specify the width of the added text (equal to the width of the table). If you make it too long, it will stretch the last column (to see the change from 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} 

enter image description here

I suppose there are many alternatives in latex, but you can start.


From comments: I tried to output it in html and did not work. Any thoughts?

You can modify the multicolumn latex command in the add.to.row print.table argument to use the html table functions instead. (using the 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>')) ``` 
+8
source

Source: https://habr.com/ru/post/977857/


All Articles