如何在R Markdown中高亮显示内联R代码的语法? [英] How to syntax highlight inline R code in R Markdown?

查看:329
本文介绍了如何在R Markdown中高亮显示内联R代码的语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题类似于一致的html内联代码和带有knitr的大块.我想代替.Rhtml文档,而是要突出显示R Markdown文档中的内联R代码,例如,通过 rmarkdown 编译`r "plot(cars, main = 'A scatterplot.')"`后,应突出显示诸如plot之类的标记.默认情况下,R代码块以语法突出显示,但是无法突出显示内联R代码.

This question is similar to consistent code html inline and in chunks with knitr. Instead of .Rhtml documents, I want to highlight inline R code in R Markdown documents, e.g., after `r "plot(cars, main = 'A scatterplot.')"` is compiled through rmarkdown, the tokens like plot should be highlighted. By default, R code chunks are syntax highlighted, but there is no way to highlight inline R code.

推荐答案

这里是使用开发版本更高软件包(devtools::install_github('yihui/highr'))中的a>.基本上,您只需定义自定义LaTeX命令即可突出显示令牌. highr:::cmd_pandoc_latex是LaTeX命令的数据帧,Pandoc用来进行语法突出显示.

Here is one solution using the development version of the highr package (devtools::install_github('yihui/highr')). Basically you just define your custom LaTeX commands to highlight the tokens. highr:::cmd_pandoc_latex is a data frame of LaTeX commands that Pandoc uses to do syntax highlighting.

head(highr:::cmd_pandoc_latex)
##                   cmd1 cmd2
## COMMENT  \\CommentTok{    }
## FUNCTION  \\NormalTok{    }
## IF        \\NormalTok{    }
## ELSE      \\NormalTok{    }
## WHILE     \\NormalTok{    }
## FOR       \\NormalTok{    }

然后,您可以重新定义针织衫inline钩子:

Then you can redefine the inline hook of knitr:

---
output:
  pdf_document:
    keep_tex: yes
---

```{r include=FALSE}
local({
  hi_pandoc = function(code) {
    if (knitr:::pandoc_to() != 'latex') return(code)
    if (packageVersion('highr') < '0.6.1') stop('highr >= 0.6.1 is required')
    res = highr::hi_latex(code, markup = highr:::cmd_pandoc_latex)
    sprintf('\\texttt{%s}', res)
  }
  hook_inline = knitr::knit_hooks$get('inline')
  knitr::knit_hooks$set(inline = function(x) {
    if (is.character(x) && inherits(x, 'AsIs')) hi_pandoc(x) else hook_inline(x)
  })
})
```

Test inline R code: `r I("plot(cars, main = 'A scatterplot.')")`.
Normal inline code `r pi`.

A code block:

```r
plot(cars, main = 'A scatterplot.')
1 + 2 # a comment
```

我使用I()作为方便的标记,告诉字符串是普通字符串突出显示的语法.这只是一个任意选择. PDF输出:

I used I() as a convenient marker to tell the character strings to be syntax highlighted from normal character strings. It is just an arbitrary choice. PDF output:

但这不是一个完美的解决方案.在某些情况下,您需要对其进行调整.例如,大多数特殊的LaTeX字符都不会转义,例如~.您可能需要处理hi_pandoc()gsub()返回的LaTeX代码.

This is not a perfect solution, though. You will need to tweak it in some cases. For example, most special LaTeX characters are not escaped, such as ~. You may need to process the LaTeX code returned by hi_pandoc() by gsub().

我个人认为内联输出会分散注意力,因此我不会在语法上突出显示它,但这完全是个人喜好.

Personally I find multiple colors in inline output distracting, so I would not syntax highlighting it, but this is entirely personal taste.

这篇关于如何在R Markdown中高亮显示内联R代码的语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆