All posts by cameron

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

There is a new version of pgfSweave, and its awesome!

I just uploaded version 1.2.1 of pgfSweave, and I think it is just great! The most notable new feature is the use of the TikZ externalization library list and make feature instead of the pgf \begingraphicnamed functionality. This has some important consequences:

  1. PGF 2.10 is required! This has been out for some months now but many LaTeX distros are notoriously slow at updating, so you may need to update manually.
  2. Backward compatibility may break with some documents who still have the \pgfrealjobname hardcoded or who use the tex.driver option.
  3. A new workflow is required to compile documents (it includes make), this is outlined in the vignette.
  4. You can use all the nifty features in the TikZ externalization library, see the TikZ/PGF manual for more details.
  5. Most importantly it is faster!

There are a boat load of other changes and new features, I think you like it, check them out:

Version 1.2.1 – 2011-04-03
* [NEW] If PGF 2.10 is not available, issue a startup message but still
        load the package (So the package can go on CRAN). PGF 2.10
        is still required to compile .tex file produced with pgfSweave
* [NEW] Remove null device code which could cause problems with grid
* [NEW] Example using xelatex for external graphics
* [NEW] Chunk options `relwidth’ and `relheight’ for scaling figures
        relative to the `width’ and `height’ options (thanks to Fabrice
        Rossi for the patch)
* [CHANGED] Fix the internal caching of external graphics, it now works as
        expected and can save significant time

Version 1.2.0 – 2011-02-21
* [NEW] Use the tikz externalization library instead of the pgf backend.
        This allows for more flexibility and faster compilations but
        BEWARE, this requires at least pgf 2.10 and may break backward
        computability in some cases
* [NEW] GNU Make is now a system requirement for externalizing graphics,
        NOT for compiling the package
* [NEW] Option to specify number of processors when externalizing graphics
* [NEW] Externalized graphics now have another layer of crude caching
* [NEW] Highlighted output is now output in a separate environment to fix
        the centering issues
* [NEW] Example document using plain latex and the tikz external library
        to generate eps graphics,
* [CHANGED] The command line interface is now much simpler internally
        and uses optparse instead of getopt for more flexibility (the
        optparse package is now a dependency)
* [CHANGED] The `tex.driver’ option has been removed, the same effect can
        be achieved using the tikz externalization library, see the new
        example for how
* [DEPRECIATED] The `pgf’ option is depreciated, please use the `tikz’