Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decreasing space between commands and output in knitr chunks

Tags:

r

latex

knitr

I'm using knitr with LaTeX and there seems to be a lot of space between the commands echoed by a code chunk and the start of the output:

enter image description here

The LaTeX code for this looks like:

  \begin{knitrout}\scriptsize
  \definecolor{shadecolor}{rgb}{1, 1, 1}\color{fgcolor}\begin{kframe}
  \begin{alltt}
  \hlstd{> }\hlstd{lda_test_pred} \hlkwb{<-} \hlkwd{predict}\hlstd{(lda_fit,} \hlkwc{newdata} \hlstd{= seg_test)}
  \hlstd{> }\hlkwd{library}\hlstd{(pROC)}
  \hlstd{> }
  \hlstd{> }\hlstd{lda_roc} \hlkwb{<-} \hlkwd{roc}\hlstd{(}\hlkwc{response} \hlstd{= seg_test}\hlopt{$}\hlstd{Class,}
  \hlstd{+ }               \hlkwc{predictor} \hlstd{= lda_test_pred}\hlopt{$}\hlstd{posterior[,} \hlstr{"PS"}\hlstd{],}
  \hlstd{+ }               \hlcom{## we need to tell the function that the _first_ level}
  \hlstd{+ }               \hlcom{## is our event of interest}
  \hlstd{+ }               \hlkwc{levels} \hlstd{=} \hlkwd{rev}\hlstd{(}\hlkwd{levels}\hlstd{(seg_test}\hlopt{$}\hlstd{Class)))}
  \hlstd{> }\hlstd{lda_roc}
  \end{alltt}
  \begin{verbatim}

  Call:
  roc.default(response = seg_test$Class, predictor = lda_test_pred$posterior[,     "PS"], levels = rev(levels(seg_test$Class)))

  Data: lda_test_pred$posterior[, "PS"] in 346 controls (seg_test$Class WS) < 664 cases (seg_test$Class PS).
  Area under the curve: 0.874
  \end{verbatim}
  \begin{alltt}
  \hlstd{> }\hlcom{# plot(exRoc print.thres = .5)}
  \end{alltt}
  \end{kframe}
  \end{knitrout}

The space is generated between the end of alltt and the start of verbatim. Part of the gap, for this example, is the blank line prior to the call output.

Any ideas on how to modulate this in knitr (without affecting any spacing between paragraphs etc)?

like image 665
topepo Avatar asked Jan 18 '16 23:01

topepo


1 Answers

Follow the advice found here

control vertical space before and after verbatim environment?

and add the following lines to your document:

\usepackage{etoolbox} 
\makeatletter 
\preto{\@verbatim}{\topsep=0pt \partopsep=0pt } 
\makeatother

For some more detail you can check this answer.

like image 105
Martin Schmelzer Avatar answered Nov 20 '22 19:11

Martin Schmelzer