RMarkdown-更改内联代码颜色

Cam*_*epo 3 r rstudio r-markdown

在RMarkdown中使用内联代码,并且我希望所有由于内联代码而导致的文本在文档中使用不同的颜色。在此示例中,我希望heat.colors在整个文档中都为红色。有没有办法做到这一点?

hrb*_*str 7

您可以执行以下操作:

---
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.
Run Code Online (Sandbox Code Playgroud)

要得到:

在此处输入图片说明

默认模板不会将内联块包装在分类<span>标签中,因此您必须手动进行。你也可以创建一个函数来做到这一点。


Hao*_*Hao 5

或者您可以text_spec在中使用kableExtra。从字面上看,它做同样的事情,但只是字面意思。在这里查看更多

---
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.
Run Code Online (Sandbox Code Playgroud)