How do I set up TeXstudio to compile xxx.Rnw files?

I am using the TeX-Editor TeXstudio 2.7, on Windows 7, and R 3.01 64-bit.

I would like to write a xxx.Rnw-file in TeXstudio and then from TeXstudio automatically (a) call R, (b) compile the Rnw to tex using knitr, (c) compile the pdf, (d) view the Pdf.

I found these pieces of information regarding the issue

Using the configuration "Rscript -e "library(knitr); knit2pdf('"test"Rnw')" for the user-defined compilatin command in TeXstudio, I get the error message that the command could not be started if I try to compile a sample file.

I would be glad if somebody made this work and is willing to share the configuration of the TeXstudio commands. Thanks a lot in advance! Jana

  • 2
    +1, for the Q, I would be glad if you could share your experience writing a .Rnw file from within TexStudio instead of other editors like RStudio IDE. I guess you wanted to do everything in one place, which I prefer also, but after 1 week from your question, did you feel it is now better? if yes, I would like to do the same, if no, please why.
    – doctorate
    Apr 18, 2014 at 9:03
  • 2
    I will share my very subjective experience(s): [Positive]: From within TeXstudio all works perfectly fine. All I have used it for (code, ggplot-graphics) worked well. [Negative] TeXstudio does not (yet?) support syntax highlighting for R-code; further I use the .Rnw to write and archive my scientific papers and most my co-authors do not do sweave which makes it hard for them; also most journals in my field require word-documents. I have to consider if it is worth the effort to export the .Rnw to word for this purpse.
    – JBJ
    Apr 18, 2014 at 13:06

3 Answers 3


I have solved it.

The command I use in the user-defined commands for compiling and displaying a Rnw-file in my own pdf-viewer is :

"C:/whateverYourPathToRonYourMachineIs/R-3.0.1/bin/Rscript.exe" -e "library(knitr); knit2pdf('%.Rnw')" | pdflatex -synctex=1 -interaction=nonstopmode %.tex | "C:/whateverYourPathToYourPDFViewerOnYourMachineIs/Foxit Reader.exe" "?am.pdf"

Screenshot of the TeXstudio configuration

  • 3
    yes,it works well. the command C:/whateverYourPathToYourPDFViewerOnYourMachineIs/Foxit Reader.exe" "?am.pdf" can be instead with ` "txs:///view-pdf" "?am.pdf"`, the pdf will be veiwed inside TeXStudio Dec 11, 2014 at 8:21
  • How do you embed/mix this rnw code with other code on a tex file with TexStudio?
    – skan
    Nov 1, 2015 at 1:49
  • It doesn't work for me. When it finds a " \Sexpr" it says "missing $ inserted. opts_ , and some undefined control sequence errors. And it doesn't accept comments with "#". How can I solve it?
    – skan
    Feb 28, 2016 at 12:42
  • OK, I got it. The file needs to have the Rnw extension. Anyway I can't get a preview.
    – skan
    Feb 28, 2016 at 13:07
  • 3
    Great solution. I've slightly modified it by creating a separate user command for 'user0:knit' (the knit command via Rscript) and then another commant for knit and compile: txs:///user0 | txs:///quick. Same result, but more concise.
    – Maxim.K
    Jul 30, 2016 at 14:38

Posting this is an answer which is similar to above because this will work for mac and (probably) on other linux based systems.

I use OS X El Capitan (mac), and the approach is kind of same. After installing "knitr" package, the user command can be defined as

R -e "knitr::knit2pdf('%.Rnw')" | txs:///pdflatex | txs:///view-pdf-internal

This works nicely.


I use the User Commands knit:knit with Rscript.exe -e "library(knitr); knit('%.Rnw')" and knit2pdf:knit2pdf with txs:///knit | txs:///pdflatex. This works nice with the magic comment % !TeX TXS-program:compile = txs:///knit2pdf

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.