Hide/show LaTeX environments including chunks in Rmarkdown

Viewed 118

I am trying to use Rmarkdown to create answer/solutions twin PDF documents. I embedded the solutions in a frame using the tcolorbox LaTeX package. Also, I am using LaTeX's comment package to easily toggle the solutions on and off. However, R chunks inside the comment environment are not well rendered. Here's a MWE:

---
title: "Untitled"
output: 
  pdf_document
header-includes:
  - \usepackage{tcolorbox}
  - \usepackage{comment}
  - \includecomment{comment} % toggle include / exclude
  - \newcommand{\benum}{\begin{enumerate}}
  - \newcommand{\eenum}{\end{enumerate}}
---

```{r, include=FALSE}
library(tidyverse)
```

\benum
\item The R chunk inside the \verb+comment+ environment is not rendered correctly:

\begin{comment}
\tcolorbo
```{r, echo=TRUE, eval = TRUE, results='asis'}
a <- rnorm(20)
a
```
\endtcolorbox
\end{comment}

\item But the same chunk outside the \verb+comment+ environment is rendered fine:
\tcolorbox
```{r, echo=TRUE, eval = TRUE, results='asis'}
a <- rnorm(20)
a
```
\endtcolorbox

\eenum

Here's the result: screenshot

How can I achieve what I want? Many thanks in advance!

0 Answers
Related