RMarkdown - 更改内联代码颜色

时间:2018-06-06 18:28:47

标签: r rstudio r-markdown

我正在使用inline code in RMarkdown,我希望所有内联代码的文本都是文档中的不同颜色。在this example中,我希望heat.colors在整个文档中都是红色的。有没有办法做到这一点?

2 个答案:

答案 0 :(得分:5)

或者您可以在text_spec中使用kableExtra。它文学上做同样的事情,但只是一点点文字。查看更多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.

答案 1 :(得分:4)

您可以执行以下操作:

---
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.

得到:

enter image description here

默认模板不会在分类<span>标记中包含内联块,因此您必须手动执行此操作。你也可以做一个功能。