Category Archives: Sweave

Who wants to maintain pgfSweave?

So the time has come for me to face the fact that I have no time to maintain pgfSweave. It was recently archived because I didn’t make necessary changes to comply with some CRAN policies. SO, I need someone to step up to the plate to make some tweakes, put it back up on CRAN and maintain it in the future. Is it you?

To do the job you need at a basic understanding (or a desire to learn) the following:

  • Git/Github
  • Sweave
  • LaTeX
  • Make
  • R (duh :p)

Compensation includes:

  • Undying glory
  • The respect of your peers
  • Self satisfaction
  • A high-five from me if we ever meet in person!

If you are interested in the job please contact me at cameron[dot]bracken[at]gmail[dot]com

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

Using tikzDevice with Sweave in R 2.13

R 2.13 introduces an option to specify a custom graphics device in an Sweave code chunk. This is really cool and allows you to use tikzDevice output like pgfSweave does. In an pinch, say when you dont have access to any non-core packages, you can use tikzDevice output with the regular Sweave driver (RweaveLatex) more simply than before. Here is a minimal example showing how.




Example using \texttt{tikzDevice} with the default \textbf{Sweave}
driver (\texttt{RweaveLatex}).  First define a custom device:

tikz.Swd <- function(name, width, height, …){
    tikzDevice::tikz(file = paste(name, "tikz", sep = "."),
                 width = width, height = height)


Then use the device for plotting, but you have to include the figure manually.

<<myplot, fig=T, grdevice=tikz.Swd, include=F, width=4,height=4>>=


WARNING: This could be very slow if your graphic is complex