0

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

```
GregRousell
  • 997
  • 2
  • 13
  • 23

1 Answers1

0

I was eventually able to come up with a solution using the pixidust package.

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

## Test Table

```{r table, results='asis',message=FALSE, warning=FALSE}

library(pixiedust)

custom_interfoot <- data.frame("Cont'd", 
                               "", "", "", "")

n_rows <- length(iris[,1])

x <- dust(iris,
          longtable = TRUE) %>%
  redust(custom_interfoot, part = "interfoot") %>%
  sprinkle (rows = c(1:n_rows), halign = "center") %>%
  sprinkle (bg_pattern = c ("#D6D6D6", "white")) 


print (x)



```
GregRousell
  • 997
  • 2
  • 13
  • 23