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 want to insert a word count into my .Rnw file.

To do this, I have the following line in myfile.Rnw:

\makeatletter\@@input|"echo `texcount -1 myfile.tex`| cut -c1-4"\makeatother

But this requires me to run pdflatex with the --shell-escape flag. With Sweave, I have a makefile that includes the line:

R CMD Sweave myfile.Rnw
pdflatex --shell-escape myfile.tex 

Is there a way to add the --shell-escape to pdflatex when I use knit instead of the makefile to compile the document?

Reproducible example:

contents of myfile.Rnw:


Words: \makeatletter\@@input|"echo `texcount -1 myfile.tex`| cut -c1-4"\makeatother
Lorem ipsum dolor

what works:

R CMD Sweave myfile.Rnw
pdflatex --shell-escape myfile.tex

what doesn't work:

"myfile.pdf" %in% dir() ## False
share|improve this question

migrated from stackoverflow.com Jan 15 '13 at 14:30

This question came from our site for professional and enthusiast programmers.

After grepping for pdflatex in the knitr source code, I found the function knit2pdf with the argument compiler. This seems to work:

knit2pdf("myfile.Rnw", compiler = "pdflatex --shell-escape")
"myfile.pdf" %in% dir() ## TRUE

And to do this in Rstudio:

Tools -> options -> Sweave -> enable shell escape commands

share|improve this answer
knit2pdf() is only a convenience function, and I do not quite expect users to set compiler (it is kind of hack); I recommend the RStudio approach you mentioned, and what happens there is basically: knit('myfile.Rnw'); pdflatex --shell-escape myfile.tex (the second step is missing in your original post, and that is why myfile.pdf does not exist -- knit() alone does not compile tex) – Yihui Jan 11 '13 at 21:52

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.