Skip to Main Content
The University of Massachusetts Amherst

LaTeX

Overview of knitr

Are you using R to do data analysis and/or create visualizations you plan to report using LaTeX? Good news! You can directly integrate your R code into your LaTeX code, thanks to a software engine called knitr. If you are using Overleaf, there is nothing you need to set up, simply follow the guidelines below to get started.

Chunks

knitr operates using segments of code referred to as "chunks." You can think of chunks as working similarly to environments in LaTeX. Chunks are created using the following code:

<<>>=

R Code Goes Here.

@

Options can be inserted inside the top-line (in between << and >>=) that will affect how the code is reported/executed, as well as how any plots are formatted. The chunk can also be labeled, simply by typing a name without an option. The chunk can then be referenced using the command: \ref{fig:chunkname}.See the table below for some common chunk options and their effects.

Option Effect Arguments

echo

Display Code in Resulting Output PDF

True or T = Code is displayed (default)

False or F = Code is NOT displayed (Code is still executed: plots will still be created and results of analysis can still be used/referenced)

include Display Whole Chunk (Code and Results) in Resulting Output PDF 

True or T = Code and results are displayed (default)

False or F = Code AND results are NOT displayed (Code is still executed: plots will still be created and results of analysis can still be used/referenced)

eval Execute Code in the Chunk

True or T = Code is executed (default)

False or F = Code is NOT executed (Code can still be displayed in output)

fig.width Width of Plot Created X = Plot will have a width of X inches
fig.height Height of Plot Created X = Plot will have a height of X inches
fig.pos Position in Output PDF for Plot Created

"h" = Plot will be positioned approximately "here" (where it is relative to other content)

"t" = Plot will be positioned at top of page

"b" = Plot will be positioned at bottom of page

"p" = Plot will be put on its own page

"H" = Plot will be positioned exactly "here" (where it is relative to other content)

"!" *Use with another argument to force the plot to be placed in that position (e.g. "b!" will force the plot to be at the bottom of the page)

fig.cap Caption for Plot Created "Example Caption Text" = Example Caption Text
fig.align Alignment in Output PDF for Plot Created

"center" = center-aligned

"left" = left-aligned

"right" = right-aligned

 

In-Line R Code

You may want to reference small pieces of information produced by your embedded R code Chunks (e.g. a p-value from an ANOVA) when writing in your LaTeX document. Rather than copying a value from the R output and pasting it in your LaTeX source code, you can use the command \Sexpr{R Code} to integrate the R code (and its output) directly into your writing. This way, should the referenced output change (e.g. you update the dataset, affecting the statistical analysis) your references to the output will update automatically.

Example Output

Below is the output PDF created in LaTeX, using knitr to embed R code. The R code embedded in the LaTeX code allows the user to produce a plot (in this example, using ggplot) directly in the output document. The embedded R code is also used to perform the ANOVA and Tukey's HSD referenced in the text. The p-values referenced in the text are generated by using the \Sexpr{R Code} command described above. View the full project on Overleaf here.