The xdvi and yap DVI viewers allow inverse (aka reverse) search: you click on a location in the output display, and your editor moves to the corresponding location in the input file. yap at least also allows forward search, where you can have your editor tell it to location where a particular input line is displayed. I would like something like this to work in Sweave files. That is, I write an Sweave file and process it through to a .dvi in the previewer, then I click in a previewer and am taken to the original location in the Sweave file. There seem to be two obstacles: The latex I'm using (from MikTeX) doesn't give any way to indicate the original source file when the .tex comes out of Sweave, and Sweave doesn't keep track of the concordance between input lines and output lines. Both of these are fixable, but it looks like a few hours work; has anyone done this already? Duncan Murdoch
On 1/13/2007 3:58 PM, Duncan Murdoch wrote:> The xdvi and yap DVI viewers allow inverse (aka reverse) search: you > click on a location in the output display, and your editor moves to the > corresponding location in the input file. yap at least also allows > forward search, where you can have your editor tell it to location where > a particular input line is displayed. > > I would like something like this to work in Sweave files. That is, I > write an Sweave file and process it through to a .dvi in the previewer, > then I click in a previewer and am taken to the original location in the > Sweave file. > > There seem to be two obstacles: The latex I'm using (from MikTeX) > doesn't give any way to indicate the original source file when the .tex > comes out of Sweave, and Sweave doesn't keep track of the concordance > between input lines and output lines. > > Both of these are fixable, but it looks like a few hours work; has > anyone done this already?I went ahead and wrote a prototype of this. It works by having Sweave output a \special that contains the information necessary to patch the .dvi file so it points to the .Rnw rather than .tex source. There's an R function that carries out the patching, after you run latex on the document. Currently the patching runs pretty slowly, but I suspect if I rewrite it in C rather than R it would be acceptably fast. If anyone has any interest in this, please let me know. Duncan Murdoch
I've now packaged up the Sweave inverse search package, and made the prototype available on my web page http://www.stats.uwo.ca/faculty/murdoch/software as patchDVI_0.2.tar.gz and patchDVI_0.2.zip. I don't know of any bugs in those, but I'd like to hear about them. To enable the inverse search, you need an \SweaveOpts line in the .Rnw file with "concordance=TRUE". I normally use MikTeX, so to make my life easier I wrote a little SweaveMiktex() function and put it in the package. Using that package in Windows, the following batch file processes Sweave, runs latex, patches the .dvi, and runs the previewer: echo library(patchDVI);SweaveMiktex('%2', '%3.tex') | Rterm --slave yap -1 -s"%1%2" %3.dvi It needs to be called with three parameters: %1 - the current line number in the editor; yap will jump to that location %2 - the name of the current file in the editor; if it's *.tex, then Sweave will be skipped, but the patching will still be done (because some other file in the project might be a .Rnw file). If not, the file is run through Sweave first. %3 - the base name of the file (no extension) to send to latex. For example, if I am on line 100 of a chapter called syntax.Rnw in a book called programming.tex, I would call this file with args 100 syntax.Rnw programming If you're using some other TeX package, the SweaveMiktex function might still work, but it's likely the script to run it and the previewer would have to change. If you have a completely different work flow, the steps you need to implement are these: 1. Put the \SweaveOpts{concordance=TRUE} line into your .Rnw file. 2. Run the patchDVI version of Sweave() on your file. 3. Run latex to produce a .dvi file. 4. Run the patchDVI function on the .dvi file to patch it. 5. Run your previewer, with options telling it you want to see a particular line in the .Rnw file. In a big project, you only need to run Sweave on those .Rnw files that have changed; the information linking to the original source is output in a file in the same place figures from the Sweave output would be saved, e.g. in the example above, as "syntax-concordance.tex". The format of the concordance is very likely to change as patchDVI evolves, so you'll want to re-run Sweave if you update your patchDVI package. Duncan Murdoch