Search code examples
rlatexr-markdown

rmarkdown::render doesn't work but does when knitting


Looked around a lot to find the answer to my question but I'm pretty stuck.

I'm trying to bulk render some pdfs using rmarkdown::render in a script but it keeps giving me this error:

! Use of \@array doesn't match its definition.
\new@ifnextchar ...served@d = #1\def \reserved@a {
                                              #2}\def \reserved@b {#3}\f...
l.85 ...2in}|>{\raggedright\arraybackslash}p{4in}}

Error: Failed to compile C:/Users/cmykim/Desktop/stackoverflow/testfile.tex. 
See testfile.log for more info.
In addition: Warning messages:
1: running command '"pdflatex" -halt-on-error -interaction=batchmode 
"C:/Users/cmykim/Desktop/stackoverflow/testfile.tex"' had status 1 
2: running command '"pdflatex" -halt-on-error -interaction=batchmode 
"C:/Users/cmykim/Desktop/stackoverflow/testfile.tex"' had status 1 

However, when I run the .Rmd file itself, it produces a PDF without issue.

I created an example that gives me the error below:

rmarkdown file:

---
output: pdf_document
---

```{r setup, include=FALSE}

## Packages
library(tidyverse)
library(knitr)
library(kableExtra)
library(tinytex)

dat <- data.frame(stringsAsFactors=FALSE,
                 id = c(1L, 2L, 3L),
               name = c("bob", "sam", "jane"),
          question1 = c("Lorem ipsum dolor sit amet", "consectetur adipiscing",
                        "Integer quis")
       )
dat <- dat %>% slice(1) %>% gather("key", "value")
```

```{r echo=FALSE}
    dat %>%
  kable("latex", col.names = NULL) %>%
  # making column 2 4 inches wide
  column_spec(2, width = "4in") %>%
  # making column 1 2 inches wide
  column_spec(1, width = "2in")

r script:

library(rmarkdown)
rmarkdown::render(input = 
"C:\\Users\\cmykim\\Desktop\\stackoverflow\\reproducible.Rmd",
                  output_format = "pdf_document",
                  output_file = "testfile.pdf",
                  output_dir = "C:\\Users\\cmykim\\Desktop\\stackoverflow")

I installed tinytex hoping it would fix it but it does not.

Any help would be appreciated!


Solution

  • On my machine I added the following to the YAML commands and it works:

    ---
    header-includes:
      \usepackage{array}
    output: 
      pdf_document
    ---