Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commented out Sexpr #110

Closed
knokknok opened this issue Jan 25, 2012 · 22 comments
Closed

Commented out Sexpr #110

knokknok opened this issue Jan 25, 2012 · 22 comments
Labels
feature Feature requests
Milestone

Comments

@knokknok
Copy link
Contributor

Something that always bugged me with Sweave is the fact that Sexpr are evaluated even though the corresponding latex code is commented-out.

Unfortunately it still bugs me in knitr ;-)

Any chance you could fix that?

Thanks.

@yihui
Copy link
Owner

yihui commented Jan 25, 2012

It is easy to solve -- just destroy \Sexpr{} in whatever way you want, e.g. %\%GoAway!%Sexpr{}%, or simpler %\%Sexpr{}%, or \Sexpr{# I do not want to run this}.

@yihui yihui closed this as completed Jan 25, 2012
@knokknok
Copy link
Contributor Author

Sure, that's what I'm used to doing... and that's the part that bugs me ;-)
It would be much nicer if I could just:

  • comment out some code without having to check that there are no Sexpr lurking in it (or finding out the hard way...)
  • uncomment the code and having it working without fixing the Sexpr back

@yihui
Copy link
Owner

yihui commented Jan 25, 2012

You mean comment all of them out using a global option? Why? That is going to make many incomplete sentences.

@knokknok
Copy link
Contributor Author

I mean that Sexpr in commented latex text should not be run.
And I'd like to not have to check for the presence of Sexpr in latex text when I comment it out.

When you comment out a chunk you usually also comment out the surrounding latex. The chunk is not run (thus some variables are not created) but the Sexpr is still run (and references to the variables fail...)

@yihui
Copy link
Owner

yihui commented Jan 25, 2012

I see. That is difficult, because it conflicts with the design of inline R code, which means code is in text lines (no matter what kind of lines) instead of individual blocks. If you comment out a block, the block is gone, but if you comment out texts with inline code, the code is still inline.

I do not have a trick for this issue, so I cannot do much about it at the moment.

@knokknok
Copy link
Contributor Author

What about changing the inline.code pattern to something like:
^[^%]\\Sexpr\{([^\\}])\}
?

@yihui yihui reopened this Jan 25, 2012
@yihui
Copy link
Owner

yihui commented Jan 25, 2012

This sounds like a good direction to go, but is not directly applicable to knitr's parsing design. I will think about how to incorporate this pattern. Thanks!

@yihui
Copy link
Owner

yihui commented Jan 26, 2012

The difficulty is you have two * which will make it difficult to locate the location of \Sexpr{}, e.g. in the following case, the location should be from 4 to 13 instead of 1 to 13.

library(stringr)
str_locate_all("abc \\Sexpr{1}", "^[^%]*\\\\Sexpr\\{([^\\}]*)\\}") 
## [[1]]
##      start end
## [1,]     1  13
## 

So I do not plan to add this feature. One way to get around is to use child documents, which you can easily comment out as a block: http://yihui.github.com/knitr/demo/child/

@yihui yihui closed this as completed Jan 26, 2012
@jamiefolson
Copy link

Sure, but this all applies to the child document all over again. Unless you're saying put every piece of code in it's own child Rnw, this doesn't help. Honestly, this is a big deal. It violates all expectations about the meaning of "comment out". I really don't understand the problem, especially since it already works that way for <<>>=@ code chunks.

I had darn well better be able to do use something to easily comment out a section of a Rnw file and know that it is both not going to be executed in R and not be processed by latex. Commenting out in latex and mangling the command is not acceptable to me.

FYI, this also leads to bugs processing things like the preamble that are commented out since comments are ignored.

@yihui
Copy link
Owner

yihui commented May 17, 2012

that makes sense, and I'm looking forward to your pull request :)

@knokknok
Copy link
Contributor Author

knokknok commented Jun 4, 2012

Would this work (of course, this is currently latex specific and would not work for languages with multiline comments...)?

  locate_inline = function(input, pattern) {
    input = gsub("(^|\n|[^\\\\])(%[^\n]*)", "\\1\\U\\2", input, perl=TRUE)
    x = cbind(start = numeric(0), end = numeric(0))
    if (group_pattern(pattern))
      x = str_locate_all(input, pattern)[[1]]
    x
  }

@yihui
Copy link
Owner

yihui commented Jun 4, 2012

you are right -- multiline comments will be tricky

I think I'd like to add support to LaTeX at the moment

@knokknok
Copy link
Contributor Author

knokknok commented Jun 4, 2012

Argg... this will also fail if you have 2 Sexpr on the same line and a % sign in the first one :-(

@yihui
Copy link
Owner

yihui commented Jun 4, 2012

Is it acceptable that I just ignore the lines ^\\s*%? I can just filter them out before parsing the inline code expressions.

@knokknok
Copy link
Contributor Author

knokknok commented Jun 4, 2012

I think that would be good enough for me. But that's just me ;-)

@yihui
Copy link
Owner

yihui commented Jun 4, 2012

For me, it makes little sense to read the tex document; to read the source, use Rnw; to read output, use PDF. The intermediate tex file is often messy, so probably people will not yell at me if I just remove the LaTeX comments in Rnw.

@knokknok
Copy link
Contributor Author

knokknok commented Jun 4, 2012

Never underestimate the capability of people to yell at you ;-)
(you could make it an option: keep your comments and be careful with your Sexpr OR lose them and don't bother...)

@yihui
Copy link
Owner

yihui commented Jun 4, 2012

then it will be inconvenient for those who want to comment out Sexpr -- they have to set the option before knit(); that means the document is not self-contained since certain options have to be set outside

OK, let me do it this way: I just remove inline R code from LaTeX comments, e.g. % x is \Sexpr{x} will become % x is. Hopefully this will make everybody happy...

@knokknok
Copy link
Contributor Author

knokknok commented Jun 5, 2012

Actually, you could just remove the \ sign so that your regex won't match but there is a minimal loss of information...

@yihui
Copy link
Owner

yihui commented Jun 5, 2012

OK, as a compromise, % x is \Sexpr{x} will become % x is x, i.e. R code is retained but \Sexpr is removed

@knokknok
Copy link
Contributor Author

knokknok commented Jun 5, 2012

Deal ;-)
Thanks!

yihui added a commit that referenced this issue Jun 5, 2012
kohske pushed a commit to kohske/knitr that referenced this issue Jun 21, 2012
inline comments will be stripped off; this only happens to LaTeX currently
yihui added a commit that referenced this issue Oct 12, 2016
@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature Feature requests
Projects
None yet
Development

No branches or pull requests

3 participants