Is there a way to enable syntax highlighting in code chunks of R markdown documents that are rendered as an ioslides_presentation?
In the markdown reference cheatsheet is written that the YAML argument highlight
does not work for ioslides. So I was looking for a way to enable syntax highlighting by an customized css file but I haven't found any solution for this since I'm not familiar with css.
Any help would be appreciated. Here's a short reproducible example:
---
title: "example"
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Slide with R Output
```{r cars, echo = TRUE, eval = FALSE}
# cars' summary as an example for a comment
summary(cars)
# example for integers
cars[,1]
```
If you navigate to your R install library folder, you should be able to go to (note that your version number may vary):
rmarkdown > rmd > ioslides > ioslides-13.5.1 > theme > css
to find the 'default.css' file. In there, you can scroll down to the /*Pretty print */
comment. Underneath, you should see a bunch of arguments that start with .prettyprint
. Your best bet may be to copy those into a new custom .css file so you can play around a little until you get the highlighting you're after. For example, I made a file called slides.css
and put it in the same folder as my markdown document. Then, I copied in these css arguments and just modified the colors:
/* Pretty print */
/* line 600, ../scss/default.scss */
.prettyprint .com {
/* a comment */
color: green;
font-style: italic;
}
/* line 604, ../scss/default.scss */
.prettyprint .lit {
/* a literal value */
color: black;
}
/* line 609, ../scss/default.scss */
.prettyprint .pun,
.prettyprint .opn,
.prettyprint .clo {
color: red;
}
/* line 618, ../scss/default.scss */
.prettyprint .pln {
color: blue;
}
Then, with my ioslides file:
---
title: "Ioslides check"
output:
ioslides_presentation:
css: slides.css
---
##
```{r}
# cars[,1] as an example for a comment
head(cars)
cars[1:5, 1]
```
My output looks like
If you want to figure out which elements you actually want to modify (for example, if you instead would like to change the look of your code), you can use the inspect function of a browser (CTRL + SHIFT + I in chrome) to highlight the elements of your ioslides output to see to which class they belong. For example, when I highlight the results output, it shows me that that can be modified by playing with the pre
(preformatted text) tag. If I therefore add a color argument to pre
:
pre {
font-family: 'Source Code Pro', 'Courier New', monospace;
font-size: 20px;
color: pink;
line-height: 28px;
padding: 10px 0 10px 60px;
letter-spacing: -1px;
margin-bottom: 20px;
width: 106%;
left: -60px;
position: relative;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
/*overflow: hidden;*/
}
I can change the results output as well:
but keep in mind that many of the classes above are also contained within the pre
tag, so if you make modifications to pre
that are not overridden by a child class, you can get some unintended results. For example, if I change the font-size
of the pre
elements:
/* line 337, ../scss/default.scss */
pre {
font-family: 'Source Code Pro', 'Courier New', monospace;
font-size: 50px;
line-height: 28px;
padding: 10px 0 10px 60px;
letter-spacing: -1px;
margin-bottom: 20px;
width: 106%;
left: -60px;
position: relative;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
/*overflow: hidden;*/
}
but only change the font-size of some of the child elements
/* line 604, ../scss/default.scss */
.prettyprint .lit {
/* a literal value */
color: black;
font-size: 20px;
}
my output will be wonky: