Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9217005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:32:35+00:00 2026-06-18T02:32:35+00:00

This existing question covers a way to alternate row colors in a latex table

  • 0

This existing question covers a way to alternate row colors in a latex table by post-processing the output from print.xtable(), but I think it’s possible to achieve the same thing by using the add.to.row argument of print.xtable() as described on stats.stackexchange, 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.

The problem I’m running into has to do with the add.to.row argument, making the length of list pos equal the length of character vector command. The help file for print.xtable() describes:

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.

when using the longtable environment, you can use this add.to.row argument to define the “header” rows of your table that should be printed on every page, like so:

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 ")

I need to keep this functionality, and add the additional functionality that every other row should get the command \\rowcolor[gray]{0.8}

Sounds simple enough. pos should be something like list=(0,1,3,5,7,9) and command should be something like c("\\hline \\endhead ","\\rowcolor[gray]{0.8}","\\rowcolor[gray]{0.8}","\\rowcolor[gray]{0.8}","\\rowcolor[gray]{0.8}","\\rowcolor[gray]{0.8}")

Of course, I want to take advantage of some built in functions to build the odd-row sequence and the repetition of "\\rowcolor[gray]{0.8}", so I thought of:

pos=list(0,seq(from=1,to=nrow(my.df),by=2))

and

command=c("\\hline \\endhead ",
          rep("\\rowcolor[gray]{0.8}",length(seq(from=1,to=nrow(my.df),by=2))))

my problem is that the pos list above evaluates to:

> pos
[[1]]
[1] 0

[[2]]
[1] 1 3 5 7 9

which has length 2…it needs to have length 6 in this case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:32:37+00:00Added an answer on June 18, 2026 at 2:32 am

    The trick is to flatten out the list.
    There may be a prettier way, the following does the trick.

    pos=list(as.list(c(0,seq(from=1,to=nrow(my.df),by=2))))[[1]]
    

    The whole package is then:

    library(xtable)
    my.df=data.frame(a=c(1:10),b=letters[1:10])
    
    print(xtable(my.df,caption="My Table"),
          tabular.environment="longtable",
          floating=FALSE,
          hline.after=c(-1,nrow(my.df)),
          add.to.row=list(
          pos=list(as.list(c(0,seq(from=1,to=nrow(my.df),by=2))))[[1]],
          command=c("\\hline \\endhead ",
          rep("\\rowcolor[gray]{0.8}",length(seq(from=1,to=nrow(my.df),by=2)))))
          )
    

    which produces

    % latex table generated in R 2.14.2 by xtable 1.7-0 package
    % Thu Jan 31 12:52:55 2013
    \begin{longtable}{rrl}
      \hline
     & a & b \\ 
      \hline \endhead 1 &   1 & a \\ 
       \rowcolor[gray]{0.8}2 &   2 & b \\ 
      3 &   3 & c \\ 
       \rowcolor[gray]{0.8}4 &   4 & d \\ 
      5 &   5 & e \\ 
       \rowcolor[gray]{0.8}6 &   6 & f \\ 
      7 &   7 & g \\ 
       \rowcolor[gray]{0.8}8 &   8 & h \\ 
      9 &   9 & i \\ 
       \rowcolor[gray]{0.8}10 &  10 & j \\ 
       \hline
    \hline
    \caption{My Table}
    \end{longtable}
    

    the formatting of the latex is a little ugly, and since \rowcolor is supposed to come before the row, even though we specified 1,3,5,9 we get coloring on 2,4,6,8 of the output.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a continuation from an existing question. Javascript - Goto URL based on
From a existing question here , someone gave this example code: int status; child_pid
So this question isn't about integrating an existing payment gateway into my site. This
I have been following the answer of this question: How to update existing object
I've been looking for an existing answer to this question, but haven't found one.
I don't know how to write the title for this question. The existing title
This question is related to an existing question I asked. I though I'll ask
This existing question sums up the basics of my question. The best answers there
I already found this existing question Is this possible in XHTML: tags in a
I haven't found an existing question on this exact problem (there were several that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.