Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

knitr

rnw

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}
like image 477
Tyler Rinker Avatar asked Dec 16 '13 19:12

Tyler Rinker


1 Answers

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

\renewenvironment{knitrout}{\begin{singlespace}}{\end{singlespace}}
like image 189
Peyton Avatar answered Nov 11 '22 18:11

Peyton