psycho95
psycho95

Reputation: 141

R Markdownd: ! LaTeX Error: Two \documentclass or \documentstyle commands

I am trying to knit a pdf in Rmarkdown, but I receive the same error over and over again: ! LaTeX Error: Two \documentclass or \documentstyle commands.

I already different approaches like deleting some rows, putting \begin{document} in different places or changing the order of loading packages.

This is my code:

---
title: "Feedback"
author: "Me"
date: "09 02 2021"
toc: true
toc_depth: 2
toc-title: "Verzeichnis"
fig.caption: "Tabelle"
smooth_scroll: false
header-includes: 
  \documentclass{article}
  \PassOptionsToPackage[usenames,dvipsnames]{xcolor}
  \usepackage{fancyhdr}
  \usepackage[T1]{fontenc}
  \usepackage[default]{sourcesanspro}
  \usepackage{tikz}
mainfont: SourceSansPro
output: pdf_document
---
\begin{document}


\addtolength{\headheight}{1.0cm} 
\fancypagestyle{plain}{} 
\thispagestyle{fancy} 
\fancyhead[L]{\includegraphics[width = 500pt]{"/Users/lisak/One Drive/OneDrive/Dokumente/Masterarbeit/Erhebung/Anschreiben/alt/banner.png"}}
\renewcommand{\headrulewidth}{0pt} 


{r, echo = FALSE, message = FALSE}
.onLoad <- function(libname = find.package("kableExtra"), pkgname = "kableExtra") {
  if (knitr::is_latex_output()) {
    load_packages <- getOption("kableExtra.latex.load_packages", default = TRUE)
    if (load_packages) {
      usepackage_latex("booktabs")
      usepackage_latex("longtable")
      usepackage_latex("array")
      usepackage_latex("multirow")
      usepackage_latex("wrapfig")
      usepackage_latex("float")
      usepackage_latex("colortbl")
      usepackage_latex("pdflscape")
      usepackage_latex("tabu")
      usepackage_latex("threeparttable")
      usepackage_latex("threeparttablex")
      usepackage_latex("ulem", "normalem")
      usepackage_latex("makecell")
      usepackage_latex("xcolor")   
      
    }
  }
  auto_format <- getOption("kableExtra.auto_format", default = TRUE)
  if (auto_format) auto_set_format()
  if (!is.null(rmarkdown::metadata$output) &&
      rmarkdown::metadata$output %in% c(
        "ioslides_presentation", "slidy_presentation",
        "gitbook", "bookdown::gitbook", "radix_article", "radix::radix_article",
        "distill_article", "distill::distill_article"
      )) {
    options(kableExtra.html.bsTable = TRUE)
  }
  if (!is.null(knitr::opts_knit$get("rmarkdown.pandoc.to")) &&
      knitr::opts_knit$get("rmarkdown.pandoc.to") %in% c("epub3", "epub")) {
    options(kableExtra.knit_print.dependency = FALSE)
  }
}

stuff....

\end{document}

Upvotes: 2

Views: 1946

Answers (1)

  • Rmarkdown will automatically insert stuff like \documentclass{article} and \begin{document} when converting the rmarkdown document into a tex document. You must not insert it a second time

  • \PassOptionsToPackage[usenames,dvipsnames]{xcolor} must be used before the documentclass. As rmarkdown takes away your ability to insert the document class yourself, you can't use this

  • rmarkdown has trouble to correctly parse the square brackets of optional arguments. As a workaround, you can hide these commands in a separate .tex file

  • the syntax of your r chunk is wrong. You must surround it with backticks


---
title: "Feedback"
author: "Me"
date: "09 02 2021"
toc: true
toc_depth: 2
toc-title: "Verzeichnis"
fig.caption: "Tabelle"
smooth_scroll: false
mainfont: SourceSansPro
output: 
  pdf_document:
    keep_tex: true
    includes:
      in_header: preamble.tex
---


```{r, echo = FALSE, message = FALSE}
.onLoad <- function(libname = find.package("kableExtra"), pkgname = "kableExtra") {
  if (knitr::is_latex_output()) {
    load_packages <- getOption("kableExtra.latex.load_packages", default = TRUE)
    if (load_packages) {
      usepackage_latex("booktabs")
      usepackage_latex("longtable")
      usepackage_latex("array")
      usepackage_latex("multirow")
      usepackage_latex("wrapfig")
      usepackage_latex("float")
      usepackage_latex("colortbl")
      usepackage_latex("pdflscape")
      usepackage_latex("tabu")
      usepackage_latex("threeparttable")
      usepackage_latex("threeparttablex")
      usepackage_latex("ulem", "normalem")
      usepackage_latex("makecell")
      usepackage_latex("xcolor")   
      
    }
  }
  auto_format <- getOption("kableExtra.auto_format", default = TRUE)
  if (auto_format) auto_set_format()
  if (!is.null(rmarkdown::metadata$output) &&
      rmarkdown::metadata$output %in% c(
        "ioslides_presentation", "slidy_presentation",
        "gitbook", "bookdown::gitbook", "radix_article", "radix::radix_article",
        "distill_article", "distill::distill_article"
      )) {
    options(kableExtra.html.bsTable = TRUE)
  }
  if (!is.null(knitr::opts_knit$get("rmarkdown.pandoc.to")) &&
      knitr::opts_knit$get("rmarkdown.pandoc.to") %in% c("epub3", "epub")) {
    options(kableExtra.knit_print.dependency = FALSE)
  }
}
```

stuff....

and preamble.tex:

\usepackage{fancyhdr}
\usepackage[T1]{fontenc}
\usepackage[default]{sourcesanspro}
\usepackage{tikz}
\addtolength{\headheight}{1.0cm} 
\fancypagestyle{plain}{} 
\thispagestyle{fancy} 
\fancyhead[L]{\includegraphics[width = 500pt]{example-image}}
\renewcommand{\headrulewidth}{0pt} 

Upvotes: 1

Related Questions