mikeqfu mikeqfu - 2 months ago 58
R Question

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

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?

J_F J_F
Answer

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>.
Comments