TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm quite new to both R and LaTeX, but I'm trying to work with both, using Sweave.

I know you can comment in R using # and in LaTeX by using %. However, when I write a #comment in an R-chunk (between <<>>= and @) I get an error:

Error: 'filename.Rnw' is not ASCII and does not declare an encoding

Execution halted

I also tried it with % but that obviously didn't work (same error btw).

How can I put a comment in an R-chunk?

share|improve this question

Late in the game, but if you copied text into the Rnw file from another document type (e.g., Word), symbols such as " are coded differently. Retype any symbols that you copied over (in entire document, not just R chuck).

share|improve this answer
Welcome to TeX.sx! – Peter Jansson Apr 22 '13 at 22:15

Try keep.source=TRUE in Sweave options. Or \SweaveOpts{keep.source=TRUE} in the preamble. Source: Sweave customization

share|improve this answer

I think the real problem is that you have non-ASCII characters in your document, but you did not declare the encoding. Sweave was looking for a line like \usepackage[foo]{inputenc} in your source document; see ?Sweave for details.

share|improve this answer

I use comments in R chunks without a problem. Here is mwe.

Test of Sweave without comment
Test of Sweave with single \# for comment
# Comment

And the output. enter image description here

Reference this Include Sweave input as verbatim for example of getting the exact Sweave chunk in your output. (including the <<>>= and @)

share|improve this answer
Why is your comment being printed out? – Mien May 4 '12 at 15:42

As suggested by user29484, this sometimes occurs when you cut and paste into the Sweave document. It has happened to me several times when cutting and pasting from a pdf file in particular.

Text Wrangler (for Mac) has a tool called Zap Gremlins... in the Text menu. I open the Rnw file in Text Wrangler, apply this tool save the file and re-open in RStudio and am back in business.

share|improve this answer

I would seriously consider using knitr rather then Sweave. It is very easy to use inside of RStudio or through your own editor. I think it does a better job with options like this and more.



share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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