RMarkdown:如何更改字体颜色?

时间:2021-11-17 19:45:59

In RMarkdown is there a way to specify the font color?

在RMarkdown中是否有指定字体颜色的方法?

There doesn't seem to be an option while browsing through the chunk options

在浏览chunk选项时,似乎没有一个选项。

5 个解决方案

#1


48  

The answer given at the link provided by @Ben Bolker:

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

如果您选择HTML (ioslides)作为输出格式,那么它将发挥作用。

However, it does not work if you select pdf (beamer) as output format. If you want to create a pdf, use LaTeX syntax:

但是,如果您选择pdf (beamer)作为输出格式,那么它将不起作用。如果您想创建pdf,请使用LaTeX语法:

    Roses are \textcolor{red}{red}, violets are \textcolor{blue}{blue}.

#2


24  

I create a function like this:

我创建了这样一个函数:

#Color Format
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
}

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.

然后,您可以像这样内联地使用它:'r colFmt(“我的红色文本”、' RED '),无论使用latex还是html文档,彩色文本都将被呈现。

#3


3  

I basically used Nicholas Hamilton's answer but because I used xtable and print, I had some problems with certain latex sequences being escaped. Namely, \\textcolor being transformed to $\backslash$textcolor. I was able to get it right by avoiding sanitizing in the following way:

我基本上用的是尼古拉斯·汉密尔顿的答案,但因为我用的是xtable和print,我在某些乳胶序列被转义时遇到了一些问题。即,\\textcolor被转换为$\反斜杠$textcolor。我可以通过以下方式避免消毒:

```{r results='asis'}
tbl = data.frame(a = letters[1:10], b = 1:10 / 10)
tbl$b = ifelse(tbl$b < 0.5, colFmt(tbl$b, "red"), colFmt(tbl$b, "green"))
print(xtable(tbl), sanitize.text.function = identity)
```

I then had to go and manually sanitize a few characters like % but at least \textcolor was correctly applied. Of course, this could be avoided by expanding your own sanitize function.

然后我必须手动清除一些字符,比如%,但至少正确地应用了\textcolor。当然,可以通过扩展自己的杀毒功能来避免这种情况。

#4


0  

This seems to work very well in both output formats, pdf and html:

这在输出格式(pdf和html)中似乎都很有效:

Roses are $\color{red}{\text{beautiful red}}$, 
violets are $\color{blue}{\text{lovely blue}}$.

Hope it helps.

希望它可以帮助。

#5


0  

Others have provided answers for output other than Word. For Word, you can use the Pandoc custom-style syntax to accomplish this with the aid of a reference word document. First, inside your reference.docx template, create a new Word style with a short, distinct name. If you want your font color to apply to a full paragraph, you can use the default, “Linked Paragraph and Character” style type. If you only want to emphasize some words in a paragraph with color, you need to select the “Character” style type. Change the font color (and any other styling you need) and save the reference.docx file.

其他人则提供了除Word之外的输出答案。对于Word,您可以使用Pandoc定制样式的语法,通过参考Word文档来实现这一点。首先,在你参考。docx模板,创建一个具有简短、独特名称的新单词样式。如果您希望字体颜色应用于完整的段落,您可以使用默认的“链接段落和字符”样式。如果你只想在一个有颜色的段落中强调一些词,你需要选择“字符”样式类型。更改字体颜色(以及您需要的任何其他样式)并保存引用。多克斯文件。

Then, inside your .Rmd file, you can use the tag as follows:

然后,在.Rmd文件中,您可以使用以下标签:

<div custom-style=“DivCol”>Whole paragraph of colored text</div>

Just a <span custom-style=“SpanCol”>few words</span> of colored text

A word regarding style names - for reasons I don’t understand, this process did not work with the style name “Span_Add” but “SpanAdd” was fine.

关于样式名的一个词——出于我不理解的原因,这个过程没有使用样式名“Span_Add”,但是“SpanAdd”就可以了。

#1


48  

The answer given at the link provided by @Ben Bolker:

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

如果您选择HTML (ioslides)作为输出格式,那么它将发挥作用。

However, it does not work if you select pdf (beamer) as output format. If you want to create a pdf, use LaTeX syntax:

但是,如果您选择pdf (beamer)作为输出格式,那么它将不起作用。如果您想创建pdf,请使用LaTeX语法:

    Roses are \textcolor{red}{red}, violets are \textcolor{blue}{blue}.

#2


24  

I create a function like this:

我创建了这样一个函数:

#Color Format
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
}

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.

然后,您可以像这样内联地使用它:'r colFmt(“我的红色文本”、' RED '),无论使用latex还是html文档,彩色文本都将被呈现。

#3


3  

I basically used Nicholas Hamilton's answer but because I used xtable and print, I had some problems with certain latex sequences being escaped. Namely, \\textcolor being transformed to $\backslash$textcolor. I was able to get it right by avoiding sanitizing in the following way:

我基本上用的是尼古拉斯·汉密尔顿的答案,但因为我用的是xtable和print,我在某些乳胶序列被转义时遇到了一些问题。即,\\textcolor被转换为$\反斜杠$textcolor。我可以通过以下方式避免消毒:

```{r results='asis'}
tbl = data.frame(a = letters[1:10], b = 1:10 / 10)
tbl$b = ifelse(tbl$b < 0.5, colFmt(tbl$b, "red"), colFmt(tbl$b, "green"))
print(xtable(tbl), sanitize.text.function = identity)
```

I then had to go and manually sanitize a few characters like % but at least \textcolor was correctly applied. Of course, this could be avoided by expanding your own sanitize function.

然后我必须手动清除一些字符,比如%,但至少正确地应用了\textcolor。当然,可以通过扩展自己的杀毒功能来避免这种情况。

#4


0  

This seems to work very well in both output formats, pdf and html:

这在输出格式(pdf和html)中似乎都很有效:

Roses are $\color{red}{\text{beautiful red}}$, 
violets are $\color{blue}{\text{lovely blue}}$.

Hope it helps.

希望它可以帮助。

#5


0  

Others have provided answers for output other than Word. For Word, you can use the Pandoc custom-style syntax to accomplish this with the aid of a reference word document. First, inside your reference.docx template, create a new Word style with a short, distinct name. If you want your font color to apply to a full paragraph, you can use the default, “Linked Paragraph and Character” style type. If you only want to emphasize some words in a paragraph with color, you need to select the “Character” style type. Change the font color (and any other styling you need) and save the reference.docx file.

其他人则提供了除Word之外的输出答案。对于Word,您可以使用Pandoc定制样式的语法,通过参考Word文档来实现这一点。首先,在你参考。docx模板,创建一个具有简短、独特名称的新单词样式。如果您希望字体颜色应用于完整的段落,您可以使用默认的“链接段落和字符”样式。如果你只想在一个有颜色的段落中强调一些词,你需要选择“字符”样式类型。更改字体颜色(以及您需要的任何其他样式)并保存引用。多克斯文件。

Then, inside your .Rmd file, you can use the tag as follows:

然后,在.Rmd文件中,您可以使用以下标签:

<div custom-style=“DivCol”>Whole paragraph of colored text</div>

Just a <span custom-style=“SpanCol”>few words</span> of colored text

A word regarding style names - for reasons I don’t understand, this process did not work with the style name “Span_Add” but “SpanAdd” was fine.

关于样式名的一个词——出于我不理解的原因,这个过程没有使用样式名“Span_Add”,但是“SpanAdd”就可以了。