RStudio Knitr 无法将 Rmd 转换为 PDF (Windows 7)

RStudio Knitr failed to convert Rmd to PDF (Windows 7)

我想使用 RStudio(版本 0.99.903)和 knitr 来转换 .Rmd 文件(请见下文)到 PDF 文件。

---
title: "test"
output: pdf_document
---

Roses are \textcolor{red}{red}, violets are \textcolor{blue}{blue}.

当我 运行 "Knit PDF" 时,我收到以下错误消息:

processing file: test.Rmd
output file: test.knit.md

! Undefined control sequence.
l.87 Roses are \textcolor

pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"C:/PROGRA~2/Pandoc/pandoc" +RTS -K512m -RTS test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.pdf --template "C:\Users\fuq\R\R-3.3.1\library\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"' had status 43 
Execution halted

但是,如果我在该 .Rmd 文件中的任何位置添加 R 代码块,例如:

---
title: "test"
output: pdf_document
---

Roses are \textcolor{red}{red}, violets are \textcolor{blue}{blue}.

```{r}
summary(cars)
```

然后我可以成功获得我期望的 PDF。

但奇怪的是,如果在上面的.Rmd文件中设置echo=FALSE(见下文)

---
title: "test"
output: pdf_document
---

Roses are \textcolor{red}{red}, violets are \textcolor{blue}{blue}.

```{r, echo=FALSE}}
summary(cars)
```

然后又,我得到了同样的错误信息:

! Undefined control sequence.
l.87 Roses are \textcolor

pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"C:/PROGRA~2/Pandoc/pandoc" +RTS -K512m -RTS test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.pdf --template "C:\Users\fuq\R\R-3.3.1\library\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"' had status 43 
Execution halted

我在 Windows 7 64 位上工作,并获得了最新版本的 MiKTex。请问谁能告诉我出了什么问题吗?

您必须包含包 color,然后它才能工作。我不知道为什么这只适用于其中的 R 块。但是这个解决方案将解决这个问题:

---
title: "test"
output: pdf_document
header-includes: \usepackage{color}
---


Roses are \textcolor{red}{red}, violets are \textcolor{blue}{blue}.

如果你想编织颜色到HTML,你必须使用这个代码:

---
title: "test"
output: html_document
---

Roses are <span style="color:red">red</span>, 
violets are <span style="color:blue">blue</span>.