将R Markdown转换为pdf时的受防护代码块 [英] fenced code blocks when converting R Markdown to pdf

查看:161
本文介绍了将R Markdown转换为pdf时的受防护代码块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将R Markdown文档编译为HTML文档时,非R代码块的格式很好.但是,当我将以下R Markdown文档编译为pdf时,唯一添加的代码格式是在字体中.没有阴影,围栏,突出显示等.

When I compile an R Markdown document to an HTML document, the non-R code blocks are formatted nicely. But when I compile the following R Markdown document to pdf, the only added code formatting is in the font. There is no shading, fencing, highlighting, etc.

---
output: pdf_document
---

```
code
```

我不想对输出进行微管理,我只想添加一些常识性格式即可将代码与散文清晰地分开.我在具有以下引擎的MAc上使用TeXShop.

I don't want to micromanage the output, I just want to add some common-sense formatting to clearly separate the code from the prose. I'm using TeXShop on a MAc with the engine below.

#!/bin/bash
/Library/Frameworks/R.framework/Versions/Current/Resources/bin/Rscript -e "rmarkdown::render(\"$1\", encoding='UTF-8')"

推荐答案

使用```,您将引入普通的markdown代码块,但不会引入knitr代码块.但是您期望的输出(围栏,突出显示,阴影)是knitr样式添加到其代码块中的样式.

With ``` you introduce a plain markdown code block but not a knitr code chunk. But the output you expect (fencing, highlighting, shading) is the style knitr adds to its code blocks.

因此,请使用```{r}将代码包装在knitr块中(如果不想评估代码,请使用eval = FALSE).这也可以用于非R代码块:只要不对代码求值,语言就没有关系.

Therefore, use ```{r} to wrap code in knitr chunks (use eval = FALSE if you don't want the code to be evaluated). This can also be used for non-R code blocks: As long as the code is not evaluated, the language doesn't matter.

但是,对于非R代码,这将导致错误或缺少语法突出显示.要获得正确的语法高亮显示,请使用选项 engine(如果该语言属于支持的语言引擎.

However, for non-R code this will lead to wrong or missing syntax highlighting. To get the correct syntax highlighting, use the option engine if the language is among the supported language engines.

下面的示例显示了痛苦减价块,一个经过评估和未经评估的R代码块,一个(未经评估的)Python块而没有突出显示,最后是两个(未经评估的)Python块具有正确的突出显示.

The example below shows a pain markdown block, an evaluated and and unevaluated R code chunk, a (unevaluated) Python chunk without highlighting and finally two (unevaluated) Python chunks with correct highlighting.

---
output:
  pdf_document
---

```
Plain markdown code block.
```


```{r}
print("This is a knitr code chunk.")
```

```{r, eval = FALSE}
print("This is a knitr code chunk that isn't evaluated.")
```


Chunk with Python code (borrowed from http://stackoverflow.com/q/231767/2706569), *wrong* (no) highlighting:
```{r, eval = FALSE}
if self._leftchild and distance - max_dist < self._median:
      yield self._leftchild
```

Chunk with Python code, *correct* highlighting:
```{r, eval = FALSE, engine = "python"}
if self._leftchild and distance - max_dist < self._median:
      yield self._leftchild
```

Chunk with Python code, *correct* highlighting (alternative notation):
```{python, eval = FALSE}
if self._leftchild and distance - max_dist < self._median:
      yield self._leftchild
```

这篇关于将R Markdown转换为pdf时的受防护代码块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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