compareGroups table 与 knitr - 如何调整 table 标题和潜台词?

compareGroups table with knitr - how to adjust table head and subtext?

我刚刚发现了很棒的包 compareGroups,我想这对我将来会有很大帮助。到目前为止,它与 Knitr/Sweave:

结合使用时表现出色
\documentclass{article}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% LaTeX packages needed for compareGroups

% long tables
\usepackage{longtable}

% multi row
\usepackage{multirow}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

% R code
<<results = "asis">>=

library(compareGroups)

# Very short code for bivariate descriptive table
tab <- compareGroups(vs ~ . , data = mtcars)
export2latex(createTable(tab))

@

\end{document}

生产

我只有两个问题,即 i) table 标题的修改(例如,我想打印 "P value" 而不是 "p.overall")和 ii) 添加多列子文本(例如 "t-test was used to calculate P values.")。如果我只是将 table 导出到 LaTeX 代码,修改 head 和 subtext 会很容易,但结合 Knitr 我不知道如何处理它。我真的很感激这个与费力的功能相结合的绝妙解决方案。 Xtables 包。

非常感谢您的帮助!

问题 i) 可以通过在调用 export2latex 之前手动重新分配矩阵 tab$descr 第 3 列的名称来解决。问题 ii) 可以通过将文本放在标题中来解决(在我看来这更合适——而且我不知道如何将其粘贴到 multicol 子标题中)。

tab <- compareGroups(vs ~ . , data = mtcars)
tab <- createTable(tab)
colnames(tab$descr)[3] <- "P value"
export2latex(tab, caption="$t$-test was used to calculate $p$ values.",
             loc.caption="bottom")

关于您要求(在下方)添加,事实上,两个标题(table 上方的编号标题和 table 下方的 non-numbered 解释性 sub-text ), 实现此目的的一种方法是将 export2latex 生成的 longtable 封装在(可能居中的)minipage 环境中,并将第二个标题作为普通文本放置在 [=28= 下方]:

\documentclass{article}
\usepackage{longtable}
\usepackage{multirow}

\begin{document}

See Table~\ref{tab:complicated} for the main results of this paper.
\begin{center}
  \begin{minipage}[c]{0.5\textwidth}

<<results = "asis", echo=FALSE>>=

library(compareGroups)

tab <- compareGroups(vs ~ . , data = mtcars)
tab <- createTable(tab)
colnames(tab$descr)[3] <- "P value"
export2latex(tab, caption="Characteristics of the patients at baseline$^*$.",
             label="tab:complicated"
             )
@
{\footnotesize $^*$A two-sided $t$-test was used to calculate the $p$ values.}
\end{minipage} 
\end{center}

\end{document}

参考文献:

https://tex.stackexchange.com/questions/15282/tabular-title-above-and-caption-below