R/Sweave/Latex - Place comment in table (xtable)

浪尽此生 提交于 2019-12-11 02:25:39

问题


I created a table using R and sweave in LaTeX. A sweave example:

\documentclass{article}
\begin{document}
\SweaveOpts{concordance=TRUE}

<<label=tab1, echo=FALSE, results=tex>>=
library(xtable)
employee <- c('John Doe','Peter Gynn','Jolie Hope')
salary <- c(21000, 23400, 26800)
mData <- data.frame(employee, salary) 
print(xtable(mData, caption = "Salary", align="ccc"), caption.placement="top", hline.after = c(c(-1, 0), nrow(mData)), include.rownames=FALSE) 
@

\end{document}

The basic LaTeX structure of the table is

\begin{table}
\begin{tabular}{cc}
...
\end{tabular}
\end{table}

To save me a lot of work I use the print and xtable functions in R to create the table code in LaTeX. But now I want to add some text between the \end{tabular} and \end{table} statements. The add.to.row argument in the print function does not help, as statements are only placed before \end{tabular}. How can I solve this problem?

Many thanks in advance for your help.


回答1:


A way to do this is to use xtable, remove the table environment floating = FALSE and package threeparttable

\documentclass{article}
\usepackage[para,online,flushleft]{threeparttable}
\begin{document}
\SweaveOpts{concordance=TRUE}

<<label=tab1, echo=FALSE, results=tex>>=
library(xtable)
employee <- c('John Doe','Peter Gynn','Jolie Hope')
salary <- c(21000, 23400, 26800)
mData <- data.frame(employee, salary) 
options(xtable.comment = FALSE)
xt<-xtable(mData, caption = "Salary", align="ccc") 
print(xt,floating = FALSE,
    caption.placement="top",
    hline.after = c(c(-1, 0), nrow(mData)),
    include.rownames=FALSE,
    file="test.tex"
    )
@


\begin{table}[h]
\caption{A table with notes in the end}
  \begin{center}
     \begin{threeparttable}
       % INPUT YOUR TEX HERE :
       \input{test.tex}
     \begin{tablenotes}
       \item[1] aaaa; \item[2] bbbb
     \end{tablenotes}
    \end{threeparttable}
   \end{center}
 \label{table:tablewithnotes}
 \end{table}     
\end{document}`enter code here



来源:https://stackoverflow.com/questions/19904105/r-sweave-latex-place-comment-in-table-xtable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!