简体   繁体   English

R 和 Latex 使用 xtable 与基线对齐

[英]R and Latex using xtable with baseline alignment

I'm using knitr/sweave to dynamically produce and include R code into my latex document.我正在使用 knitr/sweave 动态生成 R 代码并将其包含到我的乳胶文档中。 A minimal code I have is:我拥有的最小代码是:

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)

This produces as output:这产生作为输出:

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

What I want is to top align the tabular environment to the baseline, ie \\begin{tabular}[t]{rr}...\\end{tabular} .我想要的是将tabular环境与基线对齐,即\\begin{tabular}[t]{rr}...\\end{tabular}

Looking through the xtable documentation there is table.placement but this introduces a table environment.查看xtable文档有table.placement但这引入了table环境。 I tried tabular.environment and set it to tabular[t] but that didn't work as it gave \\begin{tabular[t]}我试过tabular.environment并将其设置为tabular[t]但这不起作用,因为它给了\\begin{tabular[t]}

How can this issue be solve?如何解决这个问题?

The xtable package doesn't support adding that optional argument, but you could add it yourself by editing the result. xtable包不支持添加该可选参数,但您可以通过编辑结果自行添加。 For example,例如,

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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM