rmarkdown 中的自定义高亮样式 [英] Custom highlighting style in rmarkdown

查看:86
本文介绍了rmarkdown 中的自定义高亮样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 rmarkdown 中使用自定义高亮样式?

手册对此有点沉默,最接近的是为所有内容制作一个完整的自定义 css 文件,但这仅适用于 html_document 而不适用于 pdf_document(请参阅

Is there a way to use a custom highlighting style in rmarkdown?

Manual is a bit silent regarding that and the closest to that is to make a full blown custom css file for everything, that would however work only for html_document and not for pdf_document (see https://bookdown.org/yihui/rmarkdown/html-document.html#appearance-and-style )

The newer versions of Pandoc support this: http://pandoc.org/MANUAL.html#syntax-highlighting

but when anything else but one of the default pandoc styles is specified, rmarkdown throws an error.

For example, when I download zenburn.css from highlight.js library, modify it and want to use it:

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

I get:

Error in match.arg(highlight, html_highlighters()) : 'arg' should be one of "default", "tango", "pygments", "kate", "monochrome", "espresso", "zenburn", "haddock", "textmate" Calls: ... -> pandoc_html_highlight_args -> match.arg Execution halted

解决方案

At least for HTML documents, you can simply include your customized styles using the css YAML option:

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

Concerning PDF documents, you could check the intermediate TeX file. There you will find a block of commands that look like

\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}}}

These are the lines that define the code highlighting. The first one for example defines the color for comments. You could write a header.tex in which you redefine these commands using \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}}} 

and include it in your document right before the body.

Here is an example in which we alter the highlighting of comments and keywords within the body:

---
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)
```

这篇关于rmarkdown 中的自定义高亮样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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