Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RMarkdown - Change Inline Code Color

I am using inline code in RMarkdown and I would like all the text that is a result of inline code to be a different color in the document. In this example, I would like heat.colors to be red all over the document. Is there a way to do this?

like image 976
Camila Vargas Restrepo Avatar asked Jun 06 '18 18:06

Camila Vargas Restrepo


People also ask

How do I change colors in R Markdown?

The Markdown syntax has no built-in method for changing text colors. We can use HTML and LaTeX syntax to change the formatting of words: For HTML, we can wrap the text in the <span> tag and set color with CSS, e.g., <span style="color: red;">text</span> . For PDF, we can use the LaTeX command \textcolor{}{} .

How do I change a line in R Markdown?

Add Line Breaks in R Markdown To break a line in R Markdown and have it appear in your output, use two trailing spaces and then hit return .

How do you use 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.


2 Answers

Or you can use text_spec in kableExtra. It literarily does the same thing but just a tiny bit more literal. See more here

---
title: ''
output: html_document
---

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

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

This is inline code: `r text_spec(colnames(mtcars)[1], color = "red")`.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

### This is more inline code `r text_spec(colnames(mtcars)[2], color = "red")`.

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
like image 196
Hao Avatar answered Oct 01 '22 08:10

Hao


You can do something like:

---
title: ''
output: html_document
---

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

```{css echo=FALSE}
.custom-inline {
  color: red;
  font-weight: 700
}
```
## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

This is inline code: `r sprintf("<span class='custom-inline'>%s</span>", colnames(mtcars)[1])`.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

### This is more inline code `r sprintf("<span class='custom-inline'>%s</span>", colnames(mtcars)[2])`.

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

to get:

enter image description here

The default templates do not wrap inline chunks in a classed <span> tag so you have to do it manually. You can make a function to do it, too.

like image 35
hrbrmstr Avatar answered Oct 01 '22 06:10

hrbrmstr