# Re: [Rd] Sweave processes \Sexpr in commented LaTeX source (2.3.1patched and 2.4.0)

From: Marc Schwartz (via MN) <mschwartz_at_mn.rr.com>
Date: Wed 20 Sep 2006 - 15:00:43 GMT

On Wed, 2006-09-20 at 12:20 +0200, Peter Dalgaard wrote:
> Friedrich Leisch <friedrich.leisch@stat.uni-muenchen.de> writes:
>
> > >>>>> On Tue, 19 Sep 2006 19:14:39 -0500,
> > >>>>> Marc Schwartz (MS) wrote:
> >
> > > Hi all,
> > > On FC5, using:
> >
> > > Version 2.3.1 Patched (2006-08-06 r38829)
> >
> > > and today's
> >
> > > R version 2.4.0 alpha (2006-09-19 r39397)
> >
> > > with the following .Rnw file:
> >
> >
> > > \documentclass[10pt]{article}
> > > \begin{document}
> >
> > > This line should print '2': \Sexpr{1 + 1}
> > > %% This line should NOT print '2': \Sexpr{1 + 1}
> >
> > > \end{document}
> >
> >
> > > The \Sexpr in the second line is processed even though the line is
> > > commented. This results in the following .tex file content (in the case
> > > of R 2.4.0):
> >
> >
> > > \documentclass[10pt]{article}
> > > \usepackage{/home/marcs/R.Files/SourceCode/R-alpha/share/texmf/Sweave}
> > > \begin{document}
> >
> > > This line should print '2': 2
> > > %% This line should NOT print '2': 2
> >
> > > \end{document}
> >
> >
> >
> > > Shouldn't Sweave just generally ignore commented LaTeX code? In
> > > reviewing Sweave.R I did not see a check for this, so perhaps there are
> > > circumstances where one wants a \Sexpr in commented LaTeX code
> > > processed. An example escapes me at the moment however.
> >
> > Sweave does not parse the LaTeX part of the document at all (which
> > makes it a lot easier), all it does is looking for its own "magic
> > strings" and replacing those, wherever they might occur.
> >
> > If we would start to respect comments, we would need a some kind of
> > parser for LaTeX, which we currently don't need ... and starting to
> > parse LaTeX would also open a can of worms of possible requests (what
> > to do with Sexpr inside verbatim, etc, etc).
>
> ..not to mention TeX comments inside Sexpr (e.g. %*%...). Skipping
> lines with % as the first character might be a viable compromise
> though.

Fritz and Peter,

First, for some context on what I was doing.

Thus, using (C-c ;) in emacs/auctex to comment some blocks of LaTeX code, I then ran Sweave and of course noted errors which I tracked down to the evaluation of the (presumably) commented \Sexpr's.

I think that Peter's approach would be a reasonable option, at least in this scenario and perhaps others, without getting into the need to parse the LaTeX code for '%'s preceding a '\Sexpr'. This would, I believe, require modifying the current 'docexpr' regex's in Sweave.R.

An alternative of sorts, would be to add another Sweave option, something like:

\SweaveOpts{eval.Sexpr=false}

which could be placed prior to a section where one wanted to disable the evaluation of Sexpr's and then re-enable it with a:

\SweaveOpts{eval.Sexpr=true}

at a subsequent point in the .Rnw file.

The option would of course default to 'true'. This would make the solution more "global" and again not require the need to parse the LaTeX code.

Food for thought.

Best regards,

Marc

R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu Sep 21 01:12:11 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 20 Sep 2006 - 16:30:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.