3

I have this r markdown code:

---
header-includes:
   - \usepackage{xcolor, colortbl}
output:
    pdf_document:
      latex_engine: xelatex

---



```{r, results="asis"}

library(xtable)
library(formattable)
# Your data
tab = data.frame(category = c("A","B","C"), groupA = c(.2,.3,.5), groupB= c(.6,.7,.9))

# Function to cut your data, and assign colour to each range
f <- function(x) cut(x, c(0, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, Inf), 
                      labels=c("green", "red", "blue", "orange", "yellow", "purple", "brown", "white"),
                      include.lowest = FALSE, right = TRUE)

# Apply function to columns: this overwrites your data
tab[c("groupA", "groupB")] <- lapply(tab[c("groupA", "groupB")], function(x)
                                            paste0("\\cellcolor{", f(x), "}", x))
# Sanitise output 
print(xtable(tab), sanitize.text.function = identity)
```

I need to color code the cells of the table.

I am getting this error:

output file: test.knit.md

! LaTeX Error: Missing \begin{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.1 <

pandoc.exe: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"C:/Program Files/RStudio/bin/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\e598140\Documents\R\win-library\3.2\rmarkdown\rmd\latex\default.tex" --highlight-style tango --latex-engine xelatex --variable graphics=yes --variable "geometry:margin=1in"' had status 43 
Execution halted

Any ideas what is the issue here?

G5W
  • 36,531
  • 10
  • 47
  • 80
user1471980
  • 10,127
  • 48
  • 136
  • 235
  • 2
    I'm also unable to reproduce the error. – Benjamin Jun 05 '17 at 16:14
  • any ideas what I may be missing here? – user1471980 Jun 05 '17 at 16:15
  • Are you running the latest version of Rmarkdown? – Dan Jun 05 '17 at 16:16
  • running rmarkdown_1.5 – user1471980 Jun 05 '17 at 16:19
  • Isn't it a duplicate? – M-- Jun 07 '17 at 20:22
  • 1
    Just remove `: latex_engine: xelatex` after `pdf_document` and it will use pdflatex instead. – HubertL Jun 07 '17 at 22:42
  • @HubertL, that's what I had originally, not working either. – user1471980 Jun 08 '17 at 13:00
  • Any ideas what I am missing here? I really need the colors working on the table. – user1471980 Jun 08 '17 at 13:28
  • @user1471980 ; did you read / try anything suggested at the links in the comments at your other question https://stackoverflow.com/questions/44335165/how-do-you-change-colors-of-certain-cells-in-xtable-markdown#comment75841332_44335165 . It would definitely help if you added the details of what you tried, and what didnt work to your question to save people offering repeat suggestions. – user20650 Jun 08 '17 at 13:50
  • 1
    It works fine for me. Suggestion: 1. Make sure you can build a simple file, e.g. the default one from "File | New file | R Markdown | PDF". If that works, then add stuff to it from the bad one until the problem appears. If not, then the problem is with your RStudio/LaTeX setup. – user2554330 Jun 08 '17 at 19:58
  • @user2554330, it works until I need to color code the cells in table. Then it stops working. – user1471980 Jun 14 '17 at 18:24
  • @user1471980, which line does that? – user2554330 Jun 15 '17 at 18:46
  • @user2554330 print(xtable(tab), sanitize.text.function = identity) – user1471980 Jun 15 '17 at 19:14
  • I don't see anything about colors in that line. – user2554330 Jun 15 '17 at 20:16
  • @user2554330, if I add this code to top: "header-includes: - \usepackage{xcolor, colortbl}", it gives me error. When I take it out, it produceses, the pdf file. It looks like it has something to do with the color. Any ideas? – user1471980 Jun 19 '17 at 16:33
  • 1
    That makes no sense. Without that line, you should get an error because the `\cellcolor` macro would not be defined. (In a file exactly like the current version of your main example, I get no errors.) – user2554330 Jun 19 '17 at 18:01
  • @user2554330 this is what I see in print(xtable) output: \cellcolor{red}{4.4535}, no color codes – user1471980 Jun 19 '17 at 19:24

0 Answers0