Reputation: 84519
The following Rnw
file produces the output shown below. Is there a clean way to prevent the line break in section 2?
Of course, this is just a minimal reproducible example; I don't want to remove the unevaluated chunk, which is programatically evaluated or not in my real issue.
\documentclass{article}
<<setup, include=FALSE>>=
knitr::opts_chunk$set(echo = FALSE)
@
\begin{document}
\section{eval TRUE}
<<results='asis'>>=
cat("Hello.")
@
<<eval=TRUE, results='asis'>>=
cat("How are you?")
@
What's your name?
\section{eval FALSE}
<<results='asis'>>=
cat("Hello.")
@
<<eval=FALSE, results='asis'>>=
cat("How are you?")
@
What's your name?
\end{document}
I have one solution so far:
<<results='asis'>>=
cat("Hello.")
if(FALSE) cat("How are you?")
@
What's your name?
But I'm wondering whether there's a simpler one, which does not require to group the chunks in a single one like this.
Upvotes: 2
Views: 179
Reputation: 14957
Interestingly, regardless of all options, knitr
apparently prints at least a newline for each chunk (even if eval = FALSE, echo = FALSE, results = "hide"
). Therefore, the following is just a workaround, but probably a cleaner workaround than the one in the question:
Use if
in the chunk (in lieu of the chunk option eval
) but print %
if the chunk is not supposed to be evaluated. This will make TEX ignore the line.
\documentclass{article}
<<setup, include=FALSE>>=
knitr::opts_chunk$set(echo = FALSE)
showIt <- FALSE
@
\begin{document}
\section{eval FALSE}
<<results='asis'>>=
cat("Hello.")
@
<<results='asis'>>=
if(showIt) {
cat("How are you?")
} else {
cat("%")
}
@
What's your name?
\end{document}
Upvotes: 2