title: sweave, xtable, longtable and alternating row colors...problems with `add.to.row`
body: <p><a href="https://stackoverflow.com/questions/11777586/customize-xtable">This existing question</a> covers a way to alternate row colors in a latex table by post-processing the output from <code>print.xtable()</code>, but I think it's possible to achieve the same thing by using the <code>add.to.row</code> argument of <code>print.xtable()</code> as described on <a href="https://stats.stackexchange.com/a/7953">stats.stackexchange</a>, avoiding the need for post-processing, which is nice with Sweave. That answer deals with coloring the background of one specific row, but I think it can be extended to coloring all the odd rows.</p>
  
  <p>The problem I'm running into has to do with the <code>add.to.row</code> argument, making the length of list <code>pos</code> equal the length of character vector <code>command</code>. The help file for <code>print.xtable()</code> describes:</p>
  
  <blockquote>
    <p>add.to.row: a list of two components. The first component (which should
              be called 'pos') is a list contains the position of rows on
              which extra commands should be added at the end, The second
              component (which should be called 'command') is a character
              vector of the same length of the first component which
              contains the command that should be added at the end of the
              specified rows. Default value is 'NULL', i.e. do not add
              commands.</p>
  </blockquote>
  
  <p>when using the longtable environment, you can use this <code>add.to.row</code> argument to define the "header" rows of your table that should be printed on every page, like so:</p>
  
  <pre><code>library(xtable)
  my.df=data.frame(a=c(1:10),b=letters[1:10])
  print(xtable(my.data.frame,caption="My Table"),
        tabular.environment="longtable",
        floating=FALSE,
        hline.after=c(-1,nrow(my.data.frame)),
        add.to.row=list(pos=list(0),command="\\hline \\endhead ")
  </code></pre>
  
  <p>I need to keep this functionality, and add the additional functionality that every other row should get the command <code>\\rowcolor[gray]{0.8}</code></p>
  
  <p>Sounds simple enough. <code>pos</code> should be something like <code>list=(0,1,3,5,7,9)</code> and <code>command</code> should be something like <code>c("\\hline \\endhead ","\\rowcolor[gray]{0.8}","\\rowcolor[gray]{0.8}","\\rowcolor[gray]{0.8}","\\rowcolor[gray]{0.8}","\\rowcolor[gray]{0.8}")</code></p>
  
  <p>Of course, I want to take advantage of some built in functions to build the odd-row sequence and the repetition of <code>"\\rowcolor[gray]{0.8}"</code>, so I thought of:</p>
  
  <pre><code>pos=list(0,seq(from=1,to=nrow(my.df),by=2))
  </code></pre>
  
  <p>and </p>
  
  <pre><code>command=c("\\hline \\endhead ",
            rep("\\rowcolor[gray]{0.8}",length(seq(from=1,to=nrow(my.df),by=2))))
  </code></pre>
  
  <p>my problem is that the <code>pos</code> list above evaluates to:</p>
  
  <pre><code>&gt; pos
  [[1]]
  [1] 0
  
  [[2]]
  [1] 1 3 5 7 9
  </code></pre>
  
  <p>which has length 2...it needs to have length 6 in this case.</p>