Sweave Engine for TeXShop

Sweave is an awesome utility for including the output of R code in a LaTeX document which I have started using regularly.

TeXShop is my favorite editor/viewer for LaTeX under Mac OS X for many reasons. One of which is the speed of the edit -> compile -> view process which it enables. Therefore, I have been reluctant to use Sweave in the past because of the extra LaTeX compilation step that is not supported by default in TeXShop.

Recently, my friend Charlie Sharpsteen first showed me this way to get around this with a custom “engine”. The engine file looke like this:



export PATH=$PATH:/usr/texbin:/usr/local/bin
R CMD Sweave "$1"
pdflatex "${1%.*}"

Place this file in ~/Library/TeXShop/Engines/, make sure PATH includes the location of you TeX distribution.

Make sure executable permissions (like 744) are set on both files or TeXShop will give an error message. Now open (or re-open) TeXShop and select “Sweave” from the list at the top left. Now you can use command-t to compile Sweave and view your document in one step! Thanks Chuck!

34 thoughts on “Sweave Engine for TeXShop

  1. Hi Cameron. I’m trying to implement your solution to minimize the number of steps for typesetting but I’m a novice with terminal. How did you go about the steps to make Sweave an option in the typesetting menu? I cannot figure this out.


  2. Very minimal terminal uses necessary. Just copy the text for Sweave.engine and paste into a text editor and save as ~/Library/TeXShop/Engines/Sweave.engine, the ~ means your home folder. Then open up terminal and enter

    chmod +x ~/Library/TeXShop/Engines/Sweave.engine

    does that help?

  3. hmm, I did exactly that but I’m not getting the engine to show up. I tried removing the folder and having TexShop reconstruct it, then copying over the Sweave.engine there again (as per Texshop’s instructions about user’s engines), but this still doesn’t work. I’m running the latest 2.26 Texshop.

    Here is my output with permissions:

    -rwxr-xr-x@  1 mech  staff   94 23 Dec 14:12 Sweave.engine
    -rwxr-xr-x@  1 mech  staff   53 23 Dec 14:01 SweaveNoClean.engine
    -rwxr-xr-x   1 mech  staff   72 19 Mar  2009 XeLaTeX.engine
    -rwxr-xr-x   1 mech  staff   69 19 Mar  2009 XeTeX.engine</p>

    not sure what the ‘@’ means but the engine should be executable. Similarly, the Sweave.sh is executable. I must be missing a step somewhere…

  4. ps here are the permissions for Sweave.sh within ~/Library/TeXHsop/bin

    mech$ pwd /Users/mech/Library/TeXShop/bin

    ls -l -rwxr-xr-x@ 1 mech staff 29059 23 Dec 13:56 Sweave.sh …

    I’m thinking that there must be something different that the new texshop is doing that is preventing a user created engine from showing up. If you know anything about this, I’d appreciate some feedback. Thanks and happy holidays.

  5. I am pretty sure there is not anything in TeXShop preventing this. I know it works with the most recent versions of TeXShop on both Leopard and Snow Leopard.

    Even if the permissions are not set correctly, the file should still appear in the list. The permissions will simply let the file be run as an executable.

    You should try to change the name of one of the existing engine files in the ~/Library/TeXShop/Engines and see if the change shows up in the menu.

    p.s. see here for the meaning of the @ symbol:


  6. Hi Cameron. I just realized that neither Xelatex nor Xetex are showing up in my menu. That is probably the root of my issues. It is possible that I have another copy of TeXshop Engines folder that I’m not accessing. I’ll see if I can solve that issue first as the steps that you outlined are quite straight forward.

    Thanks for the feedback.

    Enjoy your holidays and I’ll update if I resolve this issue.

  7. Hi,

    I had the same problem and managed to solve it by getting TeXShop to rebuild its entire ~/Library/TeXShop folder (as before, move it to the desktop or wherever, add the engine file(s) you need, then restart TeXShop).

    1. I think that change was made in 2.9 or so, I find it easier to install Sweave.sty in my texmf folder.

      cp -r /Library/Frameworks/R.framework/Resources/share/texmf/Sweave.sty ~/Library/texmf/tex/latex/

      this is because I prefer to use the nogin and noae options.

  8. Hello,

    Thanks for the script.

    Now, a very naive question, following up. I have the file in the indicated directory, with the execute bit on. I have moved the ~/Library/TeXShop folder out of the library and restarted TeXShop. I moved the Sweave engine back into the Library.

    Sweave still doesn’t appear in the TeXShop Typeset menu.

    Any hints?

    Running OS 10.6.2, TeXShop 2.33.

    Thank you.


  9. Thanks! This is really helpful.

    When developing R code I want to be inside the R editor. But then once I’m happy with it I want to be inside the TeXshop editor.

    If I have to keep going back to R to then create the TeX file to then edit it, it would be a big big hassle.

    Being able to edit in TeXshop then compile with one command is great.

    Thanks again.


  10. Hi Cameron. I was being thick previously. I was looking under Texshop’s top bar menu for the Sweave engine, which of course it doesn’t show. In fact, it only shows the ones that have shortcuts. However, in the working window, beside the ‘Typeset’ button you can choose the engine. And it is right there! Plus all the other ones I wanted to try!


    Maybe there are other people with this dilemma.

  11. Hi! This post is old, but I’m implementing now. It all works just fine, but I’m wondering, can I modify the Sweave engine to call Gregor Gorjanc’s Sweave.sh? And how about syntax highlighting? If you can’t get texshop to do any syntax highlighting for the code chunks, it doesn’t seem like such a good editing option for .Rnw files. Thanks!

    1. you can absolutely use Sweave.sh, just call

      Sweave.sh "$1"

      instead of the original line (making sure its in your PATH).

      As for highlighting, it is unfortunate, I agree. Though there is not really a great alternative in my opinion. I mainly only do plotting in code chunks so it is not that big of a deal. TextMate has an Sweave bundle but I keep going back to TeXShop because i can easily understand and configure the tool chain that is used. I wish that TeXShop had a system for configuring syntax highlighting like TextMate. It could be done Im sure.

  12. Thanks so much!

    In general, I’m choosing between texshop or Emacs with ESS, which does syntax highlighting very well for Sweave files, but doesn’t have the advantage of one click pdf rendering (I think).

    Probably if I knew more, I could make something work, but unfortunately I am completely dependent on googling and finding blogs like this one!

    1. I dont know much about emacs but I would highly recommend TeXShop for its native mac experience as long as you can live with the lack of highlighting for code chunks. I have tried most other editors for TeX on mac but TeXShop is still my favorite. The one click compilation and view with the built in viewer is slick and elegant. I also use other tools such as pgfSweave and incorporating that is as easy as creating a new engine.

  13. Hi Cameron. I wanted to share a new way of Sweaving with a single click but with a very powerful and free program: Kile. It’s a KDE Latex Editor and is by far the best LaTeX editor that I’ve tried.

    If you don’t mind tinkering, you can install Kile on OSX (you can Google for instructions, which require you to have latest Dev Tools, XCode, and some other utilities to compile Unix apps).

    I was able to implement my own solution in Kile for 1 click Sweaving. Because Kile isn’t OSX native, I am forced to use Preview or Skim as the PDF previewer. This I find is the only downside of using Kile on OSX. On my windows machine, I simply install a Linux distro and then work in Kile natively within the VM.

    On OSX, the pros of Kile far outweigh TeXShop’s, so this has been my editor of choice for a while now.

    Wherever I don’t have Kile installed, I still make sure I have the Sweave engine up and running.


  14. Hi,

    I get the “Sweave” to show up in the drop down menu but I can’t get the “typeset” button to be active. It is grayed out. Any suggestions?


    1. It probably has to do with the file extension. Try naming your file with the exact extension “Rnw”. TeXShop only recognizes a limited number of file extensions as “able to be typeset.” For all other files the typeset button will be grayed out.

  15. This is so convenient! I am having some problems though with generating plots with this. The plots are being generated in R and are ending up in the correct directory. Judging from the LaTex console, however, it seems as if, LaTex is not either not adding the proper suffix, “.pdf” (or “.eps”?) OR it is not looking in the proper directory. Do you know how I can go about fixing this?

  16. I thought I’d share a better way of Sweaving in TeXshop given that I am spending more time with it in my OSX virtual machine (don’t have Kile on it yet). I created this because I write my articles with R code, and need to handle bibtex. As you may know, to typeset an article with a bibliography, you need to do the following: 1. pdflatex 2. bibtex 3. pdflatex 4. pdflatex (and may be 1 more time, if it gives warnings)

    Here is my new Sweave.engine content: (Note that this will evaluate your director for the existence of *.bbl. If this file exists, it indicates that you ran BibTeX already. If it does not exist, it runs ‘bibtex’ on the *.aux file.

    PS I’ve included some comments after the commands to help you understand what each one is doing. If you already have a working Sweave.engine, just replace the contents with the ones I provide, save, and done!



    export PATH=$PATH:/usr/texbin:/usr/local/bin
    R CMD Sweave "$1"
    pdflatex "${1%.*}"

    file=$1 # record the full filename
    fname=$(basename $file) # record the full filename; may be redundant, didn’t check
    fname1=${fname%.} # extract the base of the file, i.e., w/o extension
    filebbl="${fname1}.bbl" # add a new extension; we’ll be checking directory for existence of this file
    fileaux="${fname1}.aux" # run bibtex on this if a *.bbl file does NOT exist
    test="${1}.aux" # this is NOT how to do change extensions; see above examples
    if [ -e "$filebbl" ]; then # -e checks for existence of the file; if it exists, just echo the filenames for info purposes
        echo $filebbl
        echo $fileaux
        echo $test
        echo "This file was typeset: ${1%.}"
    else # if $filebbl does NOT exist, run bibtex, then pdflatex 2x to take care of all floats/citations
        bibtex "$fileaux"
        pdflatex "${1%.<em>}"
        pdflatex "${1%.</em>}"
        echo "This file was typeset: ${1%.*}"

  17. @mech Thanks for the example! (i’ve prettied it up a bit)

    You might consider using latexmk to simplify this process.


    export PATH=$PATH:/usr/texbin:/usr/local/bin
    R CMD Sweave "$1"
    latexmk -pdf "${1%.*}"

    That will automatically detect and do whatever pdflatex/bibtex/etc. that is needed and will run pdflatex automatically the correct number of times! latexmk

    The programs rubber and texi2pdf have similar functionality.

  18. Nice. Thanks for that. I’ll try it out later tonight.

    I didn’t know that this came prepackaged with latex. Is it part of every standard build, like TeXLive (or MacTeX)?

  19. Yeah im pretty sure it does come with TeXLive, it definitely comes with MacTeX. It is just a pearl script so easy to install if you dont have it.

  20. Hey there! I know the thread is old, but I wondered if there is something similar for knitr…

    do you know/have a one-click-engine for using knitr in TeXShop?

  21. Thanks for posting this! It’s really helpful.

    Should work the same way for knitr — just need to replace your R CMD Sweave command in the shell script with your knitr command.

Leave a Reply

Your email address will not be published. Required fields are marked *