I want to create an R/exams exercise (in PDF format) where the R code is highlighted. I tried to use the highlight=TRUE
option in the code chunk, but it produced an error when I tried to generate the PDF with the exams::exams2pdf
function.
Using the following example exercise (i.e., example.Rmd
):
Question
========
Some question with code:
```{r echo=TRUE, warning=FALSE, eval=FALSE, highlight=TRUE}
library("blavaan")
HS.model <- 'visual =~ x1 + prior("normal(1,1)")*x2 + x3
verbal =~ x4 + x5 + x6 '
bfit <- bcfa(model = HS.model,
data = HolzingerSwineford1939,
dp = dpriors(lambda = "normal(1,5)"),
burnin = 500,
sample = 500,
n.chains = 4)
```
Meta-information
================
exname: some_ex_name
extype: string
exsolution: nil
When I try:
exams::exams2pdf("example.Rmd")
it trows an error:
! LaTeX Error: Environment Shaded undefined.
The exams2pdf()
function embeds the exercises into a master LaTeX template, by default plain.tex
. As this does not provide the LaTeX {Shaded}
environment that knitr
will produce for chunks with highlight = TRUE
you get the error.
The solution is to use a template that includes a suitable environment. To demonstrate how this works, R/exams ships the plain-highlight.tex
template. I recommend you adapt this into your own template that you use.
With the demo template your example works:
exams2pdf("example.Rmd", template = "plain-highlight")