Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

\documentclass{article}
\begin{document}

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

what works:

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

what doesn't work:

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

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

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

1 Answer

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
1  
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
add comment

Your Answer

 
discard

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.