简体   繁体   English

从R的xtable修剪LaTeX表中的额外水平规则

[英]Trimming extra horizontal rule in LaTeX tables from R's xtable

Q: How can I trim the extra horizontal rule length in LaTeX tables from R's xtable ? 问:如何从R的xtable修剪LaTeX表中多余的水平规则长度?

I'd like to do something like you see in my R code below. 我想做类似您在下面的R代码中看到的操作。 But that code throws and error, not producing the desired result. 但是该代码会引发错误,无法产生预期的结果。

% table 1
    \begin{table}[ht]
      \centering
      \begin{tabular}{@{}lrr@{}}
        \toprule  
        & a\_a & b\_b \\ 
        \midrule 
        A & 0.35 & -1.05 \\ 
        B & 0.64 & 0.92 \\ 
        \bottomrule 
      \end{tabular}
    \end{table}

% table 2 
    \begin{table}[ht]
      \centering
      \begin{tabular}{lrr}
        \toprule  
        & a\_a & b\_b \\ 
        \midrule 
        A & 0.36 & -1.05 \\ 
        B & 0.64 & 0.92 \\ 
        \bottomrule 
      \end{tabular}
    \end{table}

表

# Begin R code
library(xtable)
library(stringr)
options(xtable.comment = FALSE)
set.seed(10)
mat <- matrix(rnorm(4), nrow=2)
rownames(mat) <- LETTERS[1:2]
colnames(mat) <- str_c(letters[1:2], "_", letters[1:2])
#mat.x <- xtable(mat, caption="My Caption!", align="lrr", digits=2) # No Error!
mat.x <- xtable(mat, align="@{}lrr@{}", digits=2) # Error!
print(mat.x, 
      sanitize.text.function = function(x){x},
      sanitize.colnames.function = function(x){str_replace_all(x, "_", "\\\\_")},
      floating=T,
      hline.after=NULL,
      math.style.negative=F,
      add.to.row=list(pos=list(-1,0,nrow(mat.x)),
                      command=c("\\toprule ", "\\midrule ", "\\bottomrule ")))

# Error in `align<-.xtable`(`*tmp*`, value = "@{}lrr@{}") : 
#  "align" must have length equal to 3 ( ncol(x) + 1 )
# In addition: Warning message:
#  In .alignStringToVector(value) : Nonstandard alignments in align string

You need to pass either a string with the same number of characters as the number of columns + 1, or a string of this length. 您需要传递的字符数与列数+ 1相同的字符串,或者此长度的字符串。 So try (amongst other permutations) 因此,请尝试(在其他排列中)

align=c("@{}l", "r", "r@{}")

To see why have a look at the functions. 要了解为什么要看一下功能。

library(xtable)

set.seed(10)
mat <- matrix(rnorm(4), nrow=2)
rownames(mat) <- LETTERS[1:2]
colnames(mat) <- paste0(letters[1:2], "_", letters[1:2])
xtable(mat, align="@{}lrr@{}", digits=2) 

"align" must have length equal to 3 ( ncol(x) + 1 ) “ align”的长度必须等于3(ncol(x)+1)

The warning suggests passing a vector of length three, so try 警告提示您传递长度为三的向量,因此请尝试

xtable(mat, align=c("@{}l","r", "r@{}"), digits=2)
# \begin{table}[ht]
# \centering
# \begin{tabular}{@{}lrr@{}}
# ---
# ---

So that works as expected. 这样可以正常工作。 Also the align argument is robust to how you pass the string, as long as it is of the required length. 而且,只要它具有所需的长度,align参数对于传递字符串的方式也很可靠。

xtable(mat, align=c("@{}lrr@", "{", "}"), digits=2)
# \begin{table}[ht]
# \centering
# \begin{tabular}{@{}lrr@{}}
# ---
# ---

To see why the first string didn't work look at the relevant parts of xtable:::xtable.data.frame function: must be to do with align 要了解为什么第一个字符串不起作用,请查看xtable:::xtable.data.frame函数的相关部分:必须与align

align(x) <- switch(1 + is.null(align), align, c("r", c("r", "l")[(characters | factors) + 1]))

This leads to xtable:::`align<-.xtable , and then xtable:::.alignStringToVector 这导致xtable:::`align<-.xtable ,然后是xtable:::.alignStringToVector

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

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