knitr and Beamer: get rid of extra blank lines around code and output

1,364 views
Skip to first unread message

Bob

unread,
Sep 12, 2013, 12:54:47 PM9/12/13
to kn...@googlegroups.com
Hi,

I have searched the web back and forth and there are some hints on this but they did not lead to a success for me.

I am writing a .Rnw beamer presentation using RStudio and knitr for weaving.
My problem is that there is always extra blank lines between the R input and output. Looking at the .tex output, it seems to me that it is caused by the \begin{alltt} \end{alltt} environment. Is there any chance to get rid of such blank lines? They take up too much space on my slides.

Many thanks!
Bob

\begin{frame}[fragile]
  \frametitle{A matrix}
TEST
<<results='markup'>>=
  A <- matrix(1:4, nrow=2)
  A
@
\end{frame}


Yihui Xie

unread,
Sep 12, 2013, 4:37:26 PM9/12/13
to Bob, knitr
As you said, that is caused by the alltt and verbatim environments for
the source code and output, respectively. Both environments add
vertical space around them by default. I just googled a while, and
found one solution for verbatim but not alltt:

http://tex.stackexchange.com/questions/43331/control-vertical-space-before-and-after-verbatim-environment

This solution alone won't work. I do not know how to remove the
vertical space for alltt. You may try StackExchange.

If you do not mind syntax highlighting, you can turn off the highlight
option, e.g.

<<highlight=FALSE>>=
A <- matrix(1:4, nrow=2)
A
@

Then everything will be in a single verbatim environment, and you will
no longer have the space problem.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA

Bob

unread,
Sep 13, 2013, 5:47:31 AM9/13/13
to kn...@googlegroups.com, Bob
Thanks a million for the solution, Yihui!

Although I usually like the highlighting as done by knitr, the extra space is more important right now so I switch it off.

Best,
Bob

Michał Bojanowski

unread,
Mar 3, 2014, 7:10:26 AM3/3/14
to kn...@googlegroups.com, Bob
I think redefining knitrout environment and setting \topsep to 0 solves the problem, i.e.:

\newenvironment{knitrout}{\setlength{\topsep}{0mm}}{} 

~michal

Yihui Xie

unread,
Mar 3, 2014, 2:07:03 PM3/3/14
to Michał Bojanowski, knitr, Bob
Thanks for sharing the solution!

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name


Andrew Campbell

unread,
Sep 22, 2014, 2:45:22 AM9/22/14
to kn...@googlegroups.com, frot...@yahoo.com
I am brand new to LaTEX and Knitr. Can you explain where I use the line: \newenvironment{knitrout}{\setlength{\topsep}{0mm}}{}  

I've tried to paste it, exactly as is, at the top of my source file. I've also tried enclosing the R chunk within the last set of brackets.

Every time, I get the same error:

LaTEX Error: Command \knitrout already defined

Thanks!

Tim Kacprowski

unread,
Nov 3, 2015, 7:09:43 AM11/3/15
to knitr
in case you still have that problem, just replace \newenvironment by \renewenvironment.
so the correct comamnd would be
\renewenvironment{knitrout}{\setlength{\topsep}{0mm}}{}
Reply all
Reply to author
Forward
0 new messages