xtable高亮显示:\ rowcolor下移了一行 [英] xtable highlighting: \rowcolor shifted down one row

查看:98
本文介绍了xtable高亮显示:\ rowcolor下移了一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注此示例尝试将突出显示添加到由 xtable 创建的LaTeX表的行中.在我的版本中,我想在列 c 的条件为1的行中添加突出显示.这是我的 R 代码以生成表.

I am following this example in an attempt to add highlighting to rows of a LaTeX table created by xtable. In my version, I want to add highlighting to rows conditional upon column c having a value of 1. Here's my R code to generate the table.

<<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行.应该是:

As you can see, the \rowcolors are shifted down by 1 row. It should be:

   \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())命令中有一些其他功能可以满足我的情况,但是对于这个示例,我认为它们并不重要.

What is causing this? I have a few extras in my print(xtable()) command to fit my situation, but I don't think they matter for this example.

推荐答案

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

This works for me. Notice that I substracted one from pos values.

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
)

这篇关于xtable高亮显示:\ rowcolor下移了一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆