Synctex with Sweave/pgfSweave in TeXShop/TeXWorks

Ever been editing an .Rnw (Sweave) file and tried to sync a pdf with the source in TeXShop (or TeXWorks) and had it open the .tex file? This is because the synctex information (in the .synctex.gz file) is messed up. Both TeXShop and TeXWorks support synctex, that means that if everything is groovy, we should be able to jump between the corresponding places in the source and pdf windows.

Unfortunately all that goes out the window when using Sweave. Fortunately we can make it work! By that I mean it is possible to sync beween a pdf and the .Rnw source file. We need three things:

  1. Add the concordance option when compiling with Sweave. That means add the line \SweaveOpts{concordance=T} to the top of your document after loading the Sweave style file.

  2. Enable synctex with the --synctex=1 flag when compiling the tex file.

  3. From the patchDVI (sweavesearch project on R-Forge), you need to use the patchSynctex function. This package is not available on CRAN afaik so you should install it from source from R-Forge:

    install.packages("patchDVI", repos="")

Here is an example file for either Sweave or pgfSweave:




\title{Concordance Example}
\author{Cameron Bracken}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas
eget dolor enim. Donec tincidunt augue porta justo lacinia
convallis. Quisque convallis mi sit amet augue volutpat gravida.
Class aptent taciti sociosqu ad litora torquent per conubia nostra,
per inceptos himenaeos. Proin consequat fringilla adipiscing.
Integer interdum risus id lorem luctus et convallis mauris rutrum.
Suspendisse at mauris velit. Cras sed nisl ipsum, et sodales nulla.
Integer in enim id ante aliquam tincidunt eget sed urna. Fusce et
eros sit amet lacus hendrerit rutrum. Proin a tellus sed magna
feugiat mollis. Fusce est augue, sodales quis scelerisque quis,
fringilla at leo.

x <- rnorm(10)



\caption{Example figure.}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sed
leo at mauris lacinia consequat quis sed neque. Nulla auctor
malesuada leo, at sodales nulla lacinia et. Donec vel enim vitae
massa aliquam posuere. In nec convallis sapien. Aenean tincidunt,
ipsum non congue ornare, dui eros lobortis libero, sed egestas nibh
arcu vel metus. Donec condimentum, ipsum ut vehicula ullamcorper,
urna lacus aliquam est, at condimentum nunc odio at felis. Aenean
dapibus aliquam dolor, at elementum dolor imperdiet vitae. Proin
felis odio, ultricies ut gravida scelerisque, aliquet ut magna.
Nullam at sapien vel mauris iaculis viverra vel ut mi. Class aptent
taciti sociosqu ad litora torquent per conubia nostra, per inceptos
himenaeos. Nunc vulputate ipsum placerat enim hendrerit at aliquam
mi porttitor. Fusce eget commodo metus. Vivamus sed nisl eu enim
adipiscing feugiat at et dolor. Vestibulum ante ipsum primis in
faucibus orci luctus et ultrices posuere cubilia Curae;


Here is a script to compile:


R CMD Sweave "$1"
latexmk -pdf -silent -pdflatex=‘pdflatex –shell-escape –synctex=1’ "${1%.*}"
Rscript -e "library(‘patchDVI’);patchSynctex(‘${1%.*}.synctex.gz’)"

say you name it, then call it

$ myfile.Rnw

A much easier way would be to set up an engine in TeXShop or a custom “processing tool” in TeXWorks. There is a function in the patchDVI package called SweavePDF that should do the same thing but I have not tried it.

After all the compiling and patching of the synctex file is done, try right clicking and selecting ‘Sync’ in TeXShop or ‘Jump to PDF’ in TeXWorks, and everything should work! NOTE: I dont use TeXWorks so I haven’t gotten it to work but others have reported it does.

As for pgfSweave, the current version does not correctly output concordance information. But a patch enables proper concordance support (thanks aecay!). The next version of pgfSweave should have this working. To compile with pgfSweave, it is pretty much the same:


R CMD pgfsweave –graphics-only "$1"
make -j 2 -f "${1%.*}".makefile
latexmk -pdf -silent -pdflatex=’pdflatex –shell-escape –synctex=1 –file-line-error’ "${1%.*}"
Rscript -e "library(‘patchDVI’);patchSynctex(‘${1%.*}.synctex.gz’)"

2 thoughts on “Synctex with Sweave/pgfSweave in TeXShop/TeXWorks

  1. Hi, I know that this might come a bit late, but do you have any suggestions for synchronization among included files? I am including files with \SweaveInput, so that I can write each chapter of the book in a separate file, and I am having a hard time understanding how to sync back to the appropriate Rnw file from the pdf

    1. With TeXWorks, it just works. You should make sure that each of your .Rnw files has a concordance set, and make sure they have different names (using prefix-string in the \SweaveOpts). Then patchDVI will patch all of them.

      I think TeXShop might not support multiple source files with Synctex. At least it’s not working for me. I’m in TeXShop 2.47; I don’t know if that’s current or not.

Leave a Reply

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