Skip to content

Sweave processes \Sexpr in commented LaTeX source (2.3.1patched and 2.4.0)

9 messages · Marc Schwartz, Friedrich Leisch, Peter Dalgaard +3 more

#
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.

HTH,

Marc Schwartz
#
> 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).

Best,
Fritz
#
Friedrich Leisch <friedrich.leisch at stat.uni-muenchen.de> writes:
..not to mention TeX comments inside Sexpr (e.g. %*%...). Skipping
lines with % as the first character might be a viable compromise
though.
#
On Wed, 2006-09-20 at 12:20 +0200, Peter Dalgaard wrote:
Fritz and Peter,

Thanks for your replies.

First, for some context on what I was doing.

I have a large .Rnw file and was in the process of doing some debugging.
I had set some R chunks to 'eval=false' in the process. This resulted in
some R objects not being created that were in turn used in the
subsequent \Sexpr's.

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
#
Peter Dalgaard <p.dalgaard at biostat.ku.dk> writes:
+1.  You could probably ignore any lines where the first
non-whitespace char is '%'.  But if that seems to risky, then only
recognizing first-char-is-% seems a worthwhile heuristic.

Another place where this has bitten people is when they do:

%\usepackage{Sweave}

Sweave picks that up and doesn't insert the usepackage line itself.
So ignoring LaTeX comment lines would solve two problems.

Best,

+ seth
#
2006/9/20, Seth Falcon <sfalcon at fhcrc.org>:
I've found that extremely useful for sweaving Stex files which aren't
"master" files (i.e., only files to include in a main latex file).
Inserting '\usepackage{Sweave}' in each would result in a latex error.
So commenting it out is a useful workaround.

Antonio.
#
"Antonio, Fabio Di Narzo" <antonio.fabio at gmail.com> writes:
Commenting it out _and_ having Sweave see it?
#
> "Antonio, Fabio Di Narzo" <antonio.fabio at gmail.com> writes:
  >> 2006/9/20, Seth Falcon <sfalcon at fhcrc.org>:
  >>> Peter Dalgaard <p.dalgaard at biostat.ku.dk> writes:
  >>> > ..not to mention TeX comments inside Sexpr (e.g. %*%...). Skipping
  >>> > lines with % as the first character might be a viable compromise
  >>> > though.
  >>> 
  >>> +1.  You could probably ignore any lines where the first
  >>> non-whitespace char is '%'.  But if that seems to risky, then only
  >>> recognizing first-char-is-% seems a worthwhile heuristic.
  >>> 
  >>> Another place where this has bitten people is when they do:
  >>> 
  >>> %\usepackage{Sweave}
  >>> 
  >>> Sweave picks that up and doesn't insert the usepackage line itself.
  >> 
  >> I've found that extremely useful for sweaving Stex files which aren't
  >> "master" files (i.e., only files to include in a main latex file).
  >> Inserting '\usepackage{Sweave}' in each would result in a latex error.
  >> So commenting it out is a useful workaround.

  > Commenting it out _and_ having Sweave see it?

Yes, I actually do that quite often, e.g., when I have my own
definitions of the Sinput/Soutput/... environments in the document
preamble and don't want to use any Sweave style file.

Note that we recently had a thread on the \usepackage{Sweave} path
insertion problems in windows and as a result I will stop being
special about it at all, i.e., users will have to put a
\usepackage{Sweave} into their documents, and take care that latex
finds a version of it.

The thread was end of August and I didn't do it for 2.4 because I
didn't want to break all vignettes that close to a release. But I will
do for the 2.5 series once 2.4.0 is released.

Ad evaluation of \Sexpr{}: That can be considered a bug, hence I could
try a fix even in feature freeze. But as I will be offline for a week
starting tomorrow this will also have to wait until the 2.5 series (or
2.4.1, depending on the change).

My favorite would not be a separate option eval.Sexpr but eveluate
only when the global option for eval is true. Then an

	\SweaveOpts{eval=false}

at any place in a document would stop all evalualtions, be it code
chunks (which do not override the default) or Sexpr. If somebody has
good useage for a separate option I could be convinced to have it,
otherwise I'll go for \Sexpr{} listening to the eval option.

Best,
Fritz
#
On Thu, 2006-09-21 at 16:40 +0200, Friedrich Leisch wrote:
Fritz,

I appreciate your consideration of this issue.

I think that your solution using a global evaluation option would be
fine.

Thanks again,

Marc