Reputation: 17183
In an R/Markdown document that I want to convert to LaTeX I want to set knitr
options so that the entire document uses plain code chunks for all code inputs by default. Thus, for a .Rmd document with
```{r}
x <- 1 + 1
```
I want to obtain the output
```
x <- 1 + 1
```
I had hoped that the highlight=FALSE
option could be used for this but this generates text-chunks rather than plain chunks. More precisely, for the simple example above, knit()
produces an R-chunk by default (i.e., with highlight = TRUE
):
```r
x <- 1 + 1
```
After setting knitr::opts_chunk$set(highlight = FALSE)
a text-chunk is produced:
```text
x <- 1 + 1
```
But what I would like to have a plain chunk without any special language, see above.
I can obtain what I want via
knitr::opts_chunk$set(highlight = TRUE, lang = "")
Thus, I do enable highlighting but set the lang
to an empty string. This indeed yields the plain code I want to have.
There is at least one disadvantage, though (apart from the rather hacky feel of this solution). Namely, if in the same document I do want to enable highlighting in the options of one specific chunk, I have to set lang = "r"
now instead of highlight = TRUE
, e.g.,
```{r, lang="r"}
x <- 1 + 1
```
So I wonder whether there is a better solution for this?
In older versions of pandoc
(I tried 2.9.x) text-chunks were converted to {verbatim}
code chunks in LaTeX output.
However, more recent versions of pandoc
(I tried 2.17.x) text-chunks are converted to {Shaded}
instead and only plain chunks are converted to {verbatim}
.
Upvotes: 1
Views: 268
Reputation: 30114
Another hacky solution is (I don't even dare to explain it):
```{r}
knitr::opts_hooks$set(highlight = function(options) {
if (!options$highlight) {
options$highlight = TRUE
options$lang = ''
}
options
})
knitr::opts_chunk$set(highlight = FALSE)
```
```{r}
1 + 1
```
```{r, highlight=TRUE}
2 + 2
```
The language name text
when highlight = FALSE
is currently hard-coded in knitr, so you cannot change it: https://github.com/yihui/knitr/blob/907184f82/R/hooks-md.R#L171 I'm open to making it configurable, but I'm not sure how (I hope not to add a new chunk option for setting the language in the case of highlight = FALSE
).
Upvotes: 1
Reputation: 537
I'm not sure I entirely understand what you want, but does this in the yaml header help?
---
output:
html_document:
theme: null
highlight: null
---
Upvotes: 1