I am writing a knitr document using xtable to get my tables. Xtable inserts by default a comment saying:

 "latex table generated in R 3.1.0 by stable 1.7-3 package % Thu Sep 18 14:19:21 2014"

This text is obviously commented out by a % symbol in xtable's output, but knitr seems to be sanitising the % by inserting a escape character "\" before it, which then is picked up by pdfLatex, resulting in the text:

% latex table generated in R 3.1.0 by xtable 1.7-3 package % Thu Sep 18 14:19:21 2014

Can I stop either knitr or xtable from doing this?

Edit: Here is an example document.

title: "knitr document"
keep_tex: yes
number_sections: yes
toc: yes
```{r load_packages,echo=FALSE,include=FALSE,results='hide',cache=TRUE}
```{r prepare_table,echo=FALSE,results='hide'}
table <- cbind(c(1,2,3,4),c(1,2,3,4))
colnames(table) <- c("var1","var2")
```{r print_table,echo=FALSE,results='asis'}
  • Could you add an example document demonstrating the issue?
    – mvkorpel
    Sep 18, 2014 at 12:55
  • @mvkorpel Added an example there.
    – Kenji
    Sep 18, 2014 at 13:45

3 Answers 3


Thanks to @Tyler, I went browsing the knitr issues page and learned that xtable has an argument to suppress the comments (upvote to Tyler for that). It is a bit hard to use because you have to pass the argument to xtable's print method. The code that solves my problem looks like:



We can resolve this permanently by setting an xtable option:

options(xtable.comment = FALSE)

See ?print.xtable; since the comment argument defaults to getOption("xtable.comment", TRUE), any calls to print.xtable will thereafter exclude the comment. Much credit due to mnel on SO for suggesting to do it permanently.


This is an xtable issue: xtable is designed for use within a LaTeX source document, and when you try to use it in a Markdown document instead, the formatting of comments becomes difficult to control as it passes from Md -> pandoc -> LaTeX -> pdf. There is some discussion on the knitr issues page. Knitr now includes the function kable to deal with simple tables. Alternatively, you could use a wrapper to strip the comments from xtable:

tyxtable <- function(x, trim = 2) {
    tmp <- textConnection(capture.output(xtable(x), file = NULL))
    tmp <- readLines(tmp)
    tmp <- tmp[-1:(-1 * trim)]
  • 1
    Thank you for pointing to the knitr issues page. I found out there that xtable was updated in version 1.71 to include an option to suppress comments. I did some browsing in xtables' documentation and learned that all I had to do was to add the argument comment=FALSE to the xtable call.
    – Kenji
    Sep 18, 2014 at 16:07
  • This approach works nicely for Hmisc::latex as well!
    – alistaire
    Nov 30, 2016 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.