Philipp R
Philipp R

Reputation: 615

alternate row color with knitr:kable in R Markdown

I want to display alternating row colors using knitr:kablein R Markdown rendering PDF with xelatex. E.g. knitr::kable(mtcars,format="markdown")

Upvotes: 0

Views: 5881

Answers (2)

Daniel_j_iii
Daniel_j_iii

Reputation: 3252

This uses the package kableExtra

    ---
    title: "Habits"
    output: html_document
    ---
    
    <style>
    .striped tr:nth-child(even) { background: #00ff00; }
    </style>
    
    ```{r message=FALSE, warning=FALSE, echo = FALSE}
    library(kableExtra)
    
    kable(mtcars[1:15, 1:4], table.attr = "class=\"striped\"",
      format = "html")
    ```

This will give you an HTML output, but you can simply just "Print to PDF" from the browser

enter image description here

Upvotes: 1

Hao
Hao

Reputation: 7856

You can do it with kableExtra

knitr::kable(mtcars, "html") %>%
  kable_styling("striped")

or

knitr::kable(mtcars, "latex") %>%
  kable_styling(latex_options = "striped")

Upvotes: 2

Related Questions