【问题标题】:xtable highlighting: \rowcolor shifted down one rowxtable 高亮显示:\rowcolor 向下移动一行
【发布时间】:2013-11-12 04:43:58
【问题描述】:

我正在关注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()) 命令中有一些额外的东西来适应我的情况,但我认为它们对于这个例子并不重要。

【问题讨论】:

    标签: r latex xtable


    【解决方案1】:

    这对我有用。请注意,我从 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
    )
    

    【讨论】:

    • 谢谢,@Roman。它也适用于我,但我不明白为什么需要减法。但它有效,所以我会使用并感激不尽。如果有人看到没有此修复程序的另一种方法,我很想知道答案。但与此同时,再次感谢。很好地完成了工作。
    • @EricGreen 也许您对pos 参数的假设是错误的。这些位置可能表示 解析 command 的那一行之后...
    • @roman-lustrik 你是对的,使用这种计数方式的另一个好处是 xtable 可以把东西放在\begin{tabular} 之前(pos = -1),或者紧跟在\begin{tabular} 之后( pos = 0),或者紧跟在第一行之后(pos = 1,比如\toprule 命令...)。
    猜你喜欢
    • 2023-03-13
    • 1970-01-01
    • 2016-10-22
    • 2021-06-28
    • 2013-07-26
    • 1970-01-01
    • 2016-08-27
    • 2011-12-13
    • 2011-07-06
    相关资源
    最近更新 更多