1

An answer by Nicholas Hamilton specifies how to use colour text in PDF and HTML output from Markdown using an R expression.

If I create an RMarkdown document, I get no joy, Warning message is

Error in colFmt("MY RED TEXT", "red") : object 'opts_knit' not found Calls: ... inline_exec -> hook_eval -> withVisible -> eval -> eval -> colFmt Execution halted

What am I missing?

Copy and paste of RMarkdown below:

---
title: "test colour"
author: "mbn"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. 

```{r cars}
colFmt = function(x,color){
  outputFormat = opts_knit$get("rmarkdown.pandoc.to")
  if(outputFormat == 'latex')
    paste("\\textcolor{",color,"}{",x,"}",sep="")
  else if(outputFormat == 'html')
    paste("<font color='",color,"'>",x,"</font>",sep="")
  else
    x
}
```

## Test colour now

`r colFmt("MY RED TEXT",'red')`
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Mark Neal
  • 996
  • 16
  • 52

2 Answers2

1

Change opts_knit$get to knitr::opts_knit$get and your code should work.

See https://stat.ethz.ch/R-manual/R-devel/library/base/html/ns-dblcolon.html

Kenji
  • 227
  • 1
  • 10
  • 1
    Of course! Using an include statement in setup chunk also works: `library(knitr)` – Mark Neal Mar 19 '19 at 21:23
  • 1
    Also, if you want to use hex numbers for colours, rather than the limited number of common named colours, modify colFmt function to this: `if(outputFormat == 'latex') paste("\\textcolor[HTML]{",color,"}{",x,"}",sep="")` and use commands like this: `r colFmt("My colored text favorite green latex and html",'7ac143')` – Mark Neal Mar 20 '19 at 20:50
  • 1
    You'll also need (I think) in the YAML `header-includes:` `\usepackage[usenames,dvipsnames]{xcolor}'` – Mark Neal Mar 20 '19 at 21:56
  • @MarkNeal Thank you very much for your great suggestions! How would I have to modify your code if I would like to use predefined colors, e.g. `color_positive <- "orange"; color_negative <- "lightblue"`? (I predefined them at the beginning of my file and use them throughout figures and tables) – mavericks Sep 02 '20 at 15:45
  • 1
    Here is a workaround for those who would like @MarkNeal's solution for coloring text which works both in PDF/HTML while defining colors globally: in an R chunk `my_orange <- "orangered1"`=> `library(gplots)` to be able to use `col2hex()` to convert color names to hex RGB strings `hex_my_orange <- str_remove(col2hex(my_orange),"#")` => and then within the main text `r colFmt("Some orange stuff",hex_my_orange)` – mavericks Sep 03 '20 at 07:33
1

Here is an example of an rmarkdown code that is self contained and works, and uses hex colour ids to give consistent colours across pdf and html. Thanks to contributions from Kenji for pointing out I needed knitr library.

---
title: "test colour"
author: "mbn"
output: html_document
#output: pdf_document
header-includes:
  \usepackage[usenames,dvipsnames]{xcolor}
---

```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = TRUE)

```

## R Markdown

This is an R Markdown document. 

```{r cars}
colFmt = function(x,color){
  outputFormat = opts_knit$get("rmarkdown.pandoc.to")
  if(outputFormat == 'latex')
    paste("\\textcolor[HTML]{",color,"}{",x,"}",sep="")
  else if(outputFormat == 'html')
    paste("<font color='",color,"'>",x,"</font>",sep="")
  else
    x
}
```

## Test colour now

`r colFmt("My colored text favorite green latex/pdf and html",'7ac143')`
Mark Neal
  • 996
  • 16
  • 52
  • `r colFmt("Some text without underscores",'7ac143')` works well, but did not allow to use special characters such as "_" within the colored text for me – mavericks Sep 03 '20 at 07:38
  • Did you try to work with transparent colors also? I tried to adapt your code for this but did not yet succeed. Thanks for any advice! See my [SO question here](https://stackoverflow.com/questions/64694618/r-markdown-specifying-transparent-color-in-html-and-pdf-output) – mavericks Nov 05 '20 at 09:44