# Workflow in Sweave: Handling TeX errors

I've been using LaTeX and R for a few years now, but am only now bringing them together via Sweave. As I put the document together, I run my R code in its own session to verify that it works properly, then add it to the Sweave file. Then, I Sweave the file in R. This goes fine, since all the R code has checked out. When I compile the TeX file, though, there are almost invariably errors that I have made.

Finding an error in the TeX file, tracing it to the Sweave file, and re-Sweaving and TeXing isn't that difficult. It does become a pain when there are multiple errors, meaning multiple reruns of Sweave and pdftex. It would be simpler if I could fix all the errors in the TeX file, which can be rerun alone simply, and have them transfer into the Sweave file, rendering it ready for a final Sweave and TeX compile.

Any tips on how you handle this workflow?

One tip is to not make any errors, but, as an economist, I believe that the optimal number of errors is probably greater than 0.

Thanks! Charlie

-

I also use sweave (a few months) and latex (20 years). With a reasonably fast machine, the optimal scheme is simply to rebuild often. I use "make" (on a unix-like machine) so that only the chapters whose Rnw file has changed get rebuilt into tex files. That saves a lot of sweave time.

Alternatively, you can edit the .tex file and work away at errors, and put your results back into the .Rnw file when you're ready. But that's error-prone.

If you run sweave-latex often, and fix errors as soon as you see them, then you won't make so many errors.

The other advice is to use a decent editor (e.g. emacs) that does a lot of the work for you, e.g. finishing off parenthetic blocks, begin/end environments and so forth. That prevents 2/3 of the errors you're likely to make.

-
Emacs has aucTeX mode for LaTeX and ESS (Emacs Speaks Statistics) for running R in an emacs buffer. plus the parenthesis matching and automatic addition of \end tags means emacs is a really good option for working with TeX and Sweave. –  Seamus Jan 23 '11 at 18:52

Dan above has probably the right answer. Just to add that, to make code execution cheaper and easier, you might look into the cacheSweave and pgfSweave packages on CRAN, which may be more convenient than setting and unsetting the eval switch in code chunks.

-
+1 I was about to post quite the same answer; caching Sweave results is especially useful when dealing with large data sets or complex models (I used to work with genetic data during one year or two). –  chl Jan 23 '11 at 18:19
I want to throw knitr into the discussion. It's cleaner than Sweave and implements cacheing automatically. –  gmacfarlane May 25 at 23:14
Yes. My answer was posted early in 2011. Two years later Knitr is probably the best solution, unless you specifically need Sweave for R package development or something. –  Kieran Jun 4 at 16:33

Depending on your R code, some R calculations may take a long time and rebuilding often is prohibitive whereas the TeX part by itself is typically negligible. To deal with this, you can exclude R code from being executed (once you know that it works) by assigning FALSE to eval:

<<fig=TRUE,eval=FALSE>>=
# potentially expensive R code here
@


You can also set eval globally to FALSE and enable it locally (<<eval=TRUE>>=) only for the R code that you are working on. This would be a good way when you are mostly dealing with TeX problems:

\SweaveOpts{echo=FALSE,pdf=TRUE,eps=FALSE,eval=FALSE}

-
Seems like caching would be a better way to handle this? –  Ari B. Friedman Nov 16 '11 at 16:07

Part of the issue is the time it takes to compile a Sweave document. The existing points are all good, i.e.:

• try to minimise errors
• consider eval=FALSE when extensive iteration of a tex section is required and the R code chunks are time consuming
• consider sweave caching options
• use a fast computer
• use a make file and divide up the document into multiple Rnw files so that only updated Rnw files need to be rebuilt

With regards to making corrections, Duncan Murdoch has a prototype of software to enable forward and inverse search for sweave documents:

Given that its development status is Alpha, I haven't tried it yet.

-