R 和 Latex 使用具有基线对齐的 xtable

R and Latex using xtable with baseline alignment

我正在使用 knitr/sweave 动态生成 R 代码并将其包含到我的乳胶文档中。我的最小代码是:

list <- c("Bike",  "Train", "Bike",  "Bike",  "Bike",  "Train", "Bike",  "Bike", "Bike",  "Moped", "Bus",   "Moped", "Train", "Bus", "Moped", "Bike","Moped")

library(xtable)
print(xtable(table(list)),floating=FALSE,booktabs=TRUE)

这会产生输出:

\begin{tabular}{rr}
  \toprule
 & list \ 
  \midrule
Bike &   8 \ 
  Bus &   2 \ 
  Moped &   4 \ 
  Train &   3 \ 
   \bottomrule
\end{tabular}

我想要的是将 tabular 环境与基线对齐,即 \begin{tabular}[t]{rr}...\end{tabular}

查看 xtable 文档有 table.placement 但这引入了 table 环境。我试过 tabular.environment 并将其设置为 tabular[t] 但是没有用,因为它给出了 \begin{tabular[t]}

如何解决这个问题?

xtable 包不支持添加该可选参数,但您可以通过编辑结果自行添加。例如,

list <- c("Bike",  "Train", "Bike",  "Bike",  "Bike",  "Train", "Bike",  "Bike", "Bike",  "Moped", "Bus",   "Moped", "Train", "Bus", "Moped", "Bike","Moped")

library(xtable)
lines <- print(xtable(table(list)), 
               floating = FALSE, booktabs = TRUE, print.result = FALSE)
lines <- sub("\begin{tabular}", "\begin{tabular}[t]", lines, fixed = TRUE)
cat(lines)
#> % latex table generated in R 3.6.1 by xtable 1.8-4 package
#> % Mon Jan 13 09:02:27 2020
#> \begin{tabular}[t]{rr}
#>   \toprule
#>  & list \ 
#>   \midrule
#> Bike &   8 \ 
#>   Bus &   2 \ 
#>   Moped &   4 \ 
#>   Train &   3 \ 
#>    \bottomrule
#> \end{tabular}