4

Booktabs and xtable fail with Rmarkdown using RStudio. The Tufte template works fine, but using a standard .Rmd file fails with the error message:

! Undefined control sequence.
l.133   \toprule

pandoc.exe: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43

A reproducible example:

---
title: "Testing booktabs"
author: "r.bot"
date: "11 June 2015"
output: pdf_document
---

```{r, results='asis'}
library(xtable)
options(xtable.comment = FALSE)
options(xtable.booktabs = TRUE)
xtable(head(mtcars[,1:6]), caption = "First rows of mtcars")
```

R version 3.1.2 (2014-10-31) Platform: x86_64-w64-mingw32/x64 (64-bit) Latest version of Pandoc installed.

r.bot
  • 5,309
  • 1
  • 34
  • 45

1 Answers1

13

Try including a file header.tex in which you depend on the booktabs package. See the latex options for rmarkdown for details.

Edit: While the inclusion of headers is convenient and allows for wider ranging changes, here we get by with the top-level options tables: true. So the minimal change for the example above is

---
title: "Testing booktabs"
author: "r.bot"
date: "June 11, 2015"
output: pdf_document
tables: true
---

after which the sample xtable output appears with booktabs.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Yes, that resolves it, but since pandoc seems to include booktabs anyway, I don't understand why it doesn't work without an external .tex file. https://github.com/jgm/pandoc-templates/blob/master/default.latex `$if(tables)$ \usepackage{longtable,booktabs} $endif$` – r.bot Jun 11 '15 at 13:14
  • 1
    So try enabling option `tables`. – Dirk Eddelbuettel Jun 11 '15 at 13:18
  • The documentation on how to do that isn't particularly clear. After a little trial and error I found that `output: pdf_document tables: true` fixes the problem without resorting to an external .tex file. – r.bot Jun 11 '15 at 13:22
  • 2
    Dear @r.bot, the customary way to thank someone who took time read and answer your question is to upvote or accept such answer, not to intrude and edit it without asking. My thanks to pc-shooter for reverting. – Dirk Eddelbuettel Jun 11 '15 at 14:05
  • I apologise for having intruded, it was not my intention to offend. I simply wanted to make sure that the full solution was in the answer itself and not the discussion. I'll refrain from doing so in future. – r.bot Jun 11 '15 at 14:35
  • I'd be happy to edit my answer. I was actually looking for just one of those top-level switches, but didn't see one, knew about he include: solution and had to get out of the house. We can improve on this (and all this came in handy as were just discussing finer rmarkdown->pdf points as work :) – Dirk Eddelbuettel Jun 11 '15 at 15:06
  • Yes, please do edit and I will be happy to accept it. Thanks. – r.bot Jun 11 '15 at 15:59