2017-03-01 29 views
0

我能夠使用longtable來獲取列標題在後續頁面上重複,並且我可以交替排列顏色,但我無法弄清楚如何使用add.to.row將兩者結合起來。以下是顯示兩個表格的示例Markdown代碼。如何在後續頁面上重複列標題並使用xtable交替排列行?

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

``` 

回答

0

我終於能夠使用pixidust包提出解決方案。

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



``` 
相關問題