MYaseen208
MYaseen208

Reputation: 23938

Splitting knitr chunk code and its output

This is the follow-up question to this one. My MWE with output is given below. The second R chunk code doesn't have any output. So I want not to split the knitrout into two pieces.

Code

\documentclass{article} 
\begin{document}

<<setup, include=FALSE>>=
knit_hooks$set(
source = function(x, options) {
      x = knitr:::hilight_source(x, 'latex', options)
      if (options$highlight) {
        if (options$engine == 'R' || x[1] != '\\noindent') {
          paste(c('\\noindent\\textbf{R Code:}\\begin{alltt}', x, '\\end{alltt}', '','\\end{kframe} \\noindent and \\begin{kframe}\\noindent\\textbf{R Output:}'),
                collapse = '\n')
        } else {
          if ((n <- length(x)) > 5) x[n - 3] = sub('\\\\\\\\$', '', x[n - 3])
          paste(c('\\noindent\\textbf{R Code:}',x, '','\\noindent\\textbf{R Output:}'),
                collapse = '\n')
        }
      } else .verb.hook(x)
    }
)
@

Here's your first chunk.

<<chunk1, results = "hold" >>=
1:100
args(lm)
@ 

And here's another.

<<chunk2, results = "hold">>=
X <- 1:100
@ 

That seems to be it.

\end{document}

Output

enter image description here

Upvotes: 0

Views: 614

Answers (1)

EDi
EDi

Reputation: 13310

You could modify your hook, adding results = 'hide'. With your hook and \\begin{kframe}\\noindent\\textbf{R Output:}' is always printet.

\documentclass{article} 
\begin{document}

<<setup, include=FALSE>>=
knit_hooks$set(
source = function(x, options) {
      x = knitr:::hilight_source(x, 'latex', options)
      if (options$highlight) {
        if (options$engine == 'R' || x[1] != '\\noindent') {
          if(options$results == 'hide'){
             paste(c('\\noindent\\textbf{R Code:}\\begin{alltt}', x, '\\end{alltt}'),
                  collapse = '\n')
          } else {
            paste(c('\\noindent\\textbf{R Code:}\\begin{alltt}', x, '\\end{alltt}', '','\\end{kframe} \\noindent and \\begin{kframe}\\noindent\\textbf{R Output:}'),
                  collapse = '\n')
          }
        } else {
          if ((n <- length(x)) > 5) x[n - 3] = sub('\\\\\\\\$', '', x[n - 3])
          paste(c('\\noindent\\textbf{R Code:}',x, '','\\noindent\\textbf{R Output:}'),
                collapse = '\n')
        }
      } else .verb.hook(x)
    }
)
@

Here's your first chunk.

<<chunk1, results = "hold" >>=
1:100
args(lm)
@ 

And here's another.

<<chunk2, results = "hide">>=
X <- 1:100
@ 

That seems to be it.

\end{document}

Upvotes: 1

Related Questions