Search code examples
ryamllatexr-markdownpdflatex

Undesired PDF format from particular R Markdown YAML configuration


In R Studio, my R Markdown document begins like this:

---
title: 'ST 412: Homework 3'
author: "Camden White"
documentclass: amsart
geometry: margin=1in
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## 1. Read Data Set and Define Variables

```{r, message=FALSE, warning=FALSE}
library(tidyverse)
library(magrittr)
```

When I do not include documentclass: amsart, everything works fine, but when I do include it, the library section is placed before the ## 1. Read Data Set and Define Variables header.

When there is text between the two components, the order is the same order in the code, but without it, the library section comes first. This does not happen with the default article document class, and I do not know why this occurs. How can I use the amsart document class and fix this ordering issue?


Solution

  • The problem seems to be that a subsection in amsart is basically what a paragraph is in normal classes: an unnumbered bold piece of text without an new paragraph after it. The text will just continue in the same line. As the grey box with the source code spans a whole line, it can't be placed there and shows up above. You can reproduce this with normal article class like this:

    \documentclass{article}
    \usepackage{framed}
    \begin{document}
    \paragraph{1. Read Data Set and Define}
    \begin{framed}
    test
    \end{framed}
    \end{document}
    

    You can avoid the problem by making sure the source code isn't the first text in the subsection, e.g. by adding something invisible:

    ---
    title: 'ST 412: Homework 3'
    author: "Camden White"
    documentclass: amsart
    geometry: margin=1in
    output: pdf_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ## 1. Read Data Set and Define Variables
    
    \mbox{}
    ```{r, message=FALSE, warning=FALSE}
    library(tidyverse)
    library(magrittr)
    ```
    

    enter image description here