在HTML和PDF输出中指定颜色 [英] Specifying colour in HTML and PDF output
问题描述
Nicholas Hamilton的答案指定如何使用R表达式在Markdown的PDF和HTML输出中使用彩色文本.
如果我创建一个RMarkdown文档,我不会感到高兴,警告消息是
colFmt("MY RED TEXT","red")中的错误:找不到对象'opts_knit'调用:... inline_exec-> hook_eval-> withVisible-> eval-> eval-> colFmt执行停止
我想念什么?
复制并粘贴以下RMarkdown:
---标题:测试色"作者:"mbn"输出:html_document---```{r setup,include = FALSE}knitr :: opts_chunk $ set(echo = TRUE)```## R降价这是R Markdown文件.```{r汽车}colFmt = function(x,color){outputFormat = opts_knit $ get("rmarkdown.pandoc.to")if(outputFormat =='乳胶')paste("\\ textcolor {",color,} {",x,}",sep =")否则if(outputFormat =='html')paste(< font color ='",color,'>",x,</font>",sep =")别的X}```##现在测试颜色`r colFmt(我的红色文字",'红色')
将 opts_knit $ get
更改为 knitr :: opts_knit $ get
,您的代码应该可以正常工作.>
请参见 https://stat.ethz.ch/R-manual/R-devel/library/base/html/ns-dblcolon.html
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')`
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
这篇关于在HTML和PDF输出中指定颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!