1

I am trying to change the background color of table headers in R Markdown knitted to Slidy HTML. I use xtable for the output and found it easiest with CSS over other methods. I decided to go with a CSS chunk instead of a CSS file.

---
title: "Test"
output: slidy_presentation
---

```{r setup, include = F}
library(xtable)
```

CSS chunk

```{css, header, echo = F}
th {
  background: orange;
}
```

Table output

# Table output

```{r table, results = "asis", echo = F}

df <- data.frame("Col.A" = 1:3, "Col.B" = 4:6)

print(xtable(df), type = "HTML", include.rownames = F)

```

Here's the table with headers in orange. However, with {css, echo = F}, there's a blank page between the title page (#1) and the actual output (#3). I also notice, on the upper left corner in output, it reads Table output (3/2). 3/2... On the blank page, it reads Table output (2/2). And there is not a 3/2 in the dropdown. I need to mouse click to advance to page 3 (the actual output). In the Table of Contents, however, there are three pages, Test (the title page), Test(2), and Table output.

If I use {css, include = F}, the blank page was gone (just the title page and the output page) and so was the background color of the table headers.

I found in another thread, someone also recommended a CSS chunk without any indication of a blank page with such a method. How do I change the background color of the table headers without a blank page? Thank you!

Xikeck
  • 53
  • 2
  • When I create an RMarkdown file with exactly what's in the three code chunks in your question, I get two slides -- the title page and the table. I can't reproduce your problem. – lhs May 26 '22 at 00:02
  • Thanks for trying to replicate the issue and confirm your results. Any clue on the different outputs with the same code? – Xikeck May 26 '22 at 14:18
  • Do you have other content in the document? What happens when you run exactly what's in your question? I'm using RMarkdown version 2.11 and pandoc version 2.17.1.1. – lhs May 26 '22 at 14:42
  • No other content. I ran the exact same code and had the same issue on both Win 10 and MacOS 12.4. – Xikeck May 28 '22 at 01:52

1 Answers1

0

Is there a reason why you don't want to use a css file? Adding it in the yaml section is so easy and you don't have to worry about showing/not showing the code.

---
title: "Test"
output: 
  slidy_presentation:
    css: style.css
---
guasi
  • 1,461
  • 3
  • 12
  • The styling (only one) doesn't warrant a separate CSS file. Plus, I can wrap everything in one HTML file and email it to others. – Xikeck May 26 '22 at 12:53