Monthly Archives: June 2011

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="http://R-Forge.R-project.org")

Here is an example file for either Sweave or pgfSweave:

\documentclass{article}

\usepackage{tikz}

\usepackage{Sweave}
\begin{document}
\SweaveOpts{concordance=TRUE}

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

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.


<<data>>=
x <- rnorm(10)
@

<<>>=
print(x)
@

\begin{figure}[!ht]
\centering
<<plot,echo=T,fig=T,width=3.1,height=3.1>>=
plot(x,t=’l')
@

\caption{Example figure.}
\end{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;

\end{document
}

Here is a script to compile:

#!/bin/bash

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 runSweave.sh, then call it

$ runSweave.sh 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:

#!/bin/bash

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’)"