Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

One distinction between knitr and Sweave is that the former forcibly inserts two empty lines in the generated TeX source for chunks that do not do any output. I'm not sure if this issue is a bug or a feature, but for now I'm looking for a workaround.

The problem can be summarized as follows: .Rnw code like

a
<<q,echo=FALSE,results='hide'>>=
getwd()
@
b

produces LaTeX code like

a


b

where the space between a and b is not always desired. The best workaround that I found so far is to write

a\q{
<<q,echo=FALSE,results='hide'>>=
getwd()
@
}b

and define \q as a one-parameter command that expands to a single space. Is there a shorter way to achieve a similar result, perhaps without using braces and without affecting the line that starts with b?

ADDED: Is there a way to assign to a special character, say, §, the semantics of this command, so that the following will work:

a §
<<q,echo=FALSE,results='hide'>>=
getwd()
@
b

(Of course, a knitr way to achieve this would be most welcome, too.)

This answer seems related, but I haven't been able to get the \eatpar command to work. Would it eat two empty lines, too?

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

The following definition (taken from this blog) appears to work:

\makeatletter
\newcommand\gobblepars{%
    \@ifnextchar\par%
        {\expandafter\gobblepars\@gobble}%
        {}}
\makeatother

Then, I can write:

a \gobblepars
<<q,echo=FALSE,results='hide'>>=
getwd()
@
b

to achieve the desired result.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.