In RMarkdown is there a way to specify the font color?
There doesn't seem to be an option while browsing through the chunk options
Markdown doesn't support color but you can inline HTML inside Markdown, e.g.: <span style="color:blue">some *blue* text</span>. As the original/official syntax rules state (emphasis added): Markdown's syntax is intended for one purpose: to be used as a format for writing for the web.
To change the font color of a text, use the fontcolor() method. This method causes a string to be displayed in the specified color as if it were in a <font color="color"> tag.
To make the formatted text into bold type, you can simply use a pair of ** around the marked up text with no space. For example **bold** in the . Rmd file generates bold in the output document.
The answer given at the link provided by @Ben Bolker:
Roses are <span style="color:red">red</span>, violets are <span style="color:blue">blue</span>.
does work if you select HTML (ioslides) as the output format.
However, it does not work if you select pdf (beamer) as output format. If you want to create a pdf, use LaTeX syntax:
Roses are \textcolor{red}{red}, violets are \textcolor{blue}{blue}.
I create a function like this:
## Color Format colFmt <- function(x,color) { outputFormat <- knitr::opts_knit$get("rmarkdown.pandoc.to") if(outputFormat == 'latex') { ret <- paste("\\textcolor{",color,"}{",x,"}",sep="") } else if(outputFormat == 'html') { ret <- paste("<font color='",color,"'>",x,"</font>",sep="") } else { ret <- x } return(ret) }
Then you can use it inline like this:`r colFmt("MY RED TEXT",'red')`
, and colored text will be rendered regardless of whether working on latex or HTML document.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With