Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RMarkdown: How to change the font color?

Tags:

r

r-markdown

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

like image 453
xiaodai Avatar asked Mar 15 '15 23:03

xiaodai


People also ask

Can you change color in markdown?

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.

How do you change the font color in a script?

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.

How do you Bolden text in R markdown?

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.


2 Answers

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}. 
like image 118
Nadja Simons Avatar answered Sep 29 '22 14:09

Nadja Simons


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.

like image 32
Nicholas Hamilton Avatar answered Sep 29 '22 12:09

Nicholas Hamilton