有没有办法在rmarkdown中使用自定义突出显示样式?

手册对此没有任何说明,最接近的是为所有内容制作一个完整的自定义css文件,但这仅适用于html_document而不适用于pdf_document(请参阅https://bookdown.org/yihui/rmarkdown/html-document.html#appearance-and-style)

Pandoc的较新版本支持以下功能:
http://pandoc.org/MANUAL.html#syntax-highlighting

但是,如果指定了除默认的pandoc样式之一以外的任何其他内容,rmarkdown会引发错误。

例如,当我从Highlight.js库下载zenburn.css时,对其进行修改并想要使用它:

```
title: Some title
output:
    html_document:
        theme: readable
        highlight: zenburn.css
```

我得到:

match.arg(highlight,html_highlighters())中的错误:
“arg”应为“默认”,“探戈”,“色素”,“kate”,“单色”,“浓缩咖啡”,“zenburn”,“黑线鳕”,“文本伴侣”之一
调用:...-> pandoc_html_highlight_args-> match.arg
执行停止

最佳答案

至少对于HTML文档,您可以使用css YAML选项简单地包括自定义样式:

---
title: Some title
output:
    html_document:
        theme: readable
        css: zenburn.css
---

关于PDF文档,您可以检查中间TeX文件。在那里,您会发现一系列类似于
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.96,0.35,0.01}{\textit{#1}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.93,0.29,0.53}{\textbf{#1}}}

这些是定义代码突出显示的行。例如,第一个定义注释的颜色。您可以编写一个header.tex,在其中使用\renewcommand重新定义这些命令
\renewcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\renewcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}

并在正文之前将其包含在文档中,位置为

这是一个示例,其中我们更改了正文中注释和关键字的突出显示:
---
title: Some title
output:
  pdf_document:
    keep_tex: true
---

```{r}
# This is a test
head(mtcars)
```
\renewcommand{\CommentTok}[1]{\textcolor[rgb]{0.96,0.35,0.01}{\textit{#1}}}
\renewcommand{\KeywordTok}[1]{\textcolor[rgb]{0.93,0.29,0.53}{\textbf{#1}}}
```{r}
# This is a test
head(mtcars)
```

r - rmarkdown中的自定义突出显示样式-LMLPHP

07-24 09:45
查看更多