xtable突出显示:\ rowcolor向下移动一行

时间:2013-11-12 04:43:59

标签: r latex xtable

我正在关注this example,试图将突出显示添加到由xtable创建的LaTeX表的行中。在我的版本中,我想在列c的值为1的条件下向行添加突出显示。这是我生成表的R代码。

<<example>>=
# create data frame
  my.df=data.frame(a=c(1:10),b=letters[1:10],c=sample(c(0,1), 10, replace=TRUE))

# identify index of rows to highlight
  row.i.1 <- which(my.df$c==1)

print(xtable(my.df),
      only.contents=TRUE,
      include.rownames=FALSE,
      include.colnames=FALSE,
      hline.after=NULL,
      type="latex",
      add.to.row=list(
            pos=list(as.list(row.i.1))[[1]],
            command=rep("\\rowcolor{green!20!white}",
                        length(seq(from=1,to=length(row.i.1),by=1)))),
      sanitize.text.function=identity
      )
@

这会产生下表:

% latex table generated in R 3.0.1 by xtable 1.7-1 package
% Mon Nov 11 23:31:51 2013
   1 & a & 1.00 \\ 
   \rowcolor{green!20!white}  2 & b & 1.00 \\ 
   \rowcolor{green!20!white}  3 & c & 1.00 \\ 
   \rowcolor{green!20!white}  4 & d & 0.00 \\ 
    5 & e & 1.00 \\ 
   \rowcolor{green!20!white}  6 & f & 0.00 \\ 
    7 & g & 0.00 \\ 
    8 & h & 1.00 \\ 
   \rowcolor{green!20!white}  9 & i & 0.00 \\ 
   10 & j & 1.00 \\ 
   \rowcolor{green!20!white}

如您所见,\ rowcolors向下移动了1行。它应该是:

   \rowcolor{green!20!white}  1 & a & 1.00 \\ 
   \rowcolor{green!20!white}  2 & b & 1.00 \\ 
   \rowcolor{green!20!white}  3 & c & 1.00 \\ 
    4 & d & 0.00 \\ 
   \rowcolor{green!20!white}  5 & e & 1.00 \\ 
    6 & f & 0.00 \\ 
    7 & g & 0.00 \\ 
   \rowcolor{green!20!white}  8 & h & 1.00 \\ 
    9 & i & 0.00 \\ 
   \rowcolor{green!20!white}  10 & j & 1.00 \\ 

造成这种情况的原因是什么?我在print(xtable())命令中有一些额外的东西适合我的情况,但我不认为这个例子很重要。

1 个答案:

答案 0 :(得分:0)

这对我有用。请注意,我从pos值减去了一个。

print(xtable(my.df),
      only.contents=TRUE,
      include.rownames=FALSE,
      include.colnames=FALSE,
      hline.after=NULL,
      type="latex",
      add.to.row=list(
        pos=list(as.list(row.i.1-1))[[1]],
        command=rep("\\rowcolor{green!20!white}",
                    length(seq(from=1,to=length(row.i.1),by=1)))),
      sanitize.text.function=identity
)
相关问题