【问题标题】:R and Latex using xtable with baseline alignmentR 和 Latex 使用带有基线对齐的 xtable
【发布时间】:2020-01-13 12:36:26
【问题描述】:

我正在使用 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]}

如何解决这个问题?

【问题讨论】:

    标签: r latex knitr


    【解决方案1】:

    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}
    

    【讨论】:

    • 非常感谢。有趣的是缺少这样的选项。
    猜你喜欢
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 2017-02-19
    相关资源
    最近更新 更多