[R] Sweave, Texshop, and sync with included Rnw file

Yihui Xie xie at yihui.name
Fri Jan 18 03:29:38 CET 2013


Sweave produces foo-concordance.tex from foo.Rnw, and writes
\input{foo-concordance.tex} in the LaTeX output. You can turn on the
concordance option in knitr as well. Since you do not use RStudio, you
have a couple of more steps to go:

1. borrow \Sconcordance from Sweave.sty;
2. manually \input{foo-concordance.tex};
3. set the option concordance=TRUE in opts_knit;

The rest of steps are the same as Sweave -- you compile the output by
`pdflatex --synctex=1 foo.tex`, and run patchDVI on the results. I do
not use TeXShop, so I'm not sure if it really works.

\documentclass{article}
% from Sweave.sty in R
\newcommand{\Sconcordance}[1]{%
  \ifx\pdfoutput\undefined%
  \csname newcount\endcsname\pdfoutput\fi%
  \ifcase\pdfoutput\special{#1}%
  \else%
   \begingroup%
     \pdfcompresslevel=0%
     \immediate\pdfobj stream{#1}%
     \pdfcatalog{/SweaveConcordance \the\pdflastobj\space 0 R}%
   \endgroup%
  \fi}
\input{foo-concordance.tex}

<<setup, include=FALSE>>=
opts_knit$set(concordance=TRUE)
@

\begin{document}

<<>>=
for (i in 1:5) {
  print(i)
}
@

\end{document}

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA


On Thu, Jan 17, 2013 at 7:45 PM, huang min <minhuangr at gmail.com> wrote:
> Anybody know if it is possible to use texshop and knitr with the sync
> working? I add a knitr engine but cannot sync.
>
> PS, I am comfortable with texshop but not RStudio.
>
> Huang
>
> On Fri, Jan 18, 2013 at 7:33 AM, michele caseposta <mic.cipi at gmail.com>wrote:
>
>> Hi,
>> I just updated R and patchDVI (from CRAN).
>> Now I can reverse search from the pdf to the included.Rnw.
>> However, I cannot forward search from the included to the pdf. Is this how
>> it is expected to work?
>> Forward and inverse search work between main Rnw and pdf.
>>
>> I am pasting below the code in the two files.
>>
>> %%%%%%%%% Sweavetest.Rnw %%%%%%%%%%%%
>>
>> \documentclass{article}
>>
>> \begin{document}
>>
>> \SweaveOpts{concordance=TRUE}
>>
>> \title{an Sweave inclusion test}
>> \author{Michele}
>>
>> \maketitle
>>
>> This is an example for the inclusion of RNW files and forward-inverse
>> search.
>>
>> following is a chunk of sweave code:
>>
>> <<>>=
>> n <- 5
>> for(i in 1:n){
>> print(i)
>> }
>> @
>>
>> \section{text from an included file}
>> \SweaveInput{included.Rnw}
>>
>> \end{document}
>>
>> %%%%%%%%% Included.Rnw %%%%%%%%%%%%
>>
>> % !Rnw root = Sweavetest.Rnw
>>
>> this is some text included in an Rnw file
>>
>> <<>>=
>> for(i in 1:3){
>> print('included')
>> }
>> @
>>
>>
>>
>> On Jan 11, 2013, at 5:38 AM, Duncan Murdoch wrote:
>>
>> > On 13-01-10 4:54 PM, michele caseposta wrote:
>> >> Hi everybody,
>> >> thanks for the replies.
>> >> I might have not explained the problem completely.
>> >> Duncan Mackay:
>> >> Yes, I am already having a master file and separate Rnw files.
>> >> Duncan Murdock:
>> >> I am using patchDVI in the TexShop Sweave engine.
>> >> Sync works flawlessly between the master file and the pdf produced by
>> pdflatex.
>> >>
>> >> My problem is that I don't seem to be able to obtain sync between the
>> *included* Rnws and the pdf, either way.
>> >>
>> >> The sweave engine is as follows:
>> >>
>> >> #!/bin/bash
>> >>
>> >> R CMD Sweave "$1"
>> >> latexmk -pdf -silent -pdflatex=‘pdflatex –shell-escape
>> –synctex=1′"${1%.*}"
>> >> Rscript -e "library(‘patchDVI’);patchSynctex(‘${1%.*}.synctex.gz’)"
>> >>
>> >>
>> >> Funny thing is that the sync works in texworks, using the following
>> Rscript line
>> >>
>> >> patchDVI::SweavePDF('$fullname',stylepath=FALSE)
>> >>
>> >> I tried to mix and match configurations between texshop and texworks
>> but I had no luck
>> >
>> > I just tried a simple example in TeXShop and it worked for me.  My
>> Sweave engine is
>> >
>> > #!/bin/tcsh
>> >
>> > # set path= ($path /usr/local/bin)
>> > Rscript -e "patchDVI::SweavePDF(  '$1' )"
>> >
>> >
>> > So it seems to be work listing versions:  On the Mac, I'm using R 2.15.0
>> patched, rev 59478, with patchDVI version 1.8.1584 (I just uploaded 1.9 to
>> CRAN, by the way), TeXShop version 2.43.
>> >
>> > It also works on Windows, where I have current releases of R and
>> patchDVI installed.
>> >
>> > If you've got current versions of everything installed and it's still
>> not working, could you try putting together a small reproducible example?
>> >
>> > Duncan Murdoch
>> >
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> On Jan 10, 2013, at 11:23 AM, Duncan Murdoch wrote:
>> >>
>> >>> On 13-01-09 9:09 PM, Duncan Murdoch wrote:
>> >>>> On 13-01-09 3:25 PM, michele caseposta wrote:
>> >>>>> Hello everyone.
>> >>>>> I am in the process of writing a book in Latex with Texshop, on Mac.
>> >>>>> This book contains a lot of R code, hence the need to use Sweave.
>> >>>>> I was able to compile Rnw files, and to sync back and forth from the
>> pdf to the source Rnw.
>> >>>>> My problem now is that the book is divided in Chapters, and every
>> chapter is in its own Rnw file.
>> >>>>> I can compile them from the main one (book.Rnw) using the directive
>> >>>>>
>> >>>>> \SweaveInput{chapter1.Rnw}
>> >>>>>
>> >>>>> The problem stands in the fact that like this I am missing
>> synchronization between the pdf and the source Rnw. If part of text is in
>> book.Rnw I can synchronize, but if the text is in one of the included
>> files, it just doesn't work.
>> >>>>> I am using the sweave engine found in the following webpage:
>> >>>>>
>> >>>>>
>> http://cameron.bracken.bz/synctex-with-sweavepgfsweave-in-texshoptexworks
>> >>>>>
>> >>>>> Has anybody succeeded in synchronizing with included Rnw files?
>> >>>>
>> >>>> This is a problem addressed by my patchDVI package, available on
>> >>>> R-forge.  You have a main file (which can be .tex or .Rnw), and put
>> code
>> >>>> at the start of each .Rnw file to indicate where to find it.  Then you
>> >>>> just run Sweave on one of the chapters, and it automatically produces
>> >>>> the full document.
>> >>>>
>> >>>> The sample document here:
>> >>>>
>> >>>>
>> http://www.umanitoba.ca/statistics/seminars/2011/3/4/duncan-murdoch-using-sweave-R/
>> >>>>
>> >>>> includes an appendix describing how to set this up with TeXShop.
>> >>>
>> >>> I just committed an update to the vignette in patchDVI giving a quick
>> version of the instructions for basic use. Version 1.8.1585 has the new
>> vignette.
>> >>>
>> >>> I should get around to pushing it to CRAN one of these days...
>> >>>
>> >>> Duncan Murdoch



More information about the R-help mailing list