This tutorial provides a brief overview of how to create Sweave (LaTeX + R) documents in APA style.

# 0: Setup

### Install LaTeX packages

In order to create LaTeX documents, you need to install LaTeX libraries on your computer. Here are links to Mac and Windows LaTeX libraries

### Install R packages

Next you should install some R packages that will help you create Sweave documents. Open RStudio and run the following two lines of code:

# These packages help with Sweave
install.packages("knitr")
install.packages("xtable")

### Update RStudio preferences

Next, you’ll need to change some of the the RStudio Sweave preferences.

Go to RStudio – Preferences – Sweave and make the following two changes:

• Select “Sweave Rnw files using Knitr
• Set “Preview PDF” with “System Viewer”

# 1: A simple APA Sweave Template

1. Open a new Sweave file in RSTudio: File – New File – R Sweave.

When you do this, you’ll see a bare-bones file with a three commands.

1. Save the file under a new name with the .Rnw suffix (e.g.; myfirstsweave.Rnw)
2. Now open a web-browser and go to the following address to find an APA style Sweave template (Simple APA Sweave File)
3. Copy and paste all of the text from the template and paste it into your Sweave file.
4. Click the “Compile PDF” button at the top of your screen. When you do this, you should see some processing output, followed by a PDF!

# 2: Basic LaTeX commands

Important aspects of the document, such as the title, author, and affiliation are specified with commands in the form \command{}.

• The main aspects of your document such as the title, header, and author are specified with commands like \title{}, \shorttitle{}, \author{}.

• Sections are defined using the \section{} and \subsection{} commands. For example, to start the Method section, you write \section{Method}. To include the Participants subsection, you write \subsection{Participants}

• Bold and italic text are created with the \bf{} and \emph{} commands:

• SWEAVE: Here is how you write \bf{bold text}, here is how you write \emph{italic text}.
• PDF: Here is how you write bold text, here is how you write italic text.

# 3: R code chunks

You can incorporate R code into your Sweave document in one of two ways: with regular chunks, or with mini-chunks. Everything you put in an R chunk will be evaluated as R code.

## Regular R chunks

Regular R chunks start with a start ‘tag’ <<>>= and end with an end ‘tag’ @. They are indicated with gray backgrounds and look like this:

You can create a new R chunk either by manually typing the start <<>>= and end @ tags, or by clicking the green “Insert a new code chunk” button on the top right of your editor window in RStudio.

You can specify many arguments at the top of the chunk (in between <<>>=) that will change how the code is evaluated. Here are some key arguments:

• eval: Should to code in the chunk be evaluated (eval = T) or completely ignored (eval = F)? If you have code in a chunk that is not complete, but you still want to create the document without that chunk, set eval = F.

• echo: Should the raw code be repeated in the final document (echo = T) or not? (echo = F). In most APA style papers you’ll never repeat the code so you’ll usually set echo = F.

• fig.width, fig.height, fig.align: If your chunk creates a figure, these arguments will change its size and location. For example, fig.width = 4, fig.height = 10, fig.align = ‘center’ will create a centered figure that is 4 inches wide and 10 inches tall.

If you want to change some options for all of your R chunks, you can do this using the ops_chunk$set() function in an R chunk at the beginning of your document. For example, in virtually all of your R chunks you will not want to echo (aka repeat) the source code in the document. For this reason, it’s good to set the global chunk option to echo = F. ## Mini-code chunks If you want to include the output of R code (e.g.; the p-value of a t.test) directly into your text, you should use a mini-chunk. Mini-chunks are specified with the \Sexpr{} command. Everything in the curly braces will be interpreted as R code. For example, the following sentence in Sweave will be printed in the PDF as follows • SWEAVE: The mean age of participants was \Sexpr{mean(data$age)} and there were \Sexpr{sum(data\$sex == “f”)} females.
• PDF: The mean age of participants was 21 and there were 100 females.

# 4: Including figures

To include figures, use the \begin{figure} and \end{figure} commands. In between these commands, you’ll specify how to create, or find, the figure.

## Creating figures in R Chunks

To create the figure directly using R code, create an R chunk that creates the figure. For example, the following code chunk creates a histogram

## Importing figures from external files

If your figure is in an external file (like a pdf or jpg), you don’t need to use an R chunk. Instead, you include the figure using the \includegraphics{} command. Just put the name (and possibily the file path) of the figure in the braces.