I am able to use longtable to get the column headers to repeat on subsequent pages, and I'm able to alternate row colour, but I can't figure out how to combine the two using add.to.row. Below is sample Markdown code that shows the two tables.
---
title: "Test"
header-includes:
- \usepackage{fontspec}
- \setmainfont{Arial}
- \usepackage{fancyhdr}
- \usepackage{booktabs} # For Colored rows in tables
- \usepackage[table]{xcolor} # For Colored rows in tables
- \usepackage{longtable}
output:
pdf_document:
latex_engine: xelatex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Table with repeating headers
```{r table1, results='asis'}
library(knitr)
library (xtable)
x <- iris
add.to.row <- list(pos = list(0), command = NULL)
command <- paste0("\\hline\n\\endhead\n",
"\\hline\n",
"\\multicolumn{", dim(x)[2] + 1, "}{l}",
"{\\footnotesize Continued on next page}\n",
"\\endfoot\n",
"\\endlastfoot\n")
add.to.row$command <- command
print (xtable (x),
add.to.row = add.to.row,
tabular.environment = "longtable")
```
## Table with Alternate Colours
```{r table2, results='asis'}
rws <- seq(1, (nrow(x)-1), by = 2)
col <- rep("\\rowcolor[gray]{0.95}", length(rws))
print(xtable(x), booktabs = TRUE,
add.to.row = list(pos = as.list(rws), command = col),
tabular.environment = "longtable")
```