knitr single space code when class defaults double space (Rnw)

989 views Asked by At

When using knitr with a class like apa6e the code chunks default to double space like rest of the document. This is not desirable.

This could be altered by wrapping with:

\begin{singlespace}
<<*>>=
CODE
@
\end{singlespace}

How can this be done globally rather than wrapping with the tex single space tags?

MWE .Rnw file

\documentclass[leavefloats]{apa6e}
\usepackage[american]{babel}
\usepackage{csquotes}
\usepackage[style=apa,backend=biber,bibencoding=latin1]{biblatex}
\DeclareLanguageMapping{american}{american-apa}

\begin{document}

<<setup, include=FALSE, cache=FALSE>>=
# set global chunk options
opts_chunk$set(fig.path='figure/minimal-', fig.align='center', fig.show='hold')
options(replace.assign=TRUE,width=90)
library(ggplot2); library(xtable)
@

\title{MWE}
\shorttitle{MWE}
\author{Tyler Rinker}
\date{\today}
\authornote{\dots}
\abstract{This is an example of an abstract in APA.}
\maketitle


<<foo>>=
x <- "Got me some code"
y <- "look another line"
paste(x, y)
@

\begin{singlespace}
<<bar>>=
x <- "Got me some code"
y <- "look another line"
paste(x, y)
@
\end{singlespace}

\end{document}
1

There are 1 answers

0
Peyton On BEST ANSWER

You can redefine knitrout (which is empty by default) to place knitr output in the singlespace environment:

\renewenvironment{knitrout}{\begin{singlespace}}{\end{singlespace}}