# How can I get Rchunks to render inside tcolorbox?

I've created a reprex to show my problem, but I am having this same issue in a custom Rmd template I am crafting.

I would like to be able to include stylized code chunks and plot outputs inside of tcolorbox environments. For some reason. When I knit the Rmarkdown, it doesn't render the tcolorbox environment.

Here is my reprex .RMD:

---
title: "Untitled"
output:
pdf_document:
extra_dependencies: ["tcolorbox", "tikz"]
---
{r, include=FALSE}
library(tidyverse)


\begin{tcolorbox}
{r,results='asis', eval=F}
iris %>%
group_by(Species) %>%
summarise(mean = mean(Sepal.Length),
sd = sd(Sepal.Width))


{r,results='asis'}
ggplot(iris, aes(Sepal.Length, Sepal.Width, colors = Species)) +
geom_point()

\end{tcolorbox}


Here is the resulting .pdf:

When I remove the Rchunks, the tcolorbox renders correctly. I opened up the intermediate .tex file to see what was going on and it looks like it's canceling out the tcolorbox with a \textbackslash{}

\textbackslash{}begin\{tcolorbox\}

\begin{Highlighting}[]
\NormalTok{iris }\OperatorTok{%>%}\StringTok{ }
\StringTok{  }\KeywordTok{group_by}\NormalTok{(Species) }\OperatorTok{%>%}\StringTok{ }
\StringTok{  }\KeywordTok{summarise}\NormalTok{(}\DataTypeTok{mean =} \KeywordTok{mean}\NormalTok{(Sepal.Length), }
\DataTypeTok{sd =} \KeywordTok{sd}\NormalTok{(Sepal.Width))}
\end{Highlighting}

\begin{Highlighting}[]
\KeywordTok{ggplot}\NormalTok{(iris, }\KeywordTok{aes}\NormalTok{(Sepal.Length, Sepal.Width, }\DataTypeTok{colors =}\NormalTok{ Species)) }\OperatorTok{+}\StringTok{ }
\StringTok{  }\KeywordTok{geom_point}\NormalTok{()}
\end{Highlighting}

\includegraphics{Untitled1_files/figure-latex/unnamed-chunk-3-1.pdf}
\textbackslash{}end\{tcolorbox\}


Without the Rchunks, it looks fine:

\begin{tcolorbox}

\end{tcolorbox}

• I don't understand this R Markdown stuff, but since you said that the generated .tex file has \textbackslash{}begin\{tcolorbox\}, I would say that your problem has nothing to do with tcolorbox (no tcolorbox environment can be started with such code). Hence, the tag should probably be removed, in my opinion. Sorry for not being able to help... – frougon Aug 25 at 21:42

\begin{tcolorbox}\tcolorbox
\end{tcolorbox}\endtcolorbox