Code chunk background LaTeX

I am having trouble getting the background color of code chunks to change when I knit to PDF. My understanding of the code chunk options is that setting the background option should allow me to specify the background shading for code. However, it does not seem to work. MWE below. In the example I'm trying to set the background globally and locally. Neither seems to make a difference.

---
output: 
  pdf_document:
    keep_tex: true
---

```{r}
knitr::opts_chunk$set(background = c(.1, 0, 0))
``

```{r cars-1}
summary(cars)
``

```{r cars-2, background=c(0,.1,0)}
summary(cars)
``

(Note that missing backticks in code chunks is deliberate and necessary to post on the site. My actual code has properly delimited chunks.)

The .tex file does not seem to do anything with the background options. From the .tex file, I see:

[LaTeX stuff]

\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}

[more LaTeX stuff]

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{summary}\NormalTok{(cars)}
\end{Highlighting}
\end{Shaded}

I'm using tinytex (which is fantastic!) on Ubuntu 18.04.

1 Like

The problem goes away if I use .Rnw rather than .Rmd. I'm looking through the code, but any notion about why the two file formats are treated differently with respect to this option?

OK, I see now that this is in the docs: https://yihui.name/knitr/options/#code-evaluation. Background and size only work in .Rnw.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.