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

对着背影说爱祢 提交于 2020-01-15 09:37:22

问题


I wanted to use RStudio (Version 0.99.903) and knitr to convert a .Rmd file (please see below) to a PDF file.

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

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

When I run "Knit PDF" I got the following error message:

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

However, if I add a R code chunk anywhere in that .Rmd file, for example:

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

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

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

I could then successfully get a PDF that I expected.

But what was weird was that if set echo=FALSE in the above .Rmd file (see below)

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

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

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

Then again, I got the same error message:

! 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

I worked on Windows 7 64bit, and have got the latest version of MiKTex. Please could anyone give me some idea what was going wrong?


回答1:


You have to include the package color, then it works. I don't know why this works just with an R chunk in it. But this solution will solve this problem:

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


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

If you want to knit colors to HTML, you have to use this code:

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

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


来源:https://stackoverflow.com/questions/39530362/rstudio-knitr-failed-to-convert-rmd-to-pdf-windows-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!